forked from pkallberg/cx
/
env-vars-set.go
80 lines (65 loc) · 1.53 KB
/
env-vars-set.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
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/cloud66/cloud66"
"github.com/cloud66/cli"
)
func runEnvVarsSet(c *cli.Context) {
if len(c.Args()) != 1 {
cli.ShowSubcommandHelp(c)
os.Exit(2)
}
kv := c.Args()[0]
kvs := strings.Split(kv, "=")
if len(kvs) != 2 {
cli.ShowSubcommandHelp(c)
os.Exit(2)
}
key := strings.ToUpper(kvs[0])
value := kvs[1]
stack := mustStack(c)
envVars, err := client.StackEnvVars(stack.Uid)
must(err)
existing := false
for _, i := range envVars {
if i.Key == key {
if i.Readonly == true {
printFatal("The selected environment variable is readonly")
} else {
existing = true
}
}
}
fmt.Println("Please wait while your environment variable setting is applied...")
asyncId, err := startEnvVarSet(stack.Uid, key, value, existing)
if err != nil {
printFatal(err.Error())
}
genericRes, err := endEnvVarSet(*asyncId, stack.Uid)
if err != nil {
printFatal(err.Error())
}
printGenericResponse(*genericRes)
return
}
func startEnvVarSet(stackUid string, key string, value string, existing bool) (*int, error) {
var (
asyncRes *cloud66.AsyncResult
err error
)
if existing {
asyncRes, err = client.StackEnvVarSet(stackUid, key, value)
} else {
asyncRes, err = client.StackEnvVarNew(stackUid, key, value)
}
if err != nil {
return nil, err
}
return &asyncRes.Id, err
}
func endEnvVarSet(asyncId int, stackUid string) (*cloud66.GenericResponse, error) {
return client.WaitStackAsyncAction(asyncId, stackUid, 3*time.Second, 20*time.Minute, true)
}