-
Notifications
You must be signed in to change notification settings - Fork 712
/
ctl.go
192 lines (165 loc) · 4.75 KB
/
ctl.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// 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 pdctl
import (
"fmt"
"io"
"os"
"github.com/chzyer/readline"
"github.com/mattn/go-shellwords"
"github.com/pingcap/pd/v4/server"
"github.com/pingcap/pd/v4/tools/pd-ctl/pdctl/command"
"github.com/spf13/cobra"
)
// CommandFlags are flags that used in all Commands
type CommandFlags struct {
URL string
CAPath string
CertPath string
KeyPath string
Help bool
}
var (
commandFlags = CommandFlags{}
detach bool
interact bool
version bool
)
func init() {
cobra.EnablePrefixMatching = true
}
func pdctlRun(cmd *cobra.Command, args []string) {
if version {
server.PrintPDInfo()
return
}
if interact {
loop()
}
}
func getBasicCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "pd-ctl",
Short: "Placement Driver control",
}
rootCmd.PersistentFlags().StringVarP(&commandFlags.URL, "pd", "u", "http://127.0.0.1:2379", "address of pd")
rootCmd.PersistentFlags().StringVar(&commandFlags.CAPath, "cacert", "", "path of file that contains list of trusted SSL CAs")
rootCmd.PersistentFlags().StringVar(&commandFlags.CertPath, "cert", "", "path of file that contains X509 certificate in PEM format")
rootCmd.PersistentFlags().StringVar(&commandFlags.KeyPath, "key", "", "path of file that contains X509 key in PEM format")
rootCmd.PersistentFlags().BoolVarP(&commandFlags.Help, "help", "h", false, "help message")
rootCmd.AddCommand(
command.NewConfigCommand(),
command.NewRegionCommand(),
command.NewStoreCommand(),
command.NewStoresCommand(),
command.NewMemberCommand(),
command.NewExitCommand(),
command.NewLabelCommand(),
command.NewPingCommand(),
command.NewOperatorCommand(),
command.NewSchedulerCommand(),
command.NewTSOCommand(),
command.NewHotSpotCommand(),
command.NewClusterCommand(),
command.NewHealthCommand(),
command.NewLogCommand(),
command.NewPluginCommand(),
command.NewCompletionCommand(),
)
rootCmd.Flags().ParseErrorsWhitelist.UnknownFlags = true
rootCmd.SilenceErrors = true
return rootCmd
}
func getInteractCmd(args []string) *cobra.Command {
rootCmd := getBasicCmd()
rootCmd.SetArgs(args)
rootCmd.ParseFlags(args)
rootCmd.SetOutput(os.Stdout)
hiddenFlag(rootCmd)
return rootCmd
}
func getMainCmd(args []string) *cobra.Command {
rootCmd := getBasicCmd()
rootCmd.Flags().BoolVarP(&detach, "detach", "d", true, "Run pdctl without readline.")
rootCmd.Flags().BoolVarP(&interact, "interact", "i", false, "Run pdctl with readline.")
rootCmd.Flags().BoolVarP(&version, "version", "V", false, "Print version information and exit.")
rootCmd.Run = pdctlRun
rootCmd.SetArgs(args)
rootCmd.ParseFlags(args)
rootCmd.SetOutput(os.Stdout)
return rootCmd
}
// Hide the flags in help and usage messages.
func hiddenFlag(cmd *cobra.Command) {
cmd.LocalFlags().MarkHidden("pd")
cmd.LocalFlags().MarkHidden("cacert")
cmd.LocalFlags().MarkHidden("cert")
cmd.LocalFlags().MarkHidden("key")
}
// MainStart start main command
func MainStart(args []string) {
startCmd(getMainCmd, args)
}
// Start start interact command
func Start(args []string) {
startCmd(getInteractCmd, args)
}
func startCmd(getCmd func([]string) *cobra.Command, args []string) {
rootCmd := getCmd(args)
if len(commandFlags.CAPath) != 0 {
if err := command.InitHTTPSClient(commandFlags.CAPath, commandFlags.CertPath, commandFlags.KeyPath); err != nil {
rootCmd.Println(err)
return
}
}
if err := rootCmd.Execute(); err != nil {
rootCmd.Println(err)
}
}
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
}
args = append(args, "-u", commandFlags.URL)
if commandFlags.CAPath != "" && commandFlags.CertPath != "" && commandFlags.KeyPath != "" {
args = append(args, "--cacert", commandFlags.CAPath, "--cert", commandFlags.CertPath, "--key", commandFlags.KeyPath)
}
Start(args)
}
}