github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v7/routes_command.go (about) 1 package v7 2 3 import ( 4 "strings" 5 6 "code.cloudfoundry.org/cli/actor/sharedaction" 7 "code.cloudfoundry.org/cli/actor/v7action" 8 "code.cloudfoundry.org/cli/command" 9 "code.cloudfoundry.org/cli/command/v7/shared" 10 "code.cloudfoundry.org/cli/util/ui" 11 "code.cloudfoundry.org/clock" 12 ) 13 14 //go:generate counterfeiter . RoutesActor 15 16 type RoutesActor interface { 17 GetRoutesBySpace(spaceGUID string, labels string) ([]v7action.Route, v7action.Warnings, error) 18 GetRoutesByOrg(orgGUID string, labels string) ([]v7action.Route, v7action.Warnings, error) 19 GetRouteSummaries([]v7action.Route) ([]v7action.RouteSummary, v7action.Warnings, error) 20 } 21 22 type RoutesCommand struct { 23 usage interface{} `usage:"CF_NAME routes [--orglevel]"` 24 relatedCommands interface{} `related_commands:"check-route, domains, map-route, unmap-route"` 25 Orglevel bool `long:"orglevel" description:"List all the routes for all spaces of current organization"` 26 Labels string `long:"labels" description:"Selector to filter routes by labels"` 27 28 UI command.UI 29 Config command.Config 30 SharedActor command.SharedActor 31 Actor RoutesActor 32 } 33 34 func (cmd *RoutesCommand) Setup(config command.Config, ui command.UI) error { 35 cmd.UI = ui 36 cmd.Config = config 37 cmd.SharedActor = sharedaction.NewActor(config) 38 39 ccClient, _, err := shared.GetNewClientsAndConnectToCF(config, ui, "") 40 if err != nil { 41 return err 42 } 43 cmd.Actor = v7action.NewActor(ccClient, config, nil, nil, clock.NewClock()) 44 45 return nil 46 } 47 48 func (cmd RoutesCommand) Execute(args []string) error { 49 var ( 50 routes []v7action.Route 51 warnings v7action.Warnings 52 err error 53 ) 54 55 err = cmd.SharedActor.CheckTarget(true, true) 56 if err != nil { 57 return err 58 } 59 60 currentUser, err := cmd.Config.CurrentUser() 61 if err != nil { 62 return err 63 } 64 65 targetedOrg := cmd.Config.TargetedOrganization() 66 targetedSpace := cmd.Config.TargetedSpace() 67 68 if cmd.Orglevel { 69 cmd.UI.DisplayTextWithFlavor("Getting routes for org {{.CurrentOrg}} as {{.CurrentUser}}...\n", map[string]interface{}{ 70 "CurrentOrg": targetedOrg.Name, 71 "CurrentUser": currentUser.Name, 72 }) 73 routes, warnings, err = cmd.Actor.GetRoutesByOrg(targetedOrg.GUID, cmd.Labels) 74 } else { 75 cmd.UI.DisplayTextWithFlavor("Getting routes for org {{.CurrentOrg}} / space {{.CurrentSpace}} as {{.CurrentUser}}...\n", map[string]interface{}{ 76 "CurrentOrg": targetedOrg.Name, 77 "CurrentSpace": targetedSpace.Name, 78 "CurrentUser": currentUser.Name, 79 }) 80 routes, warnings, err = cmd.Actor.GetRoutesBySpace(targetedSpace.GUID, cmd.Labels) 81 } 82 83 cmd.UI.DisplayWarnings(warnings) 84 if err != nil { 85 return err 86 } 87 88 routeSummaries, warnings, err := cmd.Actor.GetRouteSummaries(routes) 89 cmd.UI.DisplayWarnings(warnings) 90 if err != nil { 91 return err 92 } 93 94 if len(routes) > 0 { 95 cmd.displayRoutesTable(routeSummaries) 96 } else { 97 cmd.UI.DisplayText("No routes found.") 98 } 99 100 return nil 101 } 102 103 func (cmd RoutesCommand) displayRoutesTable(routeSummaries []v7action.RouteSummary) { 104 var routesTable = [][]string{ 105 { 106 cmd.UI.TranslateText("space"), 107 cmd.UI.TranslateText("host"), 108 cmd.UI.TranslateText("domain"), 109 cmd.UI.TranslateText("path"), 110 cmd.UI.TranslateText("apps"), 111 }, 112 } 113 114 for _, routeSummary := range routeSummaries { 115 routesTable = append(routesTable, []string{ 116 routeSummary.SpaceName, 117 routeSummary.Host, 118 routeSummary.DomainName, 119 routeSummary.Path, 120 strings.Join(routeSummary.AppNames, ", "), 121 }) 122 } 123 124 cmd.UI.DisplayTableWithHeader("", routesTable, ui.DefaultTableSpacePadding) 125 }