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