forked from let-us-go/zkcli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
70 lines (61 loc) · 1.66 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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/c-bata/go-prompt"
"github.com/let-us-go/zkcli/core"
homedir "github.com/mitchellh/go-homedir"
"github.com/namsral/flag"
)
var gitCommit = "unknown"
var built = "unknown"
const version = "0.5.0"
func main() {
servers := flag.String("s", "127.0.0.1:2181", "Servers")
username := flag.String("u", "", "Username")
password := flag.String("p", "", "Password")
showVersion := flag.Bool("version", false, "Show version info")
verboseLog := flag.Bool("v", false, "Set to true if want to enable zk log, usefull for diagnose zk problems")
homePath, _ := homedir.Dir()
defaultConf := filepath.Join(homePath, ".config/zkcli.conf")
if _, err := os.Stat(defaultConf); err != nil {
defaultConf = ""
}
flag.String(flag.DefaultConfigFlagname, defaultConf, "path to config file")
flag.Parse()
args := flag.Args()
if *showVersion {
fmt.Printf("Version:\t%s\nGit commit:\t%s\nBuilt: %s\n",
version, gitCommit, built)
os.Exit(0)
}
config := core.NewConfig(strings.Split(*servers, ","), !*verboseLog)
if *username != "" && *password != "" {
auth := core.NewAuth(
"digest", fmt.Sprintf("%s:%s", *username, *password),
)
config.Auth = auth
}
conn, err := config.Connect()
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
defer conn.Close()
name, options := core.ParseCmd(strings.Join(args, " "))
cmd := core.NewCmd(name, options, conn, config)
if len(args) > 0 {
cmd.ExitWhenErr = true
cmd.Run()
return
}
p := prompt.New(
core.GetExecutor(cmd),
core.GetCompleter(cmd),
prompt.OptionTitle("zkcli: A interactive Zookeeper client"),
prompt.OptionPrefix(">>> "),
)
p.Run()
}