-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.go
87 lines (78 loc) · 2.45 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
package main
import (
"bufio"
"log"
"os"
"path"
"strings"
"time"
"github.com/codegangsta/cli"
"github.com/mickep76/etcdtool/command"
)
type Config struct {
peers string
}
func readStandardConfig() Config {
var ret Config
dir, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
cfg := path.Join(dir, ".etcdtool.conf")
file, err := os.Open(cfg)
if err != nil {
return ret
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "#") {
continue
}
keyvals := strings.Split(line, " ")
if len(keyvals) != 2 {
log.Fatalf("Bad line in config file %s, line: %s", cfg, line)
}
if keyvals[0] == "peers" {
ret.peers = keyvals[1]
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return ret
}
func main() {
cfg := readStandardConfig()
defaultPeers := "http://127.0.0.1:4001,http://127.0.0.1:2379"
if len(cfg.peers) > 0 {
defaultPeers = cfg.peers
}
app := cli.NewApp()
app.Name = "etcdtool"
app.Version = Version
app.Usage = "Command line tool for etcd to import, export, edit or validate data in either JSON, YAML or TOML format."
app.Flags = []cli.Flag{
cli.StringFlag{Name: "config, c", EnvVar: "ETCDTOOL_CONFIG", Usage: "Configuration file"},
cli.BoolFlag{Name: "debug, d", Usage: "Debug"},
cli.StringFlag{Name: "peers, p", Value: defaultPeers, EnvVar: "ETCDTOOL_PEERS", Usage: "Comma-delimited list of hosts in the cluster"},
cli.StringFlag{Name: "cert", Value: "", EnvVar: "ETCDTOOL_CERT", Usage: "Identify HTTPS client using this SSL certificate file"},
cli.StringFlag{Name: "key", Value: "", EnvVar: "ETCDTOOL_KEY", Usage: "Identify HTTPS client using this SSL key file"},
cli.StringFlag{Name: "ca", Value: "", EnvVar: "ETCDTOOL_CA", Usage: "Verify certificates of HTTPS-enabled servers using this CA bundle"},
cli.StringFlag{Name: "user, u", Value: "", Usage: "User"},
cli.StringFlag{Name: "password-file, F", Value: "", Usage: "File path to the user's password"},
cli.DurationFlag{Name: "timeout, t", Value: time.Second, Usage: "Connection timeout"},
cli.DurationFlag{Name: "command-timeout, T", Value: 5 * time.Second, Usage: "Command timeout"},
}
app.Commands = []cli.Command{
command.NewImportCommand(),
command.NewExportCommand(),
command.NewLsCommand(),
command.NewEditCommand(),
command.NewValidateCommand(),
command.NewTreeCommand(),
command.NewPrintConfigCommand(),
}
app.Run(os.Args)
}