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