github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/route/routes.go (about) 1 package route 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 8 "code.cloudfoundry.org/cli/cf/flags" 9 . "code.cloudfoundry.org/cli/cf/i18n" 10 11 "code.cloudfoundry.org/cli/cf/api" 12 "code.cloudfoundry.org/cli/cf/commandregistry" 13 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 14 "code.cloudfoundry.org/cli/cf/models" 15 "code.cloudfoundry.org/cli/cf/requirements" 16 "code.cloudfoundry.org/cli/cf/terminal" 17 ) 18 19 type ListRoutes struct { 20 ui terminal.UI 21 routeRepo api.RouteRepository 22 domainRepo api.DomainRepository 23 config coreconfig.Reader 24 } 25 26 func init() { 27 commandregistry.Register(&ListRoutes{}) 28 } 29 30 func (cmd *ListRoutes) MetaData() commandregistry.CommandMetadata { 31 fs := make(map[string]flags.FlagSet) 32 fs["orglevel"] = &flags.BoolFlag{Name: "orglevel", Usage: T("List all the routes for all spaces of current organization")} 33 34 return commandregistry.CommandMetadata{ 35 Name: "routes", 36 ShortName: "r", 37 Description: T("List all routes in the current space or the current organization"), 38 Usage: []string{ 39 "CF_NAME routes [--orglevel]", 40 }, 41 Flags: fs, 42 } 43 } 44 45 func (cmd *ListRoutes) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 46 usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd), 47 T("No argument required"), 48 func() bool { 49 return len(fc.Args()) != 0 50 }, 51 ) 52 53 reqs := []requirements.Requirement{ 54 usageReq, 55 requirementsFactory.NewLoginRequirement(), 56 requirementsFactory.NewTargetedSpaceRequirement(), 57 } 58 59 return reqs, nil 60 } 61 62 func (cmd *ListRoutes) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 63 cmd.ui = deps.UI 64 cmd.config = deps.Config 65 cmd.routeRepo = deps.RepoLocator.GetRouteRepository() 66 cmd.domainRepo = deps.RepoLocator.GetDomainRepository() 67 return cmd 68 } 69 70 func (cmd *ListRoutes) Execute(c flags.FlagContext) error { 71 orglevel := c.Bool("orglevel") 72 73 if orglevel { 74 cmd.ui.Say(T("Getting routes for org {{.OrgName}} as {{.Username}} ...\n", 75 map[string]interface{}{ 76 "Username": terminal.EntityNameColor(cmd.config.Username()), 77 "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), 78 })) 79 } else { 80 cmd.ui.Say(T("Getting routes for org {{.OrgName}} / space {{.SpaceName}} as {{.Username}} ...\n", 81 map[string]interface{}{ 82 "Username": terminal.EntityNameColor(cmd.config.Username()), 83 "OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name), 84 "SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name), 85 })) 86 } 87 88 table := cmd.ui.Table([]string{T("space"), T("host"), T("domain"), T("port"), T("path"), T("type"), T("apps"), T("service")}) 89 90 d := make(map[string]models.DomainFields) 91 err := cmd.domainRepo.ListDomainsForOrg(cmd.config.OrganizationFields().GUID, func(domain models.DomainFields) bool { 92 d[domain.GUID] = domain 93 return true 94 }) 95 if err != nil { 96 return errors.New(T("Failed fetching domains for organization {{.OrgName}}.\n{{.Err}}", 97 map[string]interface{}{ 98 "Err": err.Error(), 99 "OrgName": cmd.config.OrganizationFields().Name, 100 }, 101 )) 102 } 103 104 var routesFound bool 105 cb := func(route models.Route) bool { 106 routesFound = true 107 appNames := []string{} 108 for _, app := range route.Apps { 109 appNames = append(appNames, app.Name) 110 } 111 112 var port string 113 if route.Port != 0 { 114 port = fmt.Sprintf("%d", route.Port) 115 } 116 117 domain := d[route.Domain.GUID] 118 119 table.Add( 120 route.Space.Name, 121 route.Host, 122 route.Domain.Name, 123 port, 124 route.Path, 125 domain.RouterGroupType, 126 strings.Join(appNames, ","), 127 route.ServiceInstance.Name, 128 ) 129 return true 130 } 131 132 if orglevel { 133 err = cmd.routeRepo.ListAllRoutes(cb) 134 } else { 135 err = cmd.routeRepo.ListRoutes(cb) 136 } 137 if err != nil { 138 return errors.New(T("Failed fetching routes.\n{{.Err}}", map[string]interface{}{"Err": err.Error()})) 139 } 140 141 err = table.Print() 142 if err != nil { 143 return err 144 } 145 146 if !routesFound { 147 cmd.ui.Say(T("No routes found")) 148 } 149 return nil 150 }