github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/route/check_route.go (about) 1 package route 2 3 import ( 4 "fmt" 5 "strings" 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 CheckRoute struct { 17 ui terminal.UI 18 config coreconfig.Reader 19 routeRepo api.RouteRepository 20 domainRepo api.DomainRepository 21 } 22 23 func init() { 24 commandregistry.Register(&CheckRoute{}) 25 } 26 27 func (cmd *CheckRoute) MetaData() commandregistry.CommandMetadata { 28 fs := make(map[string]flags.FlagSet) 29 fs["path"] = &flags.StringFlag{Name: "path", Usage: T("Path for the route")} 30 31 return commandregistry.CommandMetadata{ 32 Name: "check-route", 33 Description: T("Perform a simple check to determine whether a route currently exists or not"), 34 Usage: []string{ 35 T("CF_NAME check-route HOST DOMAIN [--path PATH]"), 36 }, 37 Examples: []string{ 38 "CF_NAME check-route myhost example.com # example.com", 39 "CF_NAME check-route myhost example.com --path foo # myhost.example.com/foo", 40 }, 41 Flags: fs, 42 } 43 } 44 45 func (cmd *CheckRoute) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 46 if len(fc.Args()) != 2 { 47 cmd.ui.Failed(T("Incorrect Usage. Requires host and domain as arguments\n\n") + commandregistry.Commands.CommandUsage("check-route")) 48 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 2) 49 } 50 51 var reqs []requirements.Requirement 52 53 reqs = append(reqs, []requirements.Requirement{ 54 requirementsFactory.NewTargetedOrgRequirement(), 55 requirementsFactory.NewLoginRequirement(), 56 }...) 57 58 return reqs, nil 59 } 60 61 func (cmd *CheckRoute) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 62 cmd.ui = deps.UI 63 cmd.config = deps.Config 64 cmd.routeRepo = deps.RepoLocator.GetRouteRepository() 65 cmd.domainRepo = deps.RepoLocator.GetDomainRepository() 66 return cmd 67 } 68 69 func (cmd *CheckRoute) Execute(c flags.FlagContext) error { 70 hostName := c.Args()[0] 71 domainName := c.Args()[1] 72 path := c.String("path") 73 74 cmd.ui.Say(T("Checking for route...")) 75 76 exists, err := cmd.CheckRoute(hostName, domainName, path) 77 if err != nil { 78 return err 79 } 80 81 cmd.ui.Ok() 82 83 var existence string 84 if exists { 85 existence = "does exist" 86 } else { 87 existence = "does not exist" 88 } 89 90 if path != "" { 91 cmd.ui.Say(T("Route {{.HostName}}.{{.DomainName}}/{{.Path}} {{.Existence}}", 92 map[string]interface{}{ 93 "HostName": hostName, 94 "DomainName": domainName, 95 "Existence": existence, 96 "Path": strings.TrimPrefix(path, `/`), 97 }, 98 )) 99 } else { 100 cmd.ui.Say(T("Route {{.HostName}}.{{.DomainName}} {{.Existence}}", 101 map[string]interface{}{ 102 "HostName": hostName, 103 "DomainName": domainName, 104 "Existence": existence, 105 }, 106 )) 107 } 108 return nil 109 } 110 111 func (cmd *CheckRoute) CheckRoute(hostName, domainName, path string) (bool, error) { 112 orgGUID := cmd.config.OrganizationFields().GUID 113 domain, err := cmd.domainRepo.FindByNameInOrg(domainName, orgGUID) 114 if err != nil { 115 return false, err 116 } 117 118 found, err := cmd.routeRepo.CheckIfExists(hostName, domain, path) 119 if err != nil { 120 return false, err 121 } 122 123 return found, nil 124 }