forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes_command.go
106 lines (90 loc) · 2.76 KB
/
routes_command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package v7
import (
"strconv"
"strings"
"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/resources"
"code.cloudfoundry.org/cli/util/ui"
)
type RoutesCommand struct {
BaseCommand
usage interface{} `usage:"CF_NAME routes [--org-level]"`
relatedCommands interface{} `related_commands:"check-route, create-route, domains, map-route, unmap-route"`
Orglevel bool `long:"org-level" description:"List all the routes for all spaces of current organization"`
Labels string `long:"labels" description:"Selector to filter routes by labels"`
}
func (cmd RoutesCommand) Execute(args []string) error {
var (
routes []resources.Route
warnings v7action.Warnings
err error
)
err = cmd.SharedActor.CheckTarget(true, true)
if err != nil {
return err
}
currentUser, err := cmd.Config.CurrentUser()
if err != nil {
return err
}
targetedOrg := cmd.Config.TargetedOrganization()
targetedSpace := cmd.Config.TargetedSpace()
if cmd.Orglevel {
cmd.UI.DisplayTextWithFlavor("Getting routes for org {{.CurrentOrg}} as {{.CurrentUser}}...\n", map[string]interface{}{
"CurrentOrg": targetedOrg.Name,
"CurrentUser": currentUser.Name,
})
routes, warnings, err = cmd.Actor.GetRoutesByOrg(targetedOrg.GUID, cmd.Labels)
} else {
cmd.UI.DisplayTextWithFlavor("Getting routes for org {{.CurrentOrg}} / space {{.CurrentSpace}} as {{.CurrentUser}}...\n", map[string]interface{}{
"CurrentOrg": targetedOrg.Name,
"CurrentSpace": targetedSpace.Name,
"CurrentUser": currentUser.Name,
})
routes, warnings, err = cmd.Actor.GetRoutesBySpace(targetedSpace.GUID, cmd.Labels)
}
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
routeSummaries, warnings, err := cmd.Actor.GetRouteSummaries(routes)
cmd.UI.DisplayWarnings(warnings)
if err != nil {
return err
}
if len(routes) > 0 {
cmd.displayRoutesTable(routeSummaries)
} else {
cmd.UI.DisplayText("No routes found.")
}
return nil
}
func (cmd RoutesCommand) displayRoutesTable(routeSummaries []v7action.RouteSummary) {
var routesTable = [][]string{
{
cmd.UI.TranslateText("space"),
cmd.UI.TranslateText("host"),
cmd.UI.TranslateText("domain"),
cmd.UI.TranslateText("port"),
cmd.UI.TranslateText("path"),
cmd.UI.TranslateText("protocol"),
cmd.UI.TranslateText("apps"),
},
}
for _, routeSummary := range routeSummaries {
port := ""
if routeSummary.Port != 0 {
port = strconv.Itoa(routeSummary.Port)
}
routesTable = append(routesTable, []string{
routeSummary.SpaceName,
routeSummary.Host,
routeSummary.DomainName,
port,
routeSummary.Path,
routeSummary.Protocol,
strings.Join(routeSummary.AppNames, ", "),
})
}
cmd.UI.DisplayTableWithHeader("", routesTable, ui.DefaultTableSpacePadding)
}