-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimport.go
69 lines (55 loc) · 1.31 KB
/
import.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
package jsonconsul
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
)
type JsonImport struct {
// Prefix to load the config under. If empty then loads to the
// root kv node.
Prefix string
// File containing the Json to be converted to KVs.
Filename string
FlattenedKVs map[string]interface{}
}
func (ji *JsonImport) ParseFlags(args []string) {
flags := flag.NewFlagSet(Name, flag.ContinueOnError)
flags.StringVar(&ji.Prefix, "prefix", "", "What prefix should the Key Values be stored under.")
flags.Parse(args)
leftovers := flags.Args()
if len(leftovers) == 0 {
fmt.Println("Must pass a file to import")
os.Exit(-1)
} else {
ji.Filename = leftovers[0]
}
}
func (ji *JsonImport) readFile() (unmarshalled map[string]interface{}, err error) {
var (
fileOutput []byte
)
fileOutput, err = ioutil.ReadFile(ji.Filename)
if err != nil {
return nil, err
}
// Import the json file
if err = json.Unmarshal(fileOutput, &unmarshalled); err != nil {
return nil, err
}
return
}
func (ji *JsonImport) Run() error {
ji.FlattenedKVs = make(map[string]interface{})
unmarshalled, err := ji.readFile()
if err != nil {
return err
}
interfaceToConsulFlattenedMap(unmarshalled, "", ji.FlattenedKVs)
err = setConsulKVs(ji.Prefix, ji.FlattenedKVs)
if err != nil {
return err
}
return nil
}