forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
labels_command.go
273 lines (228 loc) · 8.64 KB
/
labels_command.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package v7
import (
"fmt"
"sort"
"strings"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/command/v7/shared"
"code.cloudfoundry.org/cli/types"
"code.cloudfoundry.org/cli/util/ui"
"code.cloudfoundry.org/clock"
)
type ResourceType string
const (
App ResourceType = "app"
Buildpack ResourceType = "buildpack"
Domain ResourceType = "domain"
Org ResourceType = "org"
Route ResourceType = "route"
Space ResourceType = "space"
Stack ResourceType = "stack"
)
//go:generate counterfeiter . LabelsActor
type LabelsActor interface {
GetApplicationLabels(appName string, spaceGUID string) (map[string]types.NullString, v7action.Warnings, error)
GetDomainLabels(domainName string) (map[string]types.NullString, v7action.Warnings, error)
GetOrganizationLabels(orgName string) (map[string]types.NullString, v7action.Warnings, error)
GetRouteLabels(routeName string, spaceGUID string) (map[string]types.NullString, v7action.Warnings, error)
GetSpaceLabels(spaceName string, orgGUID string) (map[string]types.NullString, v7action.Warnings, error)
GetBuildpackLabels(buildpackName string, buildpackStack string) (map[string]types.NullString, v7action.Warnings, error)
GetStackLabels(stackName string) (map[string]types.NullString, v7action.Warnings, error)
}
type LabelsCommand struct {
RequiredArgs flag.LabelsArgs `positional-args:"yes"`
BuildpackStack string `long:"stack" short:"s" description:"Specify stack to disambiguate buildpacks with the same name"`
usage interface{} `usage:"CF_NAME labels RESOURCE RESOURCE_NAME\n\nEXAMPLES:\n cf labels app dora\n cf labels org business\n cf labels buildpack go_buildpack --stack cflinuxfs3 \n\nRESOURCES:\n app\n buildpack\n domain\n org\n route\n space\n stack"`
relatedCommands interface{} `related_commands:"set-label, unset-label"`
UI command.UI
Config command.Config
SharedActor command.SharedActor
Actor LabelsActor
}
func (cmd *LabelsCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
cmd.SharedActor = sharedaction.NewActor(config)
ccClient, _, err := shared.GetNewClientsAndConnectToCF(config, ui, "")
if err != nil {
return err
}
cmd.Actor = v7action.NewActor(ccClient, config, nil, nil, clock.NewClock())
return nil
}
func (cmd LabelsCommand) Execute(args []string) error {
var (
labels map[string]types.NullString
warnings v7action.Warnings
)
username, err := cmd.Config.CurrentUserName()
if err != nil {
return err
}
err = cmd.validateFlags()
if err != nil {
return err
}
switch cmd.canonicalResourceTypeForName() {
case App:
labels, warnings, err = cmd.fetchAppLabels(username)
case Buildpack:
labels, warnings, err = cmd.fetchBuildpackLabels(username)
case Domain:
labels, warnings, err = cmd.fetchDomainLabels(username)
case Org:
labels, warnings, err = cmd.fetchOrgLabels(username)
case Route:
labels, warnings, err = cmd.fetchRouteLabels(username)
case Space:
labels, warnings, err = cmd.fetchSpaceLabels(username)
case Stack:
labels, warnings, err = cmd.fetchStackLabels(username)
default:
err = fmt.Errorf("Unsupported resource type of '%s'", cmd.RequiredArgs.ResourceType)
}
cmd.UI.DisplayWarningsV7(warnings)
if err != nil {
return err
}
cmd.printLabels(labels)
return nil
}
func (cmd LabelsCommand) fetchAppLabels(username string) (map[string]types.NullString, v7action.Warnings, error) {
err := cmd.SharedActor.CheckTarget(true, true)
if err != nil {
return nil, nil, err
}
cmd.UI.DisplayTextWithFlavor("Getting labels for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
"AppName": cmd.RequiredArgs.ResourceName,
"OrgName": cmd.Config.TargetedOrganization().Name,
"SpaceName": cmd.Config.TargetedSpace().Name,
"Username": username,
})
cmd.UI.DisplayNewline()
return cmd.Actor.GetApplicationLabels(cmd.RequiredArgs.ResourceName, cmd.Config.TargetedSpace().GUID)
}
func (cmd LabelsCommand) fetchDomainLabels(username string) (map[string]types.NullString, v7action.Warnings, error) {
err := cmd.SharedActor.CheckTarget(false, false)
if err != nil {
return nil, nil, err
}
cmd.UI.DisplayTextWithFlavor("Getting labels for domain {{.DomainName}} as {{.Username}}...", map[string]interface{}{
"DomainName": cmd.RequiredArgs.ResourceName,
"Username": username,
})
cmd.UI.DisplayNewline()
return cmd.Actor.GetDomainLabels(cmd.RequiredArgs.ResourceName)
}
func (cmd LabelsCommand) fetchOrgLabels(username string) (map[string]types.NullString, v7action.Warnings, error) {
err := cmd.SharedActor.CheckTarget(false, false)
if err != nil {
return nil, nil, err
}
cmd.UI.DisplayTextWithFlavor("Getting labels for org {{.OrgName}} as {{.Username}}...", map[string]interface{}{
"OrgName": cmd.RequiredArgs.ResourceName,
"Username": username,
})
cmd.UI.DisplayNewline()
return cmd.Actor.GetOrganizationLabels(cmd.RequiredArgs.ResourceName)
}
func (cmd LabelsCommand) fetchRouteLabels(username string) (map[string]types.NullString, v7action.Warnings, error) {
err := cmd.SharedActor.CheckTarget(true, true)
if err != nil {
return nil, nil, err
}
cmd.UI.DisplayTextWithFlavor("Getting labels for route {{.RouteName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.Username}}...", map[string]interface{}{
"RouteName": cmd.RequiredArgs.ResourceName,
"OrgName": cmd.Config.TargetedOrganization().Name,
"SpaceName": cmd.Config.TargetedSpace().Name,
"Username": username,
})
cmd.UI.DisplayNewline()
return cmd.Actor.GetRouteLabels(cmd.RequiredArgs.ResourceName, cmd.Config.TargetedSpace().GUID)
}
func (cmd LabelsCommand) fetchSpaceLabels(username string) (map[string]types.NullString, v7action.Warnings, error) {
err := cmd.SharedActor.CheckTarget(true, false)
if err != nil {
return nil, nil, err
}
cmd.UI.DisplayTextWithFlavor("Getting labels for space {{.SpaceName}} in org {{.OrgName}} as {{.Username}}...", map[string]interface{}{
"SpaceName": cmd.RequiredArgs.ResourceName,
"OrgName": cmd.Config.TargetedOrganization().Name,
"Username": username,
})
cmd.UI.DisplayNewline()
return cmd.Actor.GetSpaceLabels(cmd.RequiredArgs.ResourceName, cmd.Config.TargetedOrganization().GUID)
}
func (cmd LabelsCommand) fetchStackLabels(username string) (map[string]types.NullString, v7action.Warnings, error) {
err := cmd.SharedActor.CheckTarget(false, false)
if err != nil {
return nil, nil, err
}
cmd.UI.DisplayTextWithFlavor("Getting labels for stack {{.StackName}} as {{.Username}}...", map[string]interface{}{
"StackName": cmd.RequiredArgs.ResourceName,
"Username": username,
})
cmd.UI.DisplayNewline()
return cmd.Actor.GetStackLabels(cmd.RequiredArgs.ResourceName)
}
func (cmd LabelsCommand) fetchBuildpackLabels(username string) (map[string]types.NullString, v7action.Warnings, error) {
err := cmd.SharedActor.CheckTarget(false, false)
if err != nil {
return nil, nil, err
}
var template string
if cmd.BuildpackStack != "" {
template = "Getting labels for %s {{.ResourceName}} with stack {{.StackName}} as {{.User}}..."
} else {
template = "Getting labels for %s {{.ResourceName}} as {{.User}}..."
}
preFlavoringText := fmt.Sprintf(template, cmd.RequiredArgs.ResourceType)
cmd.UI.DisplayTextWithFlavor(
preFlavoringText,
map[string]interface{}{
"ResourceName": cmd.RequiredArgs.ResourceName,
"StackName": cmd.BuildpackStack,
"User": username,
},
)
cmd.UI.DisplayNewline()
return cmd.Actor.GetBuildpackLabels(cmd.RequiredArgs.ResourceName, cmd.BuildpackStack)
}
func (cmd LabelsCommand) canonicalResourceTypeForName() ResourceType {
return ResourceType(strings.ToLower(cmd.RequiredArgs.ResourceType))
}
func (cmd LabelsCommand) printLabels(labels map[string]types.NullString) {
if len(labels) == 0 {
cmd.UI.DisplayText("No labels found.")
return
}
keys := make([]string, 0, len(labels))
for key := range labels {
keys = append(keys, key)
}
sort.Strings(keys)
table := [][]string{
{
cmd.UI.TranslateText("key"),
cmd.UI.TranslateText("value"),
},
}
for _, key := range keys {
table = append(table, []string{key, labels[key].Value})
}
cmd.UI.DisplayTableWithHeader("", table, ui.DefaultTableSpacePadding)
}
func (cmd LabelsCommand) validateFlags() error {
if cmd.BuildpackStack != "" && cmd.canonicalResourceTypeForName() != Buildpack {
return translatableerror.ArgumentCombinationError{
Args: []string{
cmd.RequiredArgs.ResourceType, "--stack, -s",
},
}
}
return nil
}