forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target_command.go
204 lines (170 loc) · 5.17 KB
/
target_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
package v7
import (
"fmt"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/command/v7/shared"
"code.cloudfoundry.org/cli/util/configv3"
"code.cloudfoundry.org/clock"
)
//go:generate counterfeiter . TargetActor
type TargetActor interface {
GetOrganizationByName(orgName string) (v7action.Organization, v7action.Warnings, error)
GetOrganizationSpaces(orgGUID string) ([]v7action.Space, v7action.Warnings, error)
GetSpaceByNameAndOrganization(spaceName string, orgGUID string) (v7action.Space, v7action.Warnings, error)
CloudControllerAPIVersion() string
}
type TargetCommand struct {
Organization string `short:"o" description:"Organization"`
Space string `short:"s" description:"Space"`
usage interface{} `usage:"CF_NAME target [-o ORG] [-s SPACE]"`
relatedCommands interface{} `related_commands:"create-org, create-space, login, orgs, spaces"`
UI command.UI
Config command.Config
SharedActor command.SharedActor
Actor TargetActor
}
func (cmd *TargetCommand) Setup(config command.Config, ui command.UI) error {
cmd.Config = config
cmd.UI = ui
cmd.SharedActor = sharedaction.NewActor(config)
ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui, "")
if err != nil {
return err
}
cmd.Actor = v7action.NewActor(ccClient, config, nil, uaaClient, clock.NewClock())
return nil
}
func (cmd *TargetCommand) Execute(args []string) error {
err := command.WarnIfCLIVersionBelowAPIDefinedMinimum(cmd.Config, cmd.Actor.CloudControllerAPIVersion(), cmd.UI)
if err != nil {
return err
}
err = cmd.SharedActor.CheckTarget(false, false)
if err != nil {
return err
}
user, err := cmd.Config.CurrentUser()
if err != nil {
cmd.clearTargets()
return err
}
switch {
case cmd.Organization != "" && cmd.Space != "":
err = cmd.setOrgAndSpace()
if err != nil {
cmd.clearTargets()
return err
}
case cmd.Organization != "":
err = cmd.setOrg()
if err != nil {
cmd.clearTargets()
return err
}
err = cmd.autoTargetSpace(cmd.Config.TargetedOrganization().GUID)
if err != nil {
cmd.clearTargets()
return err
}
case cmd.Space != "":
err = cmd.setSpace()
if err != nil {
cmd.clearTargets()
return err
}
}
cmd.displayTargetTable(user)
if !cmd.Config.HasTargetedOrganization() {
cmd.UI.DisplayText("No org or space targeted, use '{{.CFTargetCommand}}'",
map[string]interface{}{
"CFTargetCommand": fmt.Sprintf("%s target -o ORG -s SPACE", cmd.Config.BinaryName()),
})
return nil
}
if !cmd.Config.HasTargetedSpace() {
cmd.UI.DisplayText("No space targeted, use '{{.CFTargetCommand}}'",
map[string]interface{}{
"CFTargetCommand": fmt.Sprintf("%s target -s SPACE", cmd.Config.BinaryName()),
})
}
return nil
}
func (cmd TargetCommand) clearTargets() {
if cmd.Organization != "" {
cmd.Config.UnsetOrganizationAndSpaceInformation()
} else if cmd.Space != "" {
cmd.Config.UnsetSpaceInformation()
}
}
// setOrgAndSpace sets organization and space
func (cmd *TargetCommand) setOrgAndSpace() error {
err := cmd.setOrg()
if err != nil {
return err
}
err = cmd.setSpace()
if err != nil {
return err
}
return nil
}
// setOrg sets organization
func (cmd *TargetCommand) setOrg() error {
org, warnings, err := cmd.Actor.GetOrganizationByName(cmd.Organization)
cmd.UI.DisplayWarningsV7(warnings)
if err != nil {
return err
}
cmd.Config.SetOrganizationInformation(org.GUID, cmd.Organization)
cmd.Config.UnsetSpaceInformation()
return nil
}
// autoTargetSpace targets the space if there is only one space in the org
// and no space arg was provided.
func (cmd *TargetCommand) autoTargetSpace(orgGUID string) error {
spaces, warnings, err := cmd.Actor.GetOrganizationSpaces(orgGUID)
cmd.UI.DisplayWarningsV7(warnings)
if err != nil {
return err
}
if len(spaces) == 1 {
space := spaces[0]
cmd.Config.V7SetSpaceInformation(space.GUID, space.Name)
}
return nil
}
// setSpace sets space
func (cmd *TargetCommand) setSpace() error {
if !cmd.Config.HasTargetedOrganization() {
return translatableerror.NoOrganizationTargetedError{BinaryName: cmd.Config.BinaryName()}
}
space, warnings, err := cmd.Actor.GetSpaceByNameAndOrganization(cmd.Space, cmd.Config.TargetedOrganization().GUID)
cmd.UI.DisplayWarningsV7(warnings)
if err != nil {
return err
}
cmd.Config.V7SetSpaceInformation(space.GUID, space.Name)
return nil
}
// displayTargetTable neatly displays target information.
func (cmd *TargetCommand) displayTargetTable(user configv3.User) {
table := [][]string{
{cmd.UI.TranslateText("api endpoint:"), cmd.Config.Target()},
{cmd.UI.TranslateText("api version:"), cmd.Actor.CloudControllerAPIVersion()},
{cmd.UI.TranslateText("user:"), user.Name},
}
if cmd.Config.HasTargetedOrganization() {
table = append(table, []string{
cmd.UI.TranslateText("org:"), cmd.Config.TargetedOrganization().Name,
})
}
if cmd.Config.HasTargetedSpace() {
table = append(table, []string{
cmd.UI.TranslateText("space:"), cmd.Config.TargetedSpace().Name,
})
}
cmd.UI.DisplayKeyValueTable("", table, 3)
}