-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathview.go
More file actions
302 lines (265 loc) · 9.06 KB
/
view.go
File metadata and controls
302 lines (265 loc) · 9.06 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package view
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/apiclient"
variableShared "github.com/OctopusDeploy/cli/pkg/cmd/project/variables/shared"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/output"
"github.com/OctopusDeploy/cli/pkg/util"
"github.com/OctopusDeploy/cli/pkg/util/flag"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/variables"
"github.com/spf13/cobra"
)
const (
FlagProject = "project"
FlagWeb = "web"
FlagId = "id"
)
type ViewFlags struct {
Id *flag.Flag[string]
Project *flag.Flag[string]
Web *flag.Flag[bool]
}
type ViewOptions struct {
Client *client.Client
Host string
name string
*ViewFlags
cmd *cobra.Command
}
type VarScopedItem struct {
Id string `json:"id"`
Name string `json:"name"`
}
type VarProcessScopedItem struct {
VarScopedItem
ProcessType string `json:"processtype"`
}
type VariableViewData struct {
Id string `json:"id"`
Value string `json:"value"`
Description string `json:"description"`
EnvironmentScope []*VarScopedItem `json:"environmentscope"`
RoleScope []*VarScopedItem `json:"rolescope"`
MachineScope []*VarScopedItem `json:"machinescope"`
ProcessScope []*VarProcessScopedItem `json:"processscope"`
StepScope []*VarScopedItem `json:"stepscope"`
ChannelScope []*VarScopedItem `json:"channelscope"`
Prompted bool `json:"prompted,omitempty"`
PromptLabel string `json:"promptlabel,omitempty"`
PromptLabelDescription string `json:"promptlabeldescription,omitempty"`
PromptRequired string `json:"promptrequired,omitempty"`
}
func NewViewFlags() *ViewFlags {
return &ViewFlags{
Project: flag.New[string](FlagProject, false),
Id: flag.New[string](FlagId, false),
Web: flag.New[bool](FlagWeb, false),
}
}
func NewCmdView(f factory.Factory) *cobra.Command {
viewFlags := NewViewFlags()
cmd := &cobra.Command{
Use: "view",
Short: "View all values of a project variable",
Long: "View all values of a project variable in Octopus Deploy",
Example: heredoc.Docf(`
$ %[1]s project variable view
$ %[1]s project variable view DatabaseName --project "Vet Clinic"
`, constants.ExecutableName),
Aliases: []string{"ls"},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("must supply variable name")
}
client, err := f.GetSpacedClient(apiclient.NewRequester(cmd))
if err != nil {
return err
}
opts := &ViewOptions{
client,
f.GetCurrentHost(),
args[0],
viewFlags,
cmd,
}
return viewRun(opts)
},
}
flags := cmd.Flags()
flags.StringVarP(&viewFlags.Project.Value, viewFlags.Project.Name, "p", "", "The project containing the variable")
flags.StringVar(&viewFlags.Id.Value, viewFlags.Id.Name, "", "The Id of the specifically scoped variable")
flags.BoolVarP(&viewFlags.Web.Value, viewFlags.Web.Name, "w", false, "Open in web browser")
return cmd
}
func viewRun(opts *ViewOptions) error {
project, err := opts.Client.Projects.GetByIdentifier(opts.Project.Value)
if err != nil {
return err
}
allVars, err := opts.Client.Variables.GetAll(project.GetID())
if err != nil {
return err
}
filteredVars := util.SliceFilter(
allVars.Variables,
func(variable *variables.Variable) bool {
if opts.Id.Value != "" {
return strings.EqualFold(variable.Name, opts.name) && strings.EqualFold(variable.ID, opts.Id.Value)
}
return strings.EqualFold(variable.Name, opts.name)
})
if !util.Any(filteredVars) {
return fmt.Errorf("cannot find variable '%s'", opts.name)
}
outputFormat, err := opts.cmd.Flags().GetString(constants.FlagOutputFormat)
if err != nil { // should never happen, but fallback if it does
outputFormat = constants.OutputFormatTable
}
out := opts.cmd.OutOrStdout()
switch strings.ToLower(outputFormat) {
case constants.OutputFormatBasic, constants.OutputFormatTable:
fmt.Fprintln(out, output.Bold(filteredVars[0].Name))
for _, v := range filteredVars {
data := []*output.DataRow{}
if outputFormat == constants.OutputFormatTable {
data = append(data, output.NewDataRow(output.Bold("KEY"), output.Bold("VALUE")))
}
data = append(data, output.NewDataRow("Id", output.Dim(v.GetID())))
if v.IsSensitive {
data = append(data, output.NewDataRow("Value", output.Bold("*** (sensitive)")))
} else {
data = append(data, output.NewDataRow("Value", output.Bold(v.Value)))
}
if v.Description == "" {
v.Description = constants.NoDescription
}
data = append(data, output.NewDataRow("Description", output.Dim(v.Description)))
scopeValues, err := variableShared.ToScopeValues(v, allVars.ScopeValues)
if err != nil {
return err
}
data = addScope(scopeValues.Environments, "Environment scope", data, nil)
data = addScope(scopeValues.Roles, "Role scope", data, nil)
data = addScope(scopeValues.Channels, "Channel scope", data, nil)
data = addScope(scopeValues.Machines, "Machine scope", data, nil)
data = addScope(scopeValues.TenantTags, "Tenant tag scope", data, func(item *resources.ReferenceDataItem) string {
return item.ID
})
data = addScope(scopeValues.Actions, "Step scope", data, nil)
data = addScope(
util.SliceTransform(scopeValues.Processes, func(item *resources.ProcessReferenceDataItem) *resources.ReferenceDataItem {
return &resources.ReferenceDataItem{
ID: item.ID,
Name: item.Name,
}
}),
"Process scope",
data,
nil)
if v.Prompt != nil {
data = append(data, output.NewDataRow("Prompted", "true"))
data = append(data, output.NewDataRow("Prompt Label", v.Prompt.Label))
data = append(data, output.NewDataRow("Prompt Description", output.Dim(v.Prompt.Description)))
data = append(data, output.NewDataRow("Prompt Required", strconv.FormatBool(v.Prompt.IsRequired)))
}
fmt.Fprintln(out)
output.PrintRows(data, out)
}
case constants.OutputFormatJson:
viewItems := make([]VariableViewData, 0, len(filteredVars))
for _, v := range filteredVars {
scopeValues, err := variableShared.ToScopeValues(v, allVars.ScopeValues)
if err != nil {
return err
}
vd := VariableViewData{}
vd.Id = v.ID
if v.IsSensitive {
vd.Value = "*** (sensitive)"
} else {
vd.Value = v.Value
}
vd.Description = v.Description
if util.Any(scopeValues.Environments) {
vd.EnvironmentScope = util.SliceTransform(scopeValues.Environments, func(e *resources.ReferenceDataItem) *VarScopedItem {
return &VarScopedItem{
Id: e.ID,
Name: e.Name,
}
})
}
if util.Any(scopeValues.Roles) {
vd.RoleScope = util.SliceTransform(scopeValues.Roles, func(e *resources.ReferenceDataItem) *VarScopedItem {
return &VarScopedItem{
Id: e.ID,
Name: e.Name,
}
})
}
if util.Any(scopeValues.Machines) {
vd.MachineScope = util.SliceTransform(scopeValues.Machines, func(e *resources.ReferenceDataItem) *VarScopedItem {
return &VarScopedItem{
Id: e.ID,
Name: e.Name,
}
})
}
if util.Any(scopeValues.Processes) {
vd.ProcessScope = util.SliceTransform(scopeValues.Processes, func(e *resources.ProcessReferenceDataItem) *VarProcessScopedItem {
return &VarProcessScopedItem{
VarScopedItem: VarScopedItem{
Id: e.ID,
Name: e.Name,
},
ProcessType: e.ProcessType,
}
})
}
if util.Any(scopeValues.Actions) {
vd.StepScope = util.SliceTransform(scopeValues.Actions, func(e *resources.ReferenceDataItem) *VarScopedItem {
return &VarScopedItem{
Id: e.ID,
Name: e.Name,
}
})
}
if util.Any(scopeValues.Channels) {
vd.ChannelScope = util.SliceTransform(scopeValues.Channels, func(e *resources.ReferenceDataItem) *VarScopedItem {
return &VarScopedItem{
Id: e.ID,
Name: e.Name,
}
})
}
if v.Prompt != nil {
vd.Prompted = true
vd.PromptLabel = v.Prompt.Label
vd.PromptLabelDescription = v.Prompt.Description
vd.PromptRequired = strconv.FormatBool(v.Prompt.IsRequired)
} else {
vd.Prompted = false
}
viewItems = append(viewItems, vd)
}
data, _ := json.MarshalIndent(viewItems, "", " ")
opts.cmd.Println(string(data))
}
return nil
}
func addScope(values []*resources.ReferenceDataItem, scopeDescription string, data []*output.DataRow, displaySelector func(item *resources.ReferenceDataItem) string) []*output.DataRow {
if displaySelector == nil {
displaySelector = func(item *resources.ReferenceDataItem) string { return item.Name }
}
if util.Any(values) {
data = append(data, output.NewDataRow(scopeDescription, output.FormatAsList(util.SliceTransform(values, displaySelector))))
}
return data
}