-
Notifications
You must be signed in to change notification settings - Fork 10
/
create.go
127 lines (108 loc) · 3.54 KB
/
create.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
package create
import (
"fmt"
"io"
"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/output"
"github.com/OctopusDeploy/cli/pkg/question"
"github.com/OctopusDeploy/cli/pkg/validation"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/spaces"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/teams"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/users"
"github.com/spf13/cobra"
)
func NewCmdCreate(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates a space in an instance of Octopus Deploy",
Long: "Creates a space in an instance of Octopus Deploy.",
Example: fmt.Sprintf(heredoc.Doc(`
$ %s space create"
`), constants.ExecutableName),
RunE: func(cmd *cobra.Command, args []string) error {
return createRun(f, cmd.OutOrStdout())
},
}
return cmd
}
func createRun(f factory.Factory, _ io.Writer) error {
systemClient, err := f.GetSystemClient()
if err != nil {
return err
}
existingSpaces, err := systemClient.Spaces.GetAll()
if err != nil {
return err
}
spaceNames := []string{}
for _, existingSpace := range existingSpaces {
spaceNames = append(spaceNames, existingSpace.Name)
}
var name string
err = f.Ask(&survey.Input{
Help: "The name of the space being created.",
Message: "Name",
}, &name, survey.WithValidator(survey.ComposeValidators(
survey.MaxLength(20),
survey.MinLength(1),
survey.Required,
validation.NotEquals(spaceNames, "a space with this name already exists"),
)))
if err != nil {
return err
}
space := spaces.NewSpace(name)
teams, err := selectTeams(f.Ask, systemClient, existingSpaces, "Select one or more teams to manage this space:")
if err != nil {
return err
}
for _, team := range teams {
space.SpaceManagersTeams = append(space.SpaceManagersTeams, team.ID)
}
users, err := selectUsers(f.Ask, systemClient, "Select one or more users to manage this space:")
if err != nil {
return err
}
for _, user := range users {
space.SpaceManagersTeamMembers = append(space.SpaceManagersTeams, user.ID)
}
createdSpace, err := systemClient.Spaces.Add(space)
if err != nil {
return err
}
fmt.Printf("%s The space, \"%s\" %s was created successfully.\n", output.Green("✔"), createdSpace.Name, output.Dimf("(%s)", createdSpace.ID))
return nil
}
func selectTeams(ask question.Asker, client *client.Client, existingSpaces []*spaces.Space, message string) ([]*teams.Team, error) {
systemTeams, err := client.Teams.Get(teams.TeamsQuery{
IncludeSystem: true,
})
if err != nil {
return []*teams.Team{}, err
}
return question.MultiSelectMap(ask, message, systemTeams.Items, func(team *teams.Team) string {
if len(team.SpaceID) == 0 {
return fmt.Sprintf("%s %s", team.Name, output.Dim("(System Team)"))
}
for _, existingSpace := range existingSpaces {
if team.SpaceID == existingSpace.ID {
return fmt.Sprintf("%s %s", team.Name, output.Dimf("(%s)", existingSpace.Name))
}
}
return ""
})
}
func selectUsers(ask question.Asker, client *client.Client, message string) ([]*users.User, error) {
selectedUsers := []*users.User{}
existingUsers, err := client.Users.GetAll()
if err != nil {
return selectedUsers, err
}
return question.MultiSelectMap(ask, message, existingUsers, func(existingUser *users.User) string {
return fmt.Sprintf("%s %s", existingUser.DisplayName, output.Dimf("(%s)", existingUser.Username))
})
}