This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
forked from ake-persson/etcdtool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
import_command.go
174 lines (151 loc) · 3.75 KB
/
import_command.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
package command
import (
"bufio"
"fmt"
"os"
"reflect"
"strings"
"github.com/codegangsta/cli"
"github.com/coreos/etcd/client"
"github.com/mickep76/etcdmap"
"github.com/mickep76/iodatafmt"
"golang.org/x/net/context"
)
// NewImportCommand sets data from input.
func NewImportCommand() cli.Command {
return cli.Command{
Name: "import",
Usage: "import a directory",
Flags: []cli.Flag{
cli.BoolFlag{Name: "yes, y", Usage: "Answer yes to any questions"},
cli.BoolFlag{Name: "replace, r", Usage: "Replace data"},
cli.BoolFlag{Name: "validate, v", EnvVar: "ETCDTOOL_VALIDATE", Usage: "Validate data before import"},
cli.StringFlag{Name: "format, f", Value: "JSON", EnvVar: "ETCDTOOL_FORMAT", Usage: "Data serialization format YAML, TOML or JSON"},
},
Action: func(c *cli.Context) error {
importCommandFunc(c)
return nil
},
}
}
func dirExists(dir string, c *cli.Context, ki client.KeysAPI) (bool, error) {
ctx, cancel := contextWithCommandTimeout(c)
_, err := ki.Get(ctx, dir, &client.GetOptions{})
cancel()
if err != nil {
if cerr, ok := err.(client.Error); ok && cerr.Code == 100 {
return false, nil
}
return false, err
}
return true, nil
}
func isDir(dir string, c *cli.Context, ki client.KeysAPI) (bool, error) {
ctx, cancel := contextWithCommandTimeout(c)
resp, err := ki.Get(ctx, dir, &client.GetOptions{})
cancel()
if err != nil {
return false, err
}
if resp.Node.Dir {
return false, nil
}
return true, nil
}
func askYesNo(msg string) bool {
stdin := bufio.NewReader(os.Stdin)
for {
fmt.Printf("%s [yes/no]? ", msg)
inp, _, err := stdin.ReadLine()
if err != nil {
fatal(err.Error())
}
switch strings.ToLower(string(inp)) {
case "yes":
return true
case "no":
return false
default:
fmt.Printf("Incorrect input: %s\n ", inp)
}
}
}
// importCommandFunc imports data as either JSON, YAML or TOML.
func importCommandFunc(c *cli.Context) {
if len(c.Args()) == 0 {
fatal("You need to specify directory")
}
dir := c.Args()[0]
// Remove trailing slash.
if dir != "/" {
dir = strings.TrimRight(dir, "/")
}
infof("Using dir: %s", dir)
if len(c.Args()) == 1 {
fatal("You need to specify input file")
}
input := c.Args()[1]
// Get data format.
f, err := iodatafmt.Format(c.String("format"))
if err != nil {
fatal(err.Error())
}
// Load configuration file.
e := loadConfig(c)
// New dir API.
ki := newKeyAPI(e)
importFunc(dir, input, f, c.Bool("replace"), c.Bool("yes"), e, c, ki)
}
func importFunc(dir string, file string, f iodatafmt.DataFmt, replace bool, yes bool, e Etcdtool, c *cli.Context, ki client.KeysAPI) {
// Check if dir exists and is a directory.
exists, err := dirExists(dir, c, ki)
if err != nil {
fatalf("Specified dir doesn't exist: %s", dir)
}
if exists {
exist, err := isDir(dir, c, ki)
if err != nil {
fatal(err.Error())
}
if exist {
fatalf("Specified dir is not a directory: %s", dir)
}
}
// Load file.
m, err := iodatafmt.Load(file, f)
if err != nil {
fatal(err.Error())
}
// Validate data.
if c.Bool("validate") {
validateFunc(e, dir, m)
}
if exists {
if replace {
if !yes {
if !askYesNo(fmt.Sprintf("Do you want to overwrite data in directory: %s", dir)) {
os.Exit(1)
}
}
// Delete dir.
if _, err = ki.Delete(context.TODO(), dir, &client.DeleteOptions{Recursive: true}); err != nil {
fatal(err.Error())
}
} else {
if !yes {
if !askYesNo(fmt.Sprintf("Do you want to overwrite data in directory: %s", dir)) {
os.Exit(1)
}
}
}
} else {
// Create dir.
if _, err := ki.Set(context.TODO(), dir, "", &client.SetOptions{Dir: true}); err != nil {
fatal(err.Error())
}
}
// Import data.
if err = etcdmap.Create(ki, dir, reflect.ValueOf(m)); err != nil {
fatal(err.Error())
}
}