-
Notifications
You must be signed in to change notification settings - Fork 11
/
exclude.go
194 lines (164 loc) · 6.04 KB
/
exclude.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
package exclude
import (
"fmt"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd"
sharedVariable "github.com/OctopusDeploy/cli/pkg/cmd/project/variables/shared"
"github.com/OctopusDeploy/cli/pkg/cmd/tenant/shared"
"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/util"
"github.com/OctopusDeploy/cli/pkg/util/flag"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/variables"
"github.com/spf13/cobra"
"strings"
)
const (
FlagProject = "project"
FlagVariableSet = "variable-set"
)
type ExcludeFlags struct {
Project *flag.Flag[string]
VariableSets *flag.Flag[[]string]
}
func NewExcludeVariableSetFlags() *ExcludeFlags {
return &ExcludeFlags{
Project: flag.New[string](FlagProject, false),
VariableSets: flag.New[[]string](FlagVariableSet, false),
}
}
type ExcludeOptions struct {
*ExcludeFlags
*cmd.Dependencies
shared.GetProjectCallback
shared.GetAllProjectsCallback
sharedVariable.GetAllLibraryVariableSetsCallback
}
func NewExcludeOptions(flags *ExcludeFlags, dependencies *cmd.Dependencies) *ExcludeOptions {
return &ExcludeOptions{
ExcludeFlags: flags,
Dependencies: dependencies,
GetProjectCallback: func(identifier string) (*projects.Project, error) {
return shared.GetProject(dependencies.Client, identifier)
},
GetAllProjectsCallback: func() ([]*projects.Project, error) { return shared.GetAllProjects(dependencies.Client) },
GetAllLibraryVariableSetsCallback: func() ([]*variables.LibraryVariableSet, error) {
return sharedVariable.GetAllLibraryVariableSets(dependencies.Client)
},
}
}
func NewExcludeVariableSetCmd(f factory.Factory) *cobra.Command {
createFlags := NewExcludeVariableSetFlags()
cmd := &cobra.Command{
Use: "exclude",
Short: "Exclude a variable set from a project",
Long: "Exclude a variable set from a project in Octopus Deploy",
Example: heredoc.Docf(`
$ %[1]s project variable exclude
$ %[1]s project variable exclude --variable-set "Slack Variables"
`, constants.ExecutableName),
RunE: func(c *cobra.Command, args []string) error {
opts := NewExcludeOptions(createFlags, cmd.NewDependencies(f, c))
return excludeRun(opts)
},
}
flags := cmd.Flags()
flags.StringVarP(&createFlags.Project.Value, createFlags.Project.Name, "p", "", "The project")
flags.StringSliceVarP(&createFlags.VariableSets.Value, createFlags.VariableSets.Name, "", []string{}, "The name of the library variable set")
return cmd
}
func excludeRun(opts *ExcludeOptions) error {
if !opts.NoPrompt {
err := PromptMissing(opts)
if err != nil {
return err
}
}
project, err := opts.GetProjectCallback(opts.Project.Value)
if err != nil {
return err
}
libraryVariableSets, err := opts.GetAllLibraryVariableSetsCallback()
if err != nil {
return err
}
projectModified := false
for _, variableSet := range opts.VariableSets.Value {
targetVariableSet := util.SliceFilter(libraryVariableSets, func(item *variables.LibraryVariableSet) bool { return strings.EqualFold(variableSet, item.Name) })
if util.Empty(targetVariableSet) {
return fmt.Errorf("cannot find library variable set '%s'", variableSet)
}
if len(targetVariableSet) > 1 {
return fmt.Errorf("'%s' matched more than one library variable set", variableSet)
}
if !util.SliceContainsAny(project.IncludedLibraryVariableSets, func(item string) bool { return item == targetVariableSet[0].ID }) {
fmt.Fprintf(opts.Out, output.Yellowf("'%s' is not currently included, skipping\n", targetVariableSet[0].Name))
} else {
project.IncludedLibraryVariableSets = util.SliceFilter(project.IncludedLibraryVariableSets, func(id string) bool { return id != targetVariableSet[0].ID })
projectModified = true
fmt.Fprintf(opts.Out, output.Cyanf("Removing '%s' library variable set\n", targetVariableSet[0].Name))
}
}
if projectModified {
_, err = opts.Client.Projects.Update(project)
if err != nil {
return err
}
fmt.Fprintf(opts.Out, "Successfully updated library variable sets\n")
}
if !opts.NoPrompt {
autoCmd := flag.GenerateAutomationCmd(opts.CmdPath, opts.Project, opts.VariableSets)
fmt.Fprintf(opts.Out, "\nAutomation Command: %s\n", autoCmd)
}
return nil
}
func PromptMissing(opts *ExcludeOptions) error {
var project *projects.Project
var err error
if opts.Project.Value == "" {
project, err = projectSelector("You have not specified a Project. Please select one:", opts.GetAllProjectsCallback, opts.Ask)
if err != nil {
return nil
}
opts.Project.Value = project.GetName()
} else {
project, err = opts.GetProjectCallback(opts.Project.Value)
if err != nil {
return err
}
}
libraryVariableSets, err := opts.GetAllLibraryVariableSetsCallback()
linkedVariableSets := util.SliceFilter(libraryVariableSets, func(item *variables.LibraryVariableSet) bool {
return util.SliceContains(project.IncludedLibraryVariableSets, item.ID)
})
if util.Empty(linkedVariableSets) {
return fmt.Errorf("no library variable sets available to exclude")
}
if util.Empty(opts.VariableSets.Value) {
selectedVariableSets, err := question.MultiSelectMap(
opts.Ask,
"Select the Library Variable Sets to exclude in the project",
linkedVariableSets,
func(item *variables.LibraryVariableSet) string {
return item.Name
}, false)
if err != nil {
return err
}
if util.Empty(selectedVariableSets) {
return fmt.Errorf("no library variable sets selected")
}
opts.VariableSets.Value = util.SliceTransform(selectedVariableSets, func(item *variables.LibraryVariableSet) string { return item.Name })
}
return nil
}
func projectSelector(questionText string, getAllProjectsCallback shared.GetAllProjectsCallback, ask question.Asker) (*projects.Project, error) {
existingProjects, err := getAllProjectsCallback()
if err != nil {
return nil, err
}
return question.SelectMap(ask, questionText, existingProjects, func(p *projects.Project) string { return p.GetName() })
}