-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
339 lines (304 loc) · 6.96 KB
/
main.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
)
const (
gitlabURL = "https://gitlab.com/api/v4/projects/%v/variables"
)
type GitlabVar struct {
Key string `json:"key" yaml:"key"`
Value string `json:"value" yaml:"value"`
EnvironmentScope string `json:"environment_scope" yaml:"environment_scope"`
VariableType string `json:"variable_type" yaml:"variable_type"`
Protected bool `json:"protected" yaml:"protected"`
Masked bool `json:"masked" yaml:"masked"`
}
type Varlist = []*GitlabVar
var (
importPath string
exportPath string
gitlabToken string
projectID string
envScope string
)
func main() {
cliApp := getCLI()
err := cliApp.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func getCLI() *cli.App {
app := &cli.App{
Name: "gitlabvar",
Usage: "Export and import your CI variable from gitlab",
Description: "Example: \tgitlabvar --token ABC -project 123 get\n\tgitlabvar -p 123 e -s qa",
EnableBashCompletion: true,
HideHelpCommand: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "import",
Aliases: []string{"i"},
Value: ".gitlab-ci-var.yaml",
Usage: "import yaml file path",
Destination: &importPath,
},
&cli.StringFlag{
Name: "export",
Aliases: []string{"o"},
Value: ".gitlab-ci-var.yaml",
Usage: "export yaml file path",
Destination: &exportPath,
},
&cli.StringFlag{
Name: "token",
Aliases: []string{"t"},
Usage: "gitlab token. scope required: api. get it from here: https://gitlab.com/-/profile/personal_access_tokens. Token can also be loaded from env $GITLAB_TOKEN",
Destination: &gitlabToken,
},
&cli.StringFlag{
Name: "project",
Aliases: []string{"p"},
Usage: "Project ID, get it from frontpage of the project",
Destination: &projectID,
},
},
Commands: []*cli.Command{
{
Name: "apply",
Aliases: []string{"a"},
Usage: "apply variable yaml to gitlab project",
Action: func(c *cli.Context) error {
err := verifyArg()
if err != nil {
log.Fatal(err)
}
err = applyYaml()
if err != nil {
log.Fatal(err)
}
return nil
},
},
{
Name: "get",
Aliases: []string{"g"},
Usage: "get variable yaml from gitlab project",
Action: func(c *cli.Context) error {
err := verifyArg()
if err != nil {
log.Fatal(err)
}
err = getYaml()
if err != nil {
log.Fatal(err)
}
return nil
},
},
{
Name: "env",
Aliases: []string{"e"},
Usage: "export .env file, use --scope or -s to specify EnvironmentScope",
Flags: []cli.Flag{&cli.StringFlag{
Name: "scope",
Aliases: []string{"s"},
Usage: "EnvironmentScope you prefer. Default use *",
Destination: &envScope,
}},
Action: func(c *cli.Context) error {
err := verifyArg()
if err != nil {
log.Fatal(err)
}
err = getDotEnv()
if err != nil {
log.Fatal(err)
}
return nil
},
},
{
Name: "init",
Aliases: []string{"i"},
Usage: "get a sample yaml file",
Action: func(c *cli.Context) error {
err := sampleYaml()
if err != nil {
log.Fatal(err)
}
return nil
},
},
},
}
return app
}
func verifyArg() error {
if projectID == "" {
return errors.New("projectID is required")
}
if gitlabToken == "" {
gitlabToken = os.Getenv("GITLAB_TOKEN")
}
if gitlabToken == "" {
return errors.New("token is required")
}
return nil
}
func getDotEnv() error {
varlist, err := getVars()
if err != nil {
return err
}
var buf string
for _, e := range *varlist {
if (e.EnvironmentScope == envScope || e.EnvironmentScope == "*") && e.VariableType == "env_var" {
buf += strings.Replace(e.Key, "K8S_SECRET_", "", 1) + "=\"" + e.Value + "\"\n"
}
}
err = ioutil.WriteFile(".env", []byte(buf), 0644)
if err != nil {
return err
}
fmt.Println("exported to .env")
return nil
}
func getYaml() error {
varlist, err := getVars()
if err != nil {
return err
}
d, err := yaml.Marshal(varlist)
if err != nil {
return err
}
err = ioutil.WriteFile(exportPath, d, 0644)
if err != nil {
return err
}
fmt.Println("Saved to ", exportPath)
return nil
}
func applyYaml() error {
d, err := ioutil.ReadFile(importPath)
if err != nil {
return err
}
var newVarlist *Varlist
err = yaml.Unmarshal(d, &newVarlist)
if err != nil {
return err
}
oldVarlist, err := getVars()
if err != nil {
return err
}
updateL, createL, deleteL := syncList(newVarlist, oldVarlist)
if len(*updateL) == 0 && len(*createL) == 0 && len(*deleteL) == 0 {
fmt.Println("Nothing to update")
return nil
}
fmt.Println("Are you sure you want to apply the following changes:")
if len(*updateL) != 0 {
fmt.Println("Updates:")
printList(updateL)
}
if len(*createL) != 0 {
fmt.Println("Creates:")
printList(createL)
}
if len(*deleteL) != 0 {
fmt.Println("Deletes:")
printList(deleteL)
}
fmt.Println("(y)es or any other key to cancel")
reader := bufio.NewReader(os.Stdin)
char, _, err := reader.ReadRune()
switch char {
case 'y':
fmt.Println("Applying...")
break
default:
fmt.Println("Canceled")
return nil
}
for _, e := range *updateL {
if err = updateVars(e); err != nil {
return err
}
}
for _, e := range *createL {
if err = createVars(e); err != nil {
return err
}
}
for _, e := range *deleteL {
if err = deleteVars(e); err != nil {
return err
}
}
fmt.Println("Done.")
return nil
}
func sampleYaml() error {
sample := `- key: ENV_NAME
value: ENV_VALUE
environment_scope: '*'
variable_type: env_var
protected: false
masked: false
`
err := ioutil.WriteFile(exportPath, []byte(sample), 0644)
if err != nil {
return err
}
return nil
}
func syncList(newL *Varlist, oldL *Varlist) (*Varlist, *Varlist, *Varlist) {
var (
m = make(map[string]*GitlabVar) // hashmap we are going to use. key: unique identifier, value: entire object
updateL = &Varlist{}
createL = &Varlist{}
deleteL = &Varlist{}
)
for _, e := range *oldL {
m[e.Key+e.EnvironmentScope] = e
}
for _, e := range *newL {
key := e.Key + e.EnvironmentScope
if m[key] != nil {
if !deepEq(m[key], e) {
*updateL = append(*updateL, e)
}
m[key] = nil
} else {
*createL = append(*createL, e)
}
}
for _, v := range m {
if v != nil {
*deleteL = append(*deleteL, v)
}
}
return updateL, createL, deleteL
}
func deepEq(a *GitlabVar, b *GitlabVar) bool {
if a.EnvironmentScope == b.EnvironmentScope && a.Key == b.Key && a.Value == b.Value && a.VariableType == b.VariableType && a.Masked == b.Masked && a.Protected == b.Protected {
return true
}
return false
}
func printList(l *Varlist) {
for _, e := range *l {
fmt.Println(e.Key, e.EnvironmentScope, "->", e.Value)
}
}