forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target.go
147 lines (124 loc) · 3.87 KB
/
target.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
package commands
import (
"fmt"
"code.cloudfoundry.org/cli/cf/api/organizations"
"code.cloudfoundry.org/cli/cf/api/spaces"
"code.cloudfoundry.org/cli/cf/commandregistry"
"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
"code.cloudfoundry.org/cli/cf/errors"
"code.cloudfoundry.org/cli/cf/flags"
. "code.cloudfoundry.org/cli/cf/i18n"
"code.cloudfoundry.org/cli/cf/models"
"code.cloudfoundry.org/cli/cf/requirements"
"code.cloudfoundry.org/cli/cf/terminal"
)
type Target struct {
ui terminal.UI
config coreconfig.ReadWriter
orgRepo organizations.OrganizationRepository
spaceRepo spaces.SpaceRepository
}
func init() {
commandregistry.Register(&Target{})
}
func (cmd *Target) MetaData() commandregistry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["o"] = &flags.StringFlag{ShortName: "o", Usage: T("Organization")}
fs["s"] = &flags.StringFlag{ShortName: "s", Usage: T("Space")}
return commandregistry.CommandMetadata{
Name: "target",
ShortName: "t",
Description: T("Set or view the targeted org or space"),
Usage: []string{
T("CF_NAME target [-o ORG] [-s SPACE]"),
},
Flags: fs,
}
}
func (cmd *Target) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
usageReq := requirements.NewUsageRequirement(commandregistry.CLICommandUsagePresenter(cmd),
T("No argument required"),
func() bool {
return len(fc.Args()) != 0
},
)
reqs := []requirements.Requirement{
usageReq,
requirementsFactory.NewAPIEndpointRequirement(),
}
if fc.IsSet("o") || fc.IsSet("s") {
reqs = append(reqs, requirementsFactory.NewLoginRequirement())
}
return reqs, nil
}
func (cmd *Target) SetDependency(deps commandregistry.Dependency, _ bool) commandregistry.Command {
cmd.ui = deps.UI
cmd.config = deps.Config
cmd.orgRepo = deps.RepoLocator.GetOrganizationRepository()
cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository()
return cmd
}
func (cmd *Target) Execute(c flags.FlagContext) error {
orgName := c.String("o")
spaceName := c.String("s")
if orgName != "" {
err := cmd.setOrganization(orgName)
if err != nil {
return err
} else if spaceName == "" {
spaceList, apiErr := cmd.getSpaceList()
if apiErr == nil && len(spaceList) == 1 {
cmd.setSpace(spaceList[0].Name)
}
}
}
if spaceName != "" {
err := cmd.setSpace(spaceName)
if err != nil {
return err
}
}
err := cmd.ui.ShowConfiguration(cmd.config)
if err != nil {
return err
}
cmd.ui.NotifyUpdateIfNeeded(cmd.config)
if !cmd.config.IsLoggedIn() {
return fmt.Errorf("") // Done on purpose, do not redo this in refactor code
}
return nil
}
func (cmd Target) setOrganization(orgName string) error {
// setting an org necessarily invalidates any space you had previously targeted
cmd.config.SetOrganizationFields(models.OrganizationFields{})
cmd.config.SetSpaceFields(models.SpaceFields{})
org, apiErr := cmd.orgRepo.FindByName(orgName)
if apiErr != nil {
return fmt.Errorf(T("Could not target org.\n{{.APIErr}}",
map[string]interface{}{"APIErr": apiErr.Error()}))
}
cmd.config.SetOrganizationFields(org.OrganizationFields)
return nil
}
func (cmd Target) setSpace(spaceName string) error {
cmd.config.SetSpaceFields(models.SpaceFields{})
if !cmd.config.HasOrganization() {
return errors.New(T("An org must be targeted before targeting a space"))
}
space, apiErr := cmd.spaceRepo.FindByName(spaceName)
if apiErr != nil {
return fmt.Errorf(T("Unable to access space {{.SpaceName}}.\n{{.APIErr}}",
map[string]interface{}{"SpaceName": spaceName, "APIErr": apiErr.Error()}))
}
cmd.config.SetSpaceFields(space.SpaceFields)
return nil
}
func (cmd Target) getSpaceList() ([]models.Space, error) {
spaceList := []models.Space{}
apiErr := cmd.spaceRepo.ListSpaces(
func(space models.Space) bool {
spaceList = append(spaceList, space)
return true
})
return spaceList, apiErr
}