This repository has been archived by the owner on May 25, 2023. It is now read-only.
/
variable.go
202 lines (180 loc) · 6.95 KB
/
variable.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
195
196
197
198
199
200
201
202
/*
Package cmd Copyright 2021 VMware, Inc.
SPDX-License-Identifier: BSD-2-Clause
*/
package cmd
import (
"os"
log "github.com/sirupsen/logrus"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
// getVariableCmd represents the variable command
var getVariableCmd = &cobra.Command{
Use: "variable",
Short: "Get Variables",
Long: `Get Code Stream Variables by name, project or by id - e.g:
# Get Variable by ID
cs-cli get variable --id 6b7936d3-a19d-4298-897a-65e9dc6620c8
# Get Variable by Name
cs-cli get variable --name my-variable
# Get Variable by Project
cs-cli get variable --project production`,
Run: func(cmd *cobra.Command, args []string) {
if err := ensureTargetConnection(); err != nil {
log.Fatalln(err)
}
response, err := getVariable(id, name, project, exportPath)
if err != nil {
log.Fatalln("Unable to get Code Stream Variables: ", err)
}
var resultCount = len(response)
if resultCount == 0 {
// No results
log.Warnln("No results found")
} else if resultCount == 1 {
// Print the single result
if exportPath != "" {
exportVariable(response[0], exportPath)
}
PrettyPrint(response[0])
} else {
// Print result table
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Id", "Name", "Project", "Type", "Description"})
for _, c := range response {
table.Append([]string{c.ID, c.Name, c.Project, c.Type, c.Description})
}
table.Render()
}
},
}
// getVariableCmd represents the variable command
var createVariableCmd = &cobra.Command{
Use: "variable",
Short: "Create a Variable",
Long: `Create a Variable`,
Run: func(cmd *cobra.Command, args []string) {
if err := ensureTargetConnection(); err != nil {
log.Fatalln(err)
}
if importPath != "" { // If we are importing a file
variables := importVariables(importPath)
for _, value := range variables {
if project != "" { // If the project is specified update the object
value.Project = project
}
createResponse, err := createVariable(value.Name, value.Description, value.Type, value.Project, value.Value)
if err != nil {
log.Warnln("Unable to create Code Stream Variable: ", err)
} else {
log.Infoln("Created variable", createResponse.Name, "in", createResponse.Project)
}
}
} else {
createResponse, err := createVariable(name, description, typename, project, value)
if err != nil {
log.Errorln("Unable to create Code Stream Variable: ", err)
} else {
PrettyPrint(createResponse)
}
}
},
}
// updateVariableCmd represents the variable command
var updateVariableCmd = &cobra.Command{
Use: "variable",
Short: "Update a Variable",
Long: `Update a Variable`,
Run: func(cmd *cobra.Command, args []string) {
if err := ensureTargetConnection(); err != nil {
log.Fatalln(err)
}
if importPath != "" { // If we are importing a file
variables := importVariables(importPath)
for _, value := range variables {
exisitingVariable, err := getVariable("", value.Name, value.Project, "")
if err != nil {
log.Errorln("Update failed - unable to find existing Code Stream Variable", value.Name, "in", value.Project)
} else {
_, err := updateVariable(exisitingVariable[0].ID, value.Name, value.Description, value.Type, value.Value)
if err != nil {
log.Errorln("Unable to update Code Stream Variable: ", err)
} else {
log.Infoln("Updated variable", value.Name)
}
}
}
} else { // Else we are updating using flags
updateResponse, err := updateVariable(id, name, description, typename, value)
if err != nil {
log.Errorln("Unable to update Code Stream Variable: ", err)
}
log.Infoln("Updated variable", updateResponse.Name)
}
},
}
// deleteVariableCmd represents the executions command
var deleteVariableCmd = &cobra.Command{
Use: "variable",
Short: "Delete a Variable",
Long: `Delete a Variable
# Delete Variable by ID
cs-cli delete variable --id "variable ID"
# Delete Variable by Name
cs-cli delete variable --name "My Variable"
# Delete Variable by Name and Project
cs-cli delete variable --name "My Variable" --project "My Project"
# Delete all Variables in Project
cs-cli delete variable --project "My Project"
`,
Run: func(cmd *cobra.Command, args []string) {
if err := ensureTargetConnection(); err != nil {
log.Fatalln(err)
}
if id != "" {
response, err := deleteVariable(id)
if err != nil {
log.Errorln("Unable to delete variable: ", err)
} else {
log.Infoln("Variable with id " + response.ID + " deleted")
}
} else if project != "" {
response, err := deleteVariableByProject(project)
if err != nil {
log.Errorln("Delete Variables in "+project+" failed:", err)
} else {
log.Infoln(len(response), "Variables deleted")
}
}
},
}
func init() {
// Get Variable
getCmd.AddCommand(getVariableCmd)
getVariableCmd.Flags().StringVarP(&name, "name", "n", "", "List variable with name")
getVariableCmd.Flags().StringVarP(&project, "project", "p", "", "List variables in project")
getVariableCmd.Flags().StringVarP(&id, "id", "i", "", "List variables by id")
getVariableCmd.Flags().StringVarP(&exportPath, "exportPath", "", "", "Path to export objects - relative or absolute location")
// Create Variable
createCmd.AddCommand(createVariableCmd)
createVariableCmd.Flags().StringVarP(&name, "name", "n", "", "The name of the variable to create")
createVariableCmd.Flags().StringVarP(&typename, "type", "t", "", "The type of the variable to create REGULAR|SECRET|RESTRICTED")
createVariableCmd.Flags().StringVarP(&project, "project", "p", "", "The project in which to create the variable")
createVariableCmd.Flags().StringVarP(&value, "value", "v", "", "The value of the variable to create")
createVariableCmd.Flags().StringVarP(&description, "description", "d", "", "The description of the variable to create")
createVariableCmd.Flags().StringVarP(&importPath, "importpath", "i", "", "Path to a YAML file with the variables to import")
// Update Variable
updateCmd.AddCommand(updateVariableCmd)
updateVariableCmd.Flags().StringVarP(&id, "id", "i", "", "ID of the variable to update")
updateVariableCmd.Flags().StringVarP(&name, "name", "n", "", "Update the name of the variable")
updateVariableCmd.Flags().StringVarP(&typename, "type", "t", "", "Update the type of the variable REGULAR|SECRET|RESTRICTED")
updateVariableCmd.Flags().StringVarP(&value, "value", "v", "", "Update the value of the variable ")
updateVariableCmd.Flags().StringVarP(&description, "description", "d", "", "Update the description of the variable")
updateVariableCmd.Flags().StringVarP(&importPath, "importpath", "", "", "Path to a YAML file with the variables to import")
//updateVariableCmd.MarkFlagRequired("id")
// Delete Variable
deleteCmd.AddCommand(deleteVariableCmd)
deleteVariableCmd.Flags().StringVarP(&id, "id", "i", "", "Delete variable by id")
deleteVariableCmd.Flags().StringVarP(&project, "project", "p", "", "The project in which to delete the variable, or delete all variables in project")
}