-
Notifications
You must be signed in to change notification settings - Fork 928
/
v3apps.go
132 lines (110 loc) · 3.05 KB
/
v3apps.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
package commands
import (
"fmt"
"strings"
"code.cloudfoundry.org/cli/cf/commandregistry"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/flags"
"code.cloudfoundry.org/cli/cf/formatters"
"code.cloudfoundry.org/cli/cf/requirements"
"code.cloudfoundry.org/cli/cf/terminal"
"code.cloudfoundry.org/cli/cf/v3/models"
"code.cloudfoundry.org/cli/cf/v3/repository"
. "code.cloudfoundry.org/cli/cf/i18n"
)
type V3Apps struct {
ui terminal.UI
config coreconfig.ReadWriter
repository repository.Repository
}
func init() {
commandregistry.Register(&V3Apps{})
}
func (c *V3Apps) MetaData() commandregistry.CommandMetadata {
return commandregistry.CommandMetadata{
Name: "v3apps",
Description: T("List all apps in the target space"),
Usage: []string{
"CF_NAME v3apps",
},
Hidden: true,
}
}
func (c *V3Apps) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(c),
T("No argument required"),
func() bool {
return len(fc.Args()) != 0
},
)
reqs := []requirements.Requirement{
usageReq,
requirementsFactory.NewLoginRequirement(),
requirementsFactory.NewTargetedSpaceRequirement(),
}
return reqs, nil
}
func (c *V3Apps) SetDependency(deps commandregistry.Dependency, _ bool) commandregistry.Command {
c.ui = deps.UI
c.config = deps.Config
c.repository = deps.RepoLocator.GetV3Repository()
return c
}
func (c *V3Apps) Execute(fc flags.FlagContext) error {
applications, err := c.repository.GetApplications()
if err != nil {
return err
}
processes := make([][]models.V3Process, len(applications))
routes := make([][]models.V3Route, len(applications))
for i, app := range applications {
ps, apiErr := c.repository.GetProcesses(app.Links.Processes.Href)
if apiErr != nil {
return apiErr
}
processes[i] = ps
rs, apiErr := c.repository.GetRoutes(app.Links.Routes.Href)
if apiErr != nil {
return apiErr
}
routes[i] = rs
}
table := c.ui.Table([]string{T("name"), T("requested state"), T("instances"), T("memory"), T("disk"), T("urls")})
for i := range applications {
c.addRow(table, applications[i], processes[i], routes[i])
}
err = table.Print()
if err != nil {
return err
}
return nil
}
type table interface {
Add(row ...string)
Print() error
}
func (c *V3Apps) addRow(
table table,
application models.V3Application,
processes []models.V3Process,
routes []models.V3Route,
) {
var webProcess models.V3Process
for i := range processes {
if processes[i].Type == "web" {
webProcess = processes[i]
}
}
var appRoutes []string
for _, route := range routes {
appRoutes = append(appRoutes, route.Host+route.Path)
}
table.Add(
application.Name,
strings.ToLower(application.DesiredState),
fmt.Sprintf("%d", application.TotalDesiredInstances),
formatters.ByteSize(webProcess.MemoryInMB*formatters.MEGABYTE),
formatters.ByteSize(webProcess.DiskInMB*formatters.MEGABYTE),
strings.Join(appRoutes, ", "),
)
}