-
Notifications
You must be signed in to change notification settings - Fork 21
/
variable.go
205 lines (162 loc) · 5.05 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
203
204
205
package aid
import (
"fmt"
tfe "github.com/hashicorp/go-tfe"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// SetVariableFlags define flags for the cobra command
func SetVariableFlags(cmd *cobra.Command) {
usage := "The variable ID"
cmd.Flags().String("id", "", usage)
usage = "The workspace ID."
cmd.Flags().String("workspace-id", "", usage)
usage = "The name of the variable."
cmd.Flags().String("key", "", usage)
usage = "The value of the variable."
cmd.Flags().String("value", "", usage)
usage = "The description of the variable."
cmd.Flags().String("description", "", usage)
usage = "Whether this is a Terraform or environment variable. Valid values: env, policy-set or terraform. Once defined, cannot be modified."
cmd.Flags().String("category", "", usage)
usage = "Whether to evaluate the value of the variable as a string of HCL code."
cmd.Flags().Bool("hcl", false, usage)
usage = "Whether the value is sensitive."
cmd.Flags().Bool("sensitive", false, usage)
}
// GetVariableCreateOptions return tfe.VariableCreateOptions with correpondent values given by the flags
func GetVariableCreateOptions(cmd *cobra.Command) tfe.VariableCreateOptions {
var options tfe.VariableCreateOptions
if cmd.Flags().Changed("key") {
// The name of the variable.
key, err := cmd.Flags().GetString("key")
if err != nil {
logrus.Fatalf("unable to get flag key\n%v", err)
}
options.Key = &key
}
if cmd.Flags().Changed("value") {
// The value of the variable.
value, err := cmd.Flags().GetString("value")
if err != nil {
logrus.Fatalf("unable to get flag value\n%v", err)
}
options.Value = &value
}
if cmd.Flags().Changed("description") {
// The description of the variable.
description, err := cmd.Flags().GetString("description")
if err != nil {
logrus.Fatalf("unable to get flag description\n%v", err)
}
options.Description = &description
}
if cmd.Flags().Changed("category") {
category, err := cmd.Flags().GetString("category")
if err != nil {
logrus.Fatalf("unable to get flag category\n%v", err)
}
switch category {
case "env":
options.Category = tfe.Category(tfe.CategoryEnv)
case "policy-set":
options.Category = tfe.Category(tfe.CategoryPolicySet)
case "terraform":
options.Category = tfe.Category(tfe.CategoryTerraform)
}
}
if cmd.Flags().Changed("hcl") {
// Whether to evaluate the value of the variable as a string of HCL code.
hcl, err := cmd.Flags().GetBool("hcl")
if err != nil {
logrus.Fatalf("unable to get flag hcl\n%v", err)
}
options.HCL = &hcl
}
if cmd.Flags().Changed("sensitive") {
// Whether the value is sensitive.
sensitive, err := cmd.Flags().GetBool("sensitive")
if err != nil {
logrus.Fatalf("unable to get flag sensitive\n%v", err)
}
options.Sensitive = &sensitive
}
return options
}
// GetVariableUpdateOptions return tfe.VariableUpdateOptions with correpondent values given by the flags
func GetVariableUpdateOptions(cmd *cobra.Command) tfe.VariableUpdateOptions {
var options tfe.VariableUpdateOptions
if cmd.Flags().Changed("id") {
// The name of the variable.
id, err := cmd.Flags().GetString("id")
if err != nil {
logrus.Fatalf("unable to get flag id\n%v", err)
}
options.Key = &id
}
if cmd.Flags().Changed("key") {
// The name of the variable.
key, err := cmd.Flags().GetString("key")
if err != nil {
logrus.Fatalf("unable to get flag key\n%v", err)
}
options.Key = &key
}
if cmd.Flags().Changed("value") {
// The value of the variable.
value, err := cmd.Flags().GetString("value")
if err != nil {
logrus.Fatalf("unable to get flag value\n%v", err)
}
options.Value = &value
}
if cmd.Flags().Changed("description") {
// The description of the variable.
description, err := cmd.Flags().GetString("description")
if err != nil {
logrus.Fatalf("unable to get flag description\n%v", err)
}
options.Description = &description
}
if cmd.Flags().Changed("hcl") {
// Whether to evaluate the value of the variable as a string of HCL code.
hcl, err := cmd.Flags().GetBool("hcl")
if err != nil {
logrus.Fatalf("unable to get flag hcl\n%v", err)
}
options.HCL = &hcl
}
if cmd.Flags().Changed("sensitive") {
// Whether the value is sensitive.
sensitive, err := cmd.Flags().GetBool("sensitive")
if err != nil {
logrus.Fatalf("unable to get flag sensitive\n%v", err)
}
options.Sensitive = &sensitive
}
return options
}
// PrintVariableList convert struct to JSON and displays to user
func PrintVariableList(list *tfe.VariableList) {
if len(list.Items) > 0 {
for i, item := range list.Items {
if i < len(list.Items)-1 {
fmt.Printf("%v,\n", ToJSON(item))
} else {
fmt.Printf("%v\n", ToJSON(item))
}
}
}
}
func FindVariableByKey(list *tfe.VariableList, cmd *cobra.Command) (*tfe.Variable, error) {
key, err := cmd.Flags().GetString("key")
if err != nil {
return &tfe.Variable{}, fmt.Errorf("unable to get flag key")
}
for _, item := range list.Items {
if item.Key == key {
return item, nil
}
}
return &tfe.Variable{}, fmt.Errorf("variable %s not found", key)
}