-
Notifications
You must be signed in to change notification settings - Fork 928
/
routes.go
150 lines (128 loc) · 3.93 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package route
import (
"errors"
"fmt"
"strings"
"code.cloudfoundry.org/cli/cf/flags"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/api"
"code.cloudfoundry.org/cli/cf/commandregistry"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/requirements"
"code.cloudfoundry.org/cli/cf/terminal"
)
type ListRoutes struct {
ui terminal.UI
routeRepo api.RouteRepository
domainRepo api.DomainRepository
config coreconfig.Reader
}
func init() {
commandregistry.Register(&ListRoutes{})
}
func (cmd *ListRoutes) MetaData() commandregistry.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 commandregistry.CommandMetadata{
Name: "routes",
ShortName: "r",
Description: T("List all routes in the current space or the current organization"),
Usage: []string{
"CF_NAME routes [--orglevel]",
},
Flags: fs,
}
}
func (cmd *ListRoutes) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd),
T("No argument required"),
func() bool {
return len(fc.Args()) != 0
},
)
reqs := []requirements.Requirement{
usageReq,
requirementsFactory.NewLoginRequirement(),
requirementsFactory.NewTargetedSpaceRequirement(),
}
return reqs, nil
}
func (cmd *ListRoutes) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
cmd.ui = deps.UI
cmd.config = deps.Config
cmd.routeRepo = deps.RepoLocator.GetRouteRepository()
cmd.domainRepo = deps.RepoLocator.GetDomainRepository()
return cmd
}
func (cmd *ListRoutes) Execute(c flags.FlagContext) error {
orglevel := c.Bool("orglevel")
if orglevel {
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("port"), T("path"), T("type"), T("apps"), T("service")})
d := make(map[string]models.DomainFields)
err := cmd.domainRepo.ListDomainsForOrg(cmd.config.OrganizationFields().GUID, func(domain models.DomainFields) bool {
d[domain.GUID] = domain
return true
})
if err != nil {
return errors.New(T("Failed fetching domains for organization {{.OrgName}}.\n{{.Err}}",
map[string]interface{}{
"Err": err.Error(),
"OrgName": cmd.config.OrganizationFields().Name,
},
))
}
var routesFound bool
cb := func(route models.Route) bool {
routesFound = true
appNames := []string{}
for _, app := range route.Apps {
appNames = append(appNames, app.Name)
}
var port string
if route.Port != 0 {
port = fmt.Sprintf("%d", route.Port)
}
domain := d[route.Domain.GUID]
table.Add(
route.Space.Name,
route.Host,
route.Domain.Name,
port,
route.Path,
domain.RouterGroupType,
strings.Join(appNames, ","),
route.ServiceInstance.Name,
)
return true
}
if orglevel {
err = cmd.routeRepo.ListAllRoutes(cb)
} else {
err = cmd.routeRepo.ListRoutes(cb)
}
if err != nil {
return errors.New(T("Failed fetching routes.\n{{.Err}}", map[string]interface{}{"Err": err.Error()}))
}
err = table.Print()
if err != nil {
return err
}
if !routesFound {
cmd.ui.Say(T("No routes found"))
}
return nil
}