/
routes.go
105 lines (87 loc) · 3.1 KB
/
routes.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
package route
import (
"strings"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/flags"
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/command_registry"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
)
type ListRoutes struct {
ui terminal.UI
routeRepo api.RouteRepository
config core_config.Reader
}
func init() {
command_registry.Register(&ListRoutes{})
}
func (cmd *ListRoutes) MetaData() command_registry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["orglevel"] = &flags.BoolFlag{Name: "orglevel", Usage: T("List all the routes for all spaces of current organization")}
return command_registry.CommandMetadata{
Name: "routes",
ShortName: "r",
Description: T("List all routes in the current space or the current organization"),
Usage: "CF_NAME routes [--orglevel]",
Flags: fs,
}
}
func (cmd *ListRoutes) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
if len(fc.Args()) != 0 {
cmd.ui.Failed(T("Incorrect Usage. No argument required\n\n") + command_registry.Commands.CommandUsage("routes"))
}
return []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
requirementsFactory.NewTargetedSpaceRequirement(),
}, nil
}
func (cmd *ListRoutes) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
cmd.ui = deps.Ui
cmd.config = deps.Config
cmd.routeRepo = deps.RepoLocator.GetRouteRepository()
return cmd
}
func (cmd *ListRoutes) Execute(c flags.FlagContext) {
flag := c.Bool("orglevel")
if flag {
cmd.ui.Say(T("Getting routes for org {{.OrgName}} as {{.Username}} ...\n",
map[string]interface{}{
"Username": terminal.EntityNameColor(cmd.config.Username()),
"OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
}))
} else {
cmd.ui.Say(T("Getting routes for org {{.OrgName}} / space {{.SpaceName}} as {{.Username}} ...\n",
map[string]interface{}{
"Username": terminal.EntityNameColor(cmd.config.Username()),
"OrgName": terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
"SpaceName": terminal.EntityNameColor(cmd.config.SpaceFields().Name),
}))
}
table := cmd.ui.Table([]string{T("space"), T("host"), T("domain"), T("path"), T("apps"), T("service")})
var routesFound bool
cb := func(route models.Route) bool {
routesFound = true
appNames := []string{}
for _, app := range route.Apps {
appNames = append(appNames, app.Name)
}
table.Add(route.Space.Name, route.Host, route.Domain.Name, route.Path, strings.Join(appNames, ","), route.ServiceInstance.Name)
return true
}
var err error
if flag {
err = cmd.routeRepo.ListAllRoutes(cb)
} else {
err = cmd.routeRepo.ListRoutes(cb)
}
table.Print()
if err != nil {
cmd.ui.Failed(T("Failed fetching routes.\n{{.Err}}", map[string]interface{}{"Err": err.Error()}))
}
if !routesFound {
cmd.ui.Say(T("No routes found"))
}
}