github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/route/create_route.go (about) 1 package route 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/cf/api" 7 "code.cloudfoundry.org/cli/cf/commandregistry" 8 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 9 "code.cloudfoundry.org/cli/cf/flags" 10 . "code.cloudfoundry.org/cli/cf/i18n" 11 "code.cloudfoundry.org/cli/cf/models" 12 "code.cloudfoundry.org/cli/cf/requirements" 13 "code.cloudfoundry.org/cli/cf/terminal" 14 ) 15 16 //go:generate counterfeiter . Creator 17 18 type Creator interface { 19 CreateRoute(hostName string, path string, port int, randomPort bool, domain models.DomainFields, space models.SpaceFields) (route models.Route, apiErr error) 20 } 21 22 type CreateRoute struct { 23 ui terminal.UI 24 config coreconfig.Reader 25 routeRepo api.RouteRepository 26 spaceReq requirements.SpaceRequirement 27 domainReq requirements.DomainRequirement 28 } 29 30 func init() { 31 commandregistry.Register(&CreateRoute{}) 32 } 33 34 func (cmd *CreateRoute) MetaData() commandregistry.CommandMetadata { 35 fs := make(map[string]flags.FlagSet) 36 fs["hostname"] = &flags.StringFlag{Name: "hostname", ShortName: "n", Usage: T("Hostname for the HTTP route (required for shared domains)")} 37 fs["path"] = &flags.StringFlag{Name: "path", Usage: T("Path for the HTTP route")} 38 fs["port"] = &flags.IntFlag{Name: "port", Usage: T("Port for the TCP route")} 39 fs["random-port"] = &flags.BoolFlag{Name: "random-port", Usage: T("Create a random port for the TCP route")} 40 41 return commandregistry.CommandMetadata{ 42 Name: "create-route", 43 Description: T("Create a url route in a space for later use"), 44 Usage: []string{ 45 fmt.Sprintf("%s:\n", T("Create an HTTP route")), 46 " CF_NAME create-route ", 47 fmt.Sprintf("%s ", T("SPACE")), 48 fmt.Sprintf("%s ", T("DOMAIN")), 49 fmt.Sprintf("[--hostname %s] ", T("HOSTNAME")), 50 fmt.Sprintf("[--path %s]\n\n", T("PATH")), 51 fmt.Sprintf(" %s:\n", T("Create a TCP route")), 52 " CF_NAME create-route ", 53 fmt.Sprintf("%s ", T("SPACE")), 54 fmt.Sprintf("%s ", T("DOMAIN")), 55 fmt.Sprintf("(--port %s | --random-port)", T("PORT")), 56 }, 57 Examples: []string{ 58 "CF_NAME create-route my-space example.com # example.com", 59 "CF_NAME create-route my-space example.com --hostname myapp # myapp.example.com", 60 "CF_NAME create-route my-space example.com --hostname myapp --path foo # myapp.example.com/foo", 61 "CF_NAME create-route my-space example.com --port 50000 # example.com:50000", 62 }, 63 Flags: fs, 64 } 65 } 66 67 func (cmd *CreateRoute) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 68 if len(fc.Args()) != 2 { 69 cmd.ui.Failed(T("Incorrect Usage. Requires SPACE and DOMAIN as arguments\n\n") + commandregistry.Commands.CommandUsage("create-route")) 70 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2) 71 } 72 73 if fc.IsSet("port") && (fc.IsSet("hostname") || fc.IsSet("path")) { 74 cmd.ui.Failed(T("Cannot specify port together with hostname and/or path.")) 75 return nil, fmt.Errorf("Cannot specify port together with hostname and/or path.") 76 } 77 78 if fc.IsSet("random-port") && (fc.IsSet("port") || fc.IsSet("hostname") || fc.IsSet("path")) { 79 cmd.ui.Failed(T("Cannot specify random-port together with port, hostname and/or path.")) 80 return nil, fmt.Errorf("Cannot specify random-port together with port, hostname and/or path.") 81 } 82 83 domainName := fc.Args()[1] 84 85 cmd.spaceReq = requirementsFactory.NewSpaceRequirement(fc.Args()[0]) 86 cmd.domainReq = requirementsFactory.NewDomainRequirement(domainName) 87 88 reqs := []requirements.Requirement{ 89 requirementsFactory.NewLoginRequirement(), 90 requirementsFactory.NewTargetedOrgRequirement(), 91 cmd.spaceReq, 92 cmd.domainReq, 93 } 94 95 return reqs, nil 96 } 97 98 func (cmd *CreateRoute) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 99 cmd.ui = deps.UI 100 cmd.config = deps.Config 101 cmd.routeRepo = deps.RepoLocator.GetRouteRepository() 102 return cmd 103 } 104 105 func (cmd *CreateRoute) Execute(c flags.FlagContext) error { 106 hostName := c.String("n") 107 space := cmd.spaceReq.GetSpace() 108 domain := cmd.domainReq.GetDomain() 109 path := c.String("path") 110 port := c.Int("port") 111 randomPort := c.Bool("random-port") 112 113 _, err := cmd.CreateRoute(hostName, path, port, randomPort, domain, space.SpaceFields) 114 if err != nil { 115 return err 116 } 117 118 return nil 119 } 120 121 func (cmd *CreateRoute) CreateRoute(hostName string, path string, port int, randomPort bool, domain models.DomainFields, space models.SpaceFields) (models.Route, error) { 122 cmd.ui.Say(T("Creating route {{.URL}} for org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", 123 map[string]interface{}{ 124 "URL": terminal.EntityNameColor(domain.URLForHostAndPath(hostName, path, port)), 125 "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), 126 "SpaceName": terminal.EntityNameColor(space.Name), 127 "Username": terminal.EntityNameColor(cmd.config.Username())})) 128 129 route, err := cmd.routeRepo.CreateInSpace(hostName, path, domain.GUID, space.GUID, port, randomPort) 130 if err != nil { 131 var findErr error 132 route, findErr = cmd.routeRepo.Find(hostName, domain, path, port) 133 if findErr != nil { 134 return models.Route{}, err 135 } 136 137 if route.Space.GUID != space.GUID || route.Domain.GUID != domain.GUID { 138 return models.Route{}, err 139 } 140 141 cmd.ui.Ok() 142 cmd.ui.Warn(T("Route {{.URL}} already exists", 143 map[string]interface{}{"URL": route.URL()})) 144 145 return route, nil 146 } 147 148 cmd.ui.Ok() 149 if randomPort { 150 cmd.ui.Say("Route %s:%d has been created", route.Domain.Name, route.Port) 151 } 152 153 return route, nil 154 }