forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target.go
137 lines (115 loc) · 3.81 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
package commands
import (
"github.com/cloudfoundry/cli/cf/api/organizations"
"github.com/cloudfoundry/cli/cf/api/spaces"
"github.com/cloudfoundry/cli/cf/command_registry"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/errors"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/flags"
"github.com/cloudfoundry/cli/flags/flag"
)
type Target struct {
ui terminal.UI
config core_config.ReadWriter
orgRepo organizations.OrganizationRepository
spaceRepo spaces.SpaceRepository
}
func init() {
command_registry.Register(&Target{})
}
func (cmd *Target) MetaData() command_registry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["o"] = &cliFlags.StringFlag{Name: "o", Usage: T("organization")}
fs["s"] = &cliFlags.StringFlag{Name: "s", Usage: T("space")}
return command_registry.CommandMetadata{
Name: "target",
ShortName: "t",
Description: T("Set or view the targeted org or space"),
Usage: T("CF_NAME target [-o ORG] [-s SPACE]"),
Flags: fs,
}
}
func (cmd *Target) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
if len(fc.Args()) != 0 {
cmd.ui.Failed(T("Incorrect Usage. No argument required\n\n") + command_registry.Commands.CommandUsage("target"))
}
reqs = []requirements.Requirement{
requirementsFactory.NewApiEndpointRequirement(),
}
if fc.IsSet("o") || fc.IsSet("s") {
reqs = append(reqs, requirementsFactory.NewLoginRequirement())
}
return
}
func (cmd *Target) SetDependency(deps command_registry.Dependency, _ bool) command_registry.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) {
orgName := c.String("o")
spaceName := c.String("s")
if orgName != "" {
err := cmd.setOrganization(orgName)
if err != nil {
cmd.ui.Failed(err.Error())
} 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 {
cmd.ui.Failed(err.Error())
}
}
cmd.ui.ShowConfiguration(cmd.config)
if !cmd.config.IsLoggedIn() {
cmd.ui.PanicQuietly()
}
cmd.ui.NotifyUpdateIfNeeded(cmd.config)
return
}
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 errors.NewWithFmt(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 errors.NewWithFmt(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
}