forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v3apps.go
129 lines (107 loc) · 2.99 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
package commands
import (
"fmt"
"strings"
"github.com/cloudfoundry/cli/cf/commandregistry"
"github.com/cloudfoundry/cli/cf/configuration/coreconfig"
"github.com/cloudfoundry/cli/cf/flags"
"github.com/cloudfoundry/cli/cf/formatters"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/cf/v3/models"
"github.com/cloudfoundry/cli/cf/v3/repository"
. "github.com/cloudfoundry/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 {
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
}
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, err := c.repository.GetProcesses(app.Links.Processes.Href)
if err != nil {
return err
}
processes[i] = ps
rs, err := c.repository.GetRoutes(app.Links.Routes.Href)
if err != nil {
return err
}
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])
}
table.Print()
return nil
}
type table interface {
Add(row ...string)
Print()
}
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, ", "),
)
}