-
Notifications
You must be signed in to change notification settings - Fork 718
/
main.go
144 lines (133 loc) · 3.23 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
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
// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/signal"
"strings"
"syscall"
"github.com/chzyer/readline"
"github.com/mattn/go-shellwords"
"github.com/pingcap/pd/server"
"github.com/pingcap/pd/tools/pd-ctl/pdctl"
flag "github.com/spf13/pflag"
)
var (
url string
detach bool
version bool
help bool
caPath string
certPath string
keyPath string
)
func init() {
flag.StringVarP(&url, "pd", "u", "http://127.0.0.1:2379", "The pd address.")
flag.BoolVarP(&detach, "detach", "d", false, "Run pdctl without readline.")
flag.BoolVarP(&version, "version", "V", false, "Print version information and exit.")
flag.StringVar(&caPath, "cacert", "", "The path of file that contains list of trusted SSL CAs.")
flag.StringVar(&certPath, "cert", "", "The path of file that contains X509 certificate in PEM format.")
flag.StringVar(&keyPath, "key", "", "The path of file that contains X509 key in PEM format.")
flag.BoolVarP(&help, "help", "h", false, "Help message.")
}
func main() {
pdAddr := os.Getenv("PD_ADDR")
if pdAddr != "" {
os.Args = append(os.Args, "-u", pdAddr)
}
flag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true
flag.Parse()
if help {
flag.Usage()
os.Exit(0)
}
if version {
server.PrintPDInfo()
os.Exit(0)
}
sc := make(chan os.Signal, 1)
signal.Notify(sc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
sig := <-sc
fmt.Printf("\nGot signal [%v] to exit.\n", sig)
switch sig {
case syscall.SIGTERM:
os.Exit(0)
default:
os.Exit(1)
}
}()
var input []string
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
detach = true
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Println(err)
return
}
input = strings.Split(strings.TrimSpace(string(b[:])), " ")
}
if detach {
pdctl.Start(append(os.Args[1:], input...))
return
}
loop()
}
func loop() {
l, err := readline.NewEx(&readline.Config{
Prompt: "\033[31m»\033[0m ",
HistoryFile: "/tmp/readline.tmp",
InterruptPrompt: "^C",
EOFPrompt: "^D",
HistorySearchFold: true,
})
if err != nil {
panic(err)
}
defer l.Close()
for {
line, err := l.Readline()
if err != nil {
if err == readline.ErrInterrupt {
break
} else if err == io.EOF {
break
}
continue
}
if line == "exit" {
os.Exit(0)
}
args, err := shellwords.Parse(line)
if err != nil {
fmt.Printf("parse command err: %v\n", err)
continue
}
if url != "" && !strings.HasPrefix(url, "http") {
url = "http://" + url
}
args = append(args, "-u", url)
if caPath != "" && certPath != "" && keyPath != "" {
args = append(args, "--cacert", caPath, "--cert", certPath, "--key", keyPath)
}
pdctl.Start(args)
}
}