-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig.go
More file actions
92 lines (79 loc) · 1.85 KB
/
config.go
File metadata and controls
92 lines (79 loc) · 1.85 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
package config
import (
"fmt"
"strings"
docopt "github.com/docopt/docopt-go"
)
// Config runs the config subcommand
func Config() error {
usage := `Deis Cluster Configuration
Usage:
deisctl config <target> get [<key>...] [options]
deisctl config <target> set <key=val>... [options]
Options:
--verbose print out the request bodies [default: false]
`
// parse command-line arguments
args, err := docopt.Parse(usage, nil, true, "", true)
if err != nil {
return err
}
err = setConfigFlags(args)
if err != nil {
return err
}
return doConfig(args)
}
// Flags for config package
var Flags struct {
}
func setConfigFlags(args map[string]interface{}) error {
return nil
}
func doConfig(args map[string]interface{}) error {
client, err := getEtcdClient()
if err != nil {
return err
}
rootPath := "/deis/" + args["<target>"].(string) + "/"
var vals []string
if args["set"] == true {
vals, err = doConfigSet(client, rootPath, args["<key=val>"].([]string))
} else {
vals, err = doConfigGet(client, rootPath, args["<key>"].([]string))
}
if err != nil {
return err
}
// print results
for _, v := range vals {
fmt.Printf("%v\n", v)
}
return nil
}
func doConfigSet(client *etcdClient, root string, kvs []string) ([]string, error) {
var result []string
for _, kv := range kvs {
split := strings.Split(kv, "=")
if len(split) != 2 {
return result, fmt.Errorf("invalid argument: %v", kv)
}
val, err := client.Set(root+split[0], split[1])
if err != nil {
return result, err
}
result = append(result, val)
}
return result, nil
}
func doConfigGet(client *etcdClient, root string, keys []string) ([]string, error) {
var result []string
for _, k := range keys {
val, err := client.Get(root + k)
if err != nil {
return result, err
}
result = append(result, val)
}
return result, nil
}