github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/command/v7/create_route_command.go (about) 1 package v7 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/command/flag" 8 ) 9 10 type CreateRouteCommand struct { 11 BaseCommand 12 13 RequiredArgs flag.Domain `positional-args:"yes"` 14 usage interface{} `usage:"Create an HTTP route:\n CF_NAME create-route DOMAIN [--hostname HOSTNAME] [--path PATH]\n\n Create a TCP route:\n CF_NAME create-route DOMAIN [--port PORT]\n\nEXAMPLES:\n CF_NAME create-route example.com # example.com\n CF_NAME create-route example.com --hostname myapp # myapp.example.com\n CF_NAME create-route example.com --hostname myapp --path foo # myapp.example.com/foo\n CF_NAME create-route example.com --port 5000 # example.com:5000"` 15 Hostname string `long:"hostname" short:"n" description:"Hostname for the HTTP route (required for shared domains)"` 16 Path flag.V7RoutePath `long:"path" description:"Path for the HTTP route"` 17 Port int `long:"port" description:"Port for the TCP route (default: random port)"` 18 relatedCommands interface{} `related_commands:"check-route, domains, map-route, routes, unmap-route"` 19 } 20 21 func (cmd CreateRouteCommand) Execute(args []string) error { 22 err := cmd.SharedActor.CheckTarget(true, true) 23 if err != nil { 24 return err 25 } 26 27 user, err := cmd.Config.CurrentUser() 28 if err != nil { 29 return err 30 } 31 32 domain := cmd.RequiredArgs.Domain 33 hostname := cmd.Hostname 34 pathName := cmd.Path.Path 35 port := cmd.Port 36 spaceName := cmd.Config.TargetedSpace().Name 37 orgName := cmd.Config.TargetedOrganization().Name 38 spaceGUID := cmd.Config.TargetedSpace().GUID 39 url := desiredURL(domain, hostname, pathName, port) 40 41 cmd.UI.DisplayTextWithFlavor("Creating route {{.URL}} for org {{.Organization}} / space {{.Space}} as {{.User}}...", 42 map[string]interface{}{ 43 "URL": url, 44 "User": user.Name, 45 "Space": spaceName, 46 "Organization": orgName, 47 }) 48 49 route, warnings, err := cmd.Actor.CreateRoute(spaceGUID, domain, hostname, pathName, port) 50 51 cmd.UI.DisplayWarnings(warnings) 52 if err != nil { 53 if _, ok := err.(actionerror.RouteAlreadyExistsError); ok { 54 cmd.UI.DisplayWarning(err.Error()) 55 cmd.UI.DisplayOK() 56 return nil 57 } 58 return err 59 } 60 61 cmd.UI.DisplayText("Route {{.URL}} has been created.", 62 map[string]interface{}{ 63 "URL": route.URL, 64 }) 65 66 cmd.UI.DisplayOK() 67 return nil 68 } 69 70 func desiredURL(domain, hostname, path string, port int) string { 71 url := "" 72 73 if hostname != "" { 74 url += hostname + "." 75 } 76 77 url += domain 78 79 if path != "" { 80 url += path 81 } 82 83 if port != 0 { 84 url += fmt.Sprintf(":%d", port) 85 } 86 87 return url 88 }