/
v3apps.go
117 lines (97 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
package commands
import (
"fmt"
"strings"
"github.com/cloudfoundry/cli/cf/command_registry"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"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/flags"
. "github.com/cloudfoundry/cli/cf/i18n"
)
type V3Apps struct {
ui terminal.UI
config core_config.ReadWriter
repository repository.Repository
}
func init() {
command_registry.Register(&V3Apps{})
}
func (c *V3Apps) MetaData() command_registry.CommandMetadata {
return command_registry.CommandMetadata{
Name: "v3apps",
Description: T("List all apps in the target space"),
Usage: "CF_NAME v3apps",
Hidden: true,
}
}
func (c *V3Apps) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
if len(fc.Args()) != 0 {
c.ui.Failed(T("Incorrect Usage. No argument required\n\n{{.Usage}}", map[string]interface{}{
"Usage": command_registry.Commands.CommandUsage("v3apps"),
}))
}
return []requirements.Requirement{
requirementsFactory.NewLoginRequirement(),
requirementsFactory.NewTargetedSpaceRequirement(),
}, nil
}
func (c *V3Apps) SetDependency(deps command_registry.Dependency, _ bool) command_registry.Command {
c.ui = deps.Ui
c.config = deps.Config
c.repository = deps.RepoLocator.GetV3Repository()
return c
}
func (c *V3Apps) Execute(fc flags.FlagContext) {
applications, err := c.repository.GetApplications()
if err != nil {
c.ui.Failed(err.Error())
}
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 {
c.ui.Failed(err.Error())
}
processes[i] = ps
rs, err := c.repository.GetRoutes(app.Links.Routes.Href)
if err != nil {
c.ui.Failed(err.Error())
}
routes[i] = rs
}
table := terminal.NewTable(c.ui, []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()
}
func (c *V3Apps) addRow(
table terminal.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, ", "),
)
}