-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathflags.go
502 lines (412 loc) · 17.5 KB
/
flags.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// Copyright 2015 The Cockroach Authors.
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Daniel Theophanes ([email protected])
package cli
import (
"flag"
"fmt"
"net"
"strconv"
"strings"
"github.com/kr/text"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/cockroachdb/cockroach/base"
"github.com/cockroachdb/cockroach/cli/cliflags"
"github.com/cockroachdb/cockroach/security"
"github.com/cockroachdb/cockroach/util"
"github.com/cockroachdb/cockroach/util/log/logflags"
)
var maxResults int64
var connURL string
var connUser, connHost, connPort, httpPort, connDBName string
// cliContext is the CLI Context used for the command-line client.
var cliContext = NewContext()
var cacheSize *bytesValue
var insecure *insecureValue
var flagUsage = map[string]string{
cliflags.AttrsName: wrapText(`
An ordered, colon-separated list of node attributes. Attributes are
arbitrary strings specifying topography or machine
capabilities. Topography might include datacenter designation
(e.g. "us-west-1a", "us-west-1b", "us-east-1c"). Machine capabilities
might include specialized hardware or number of cores (e.g. "gpu",
"x16c"). The relative geographic proximity of two nodes is inferred
from the common prefix of the attributes list, so topographic
attributes should be specified first and in the same order for all
nodes. For example:`) + `
--attrs=us-west-1b:gpu
`,
cliflags.CacheName: wrapText(`
Total size in bytes for caches, shared evenly if there are multiple
storage devices. Size suffixes are supported (e.g. 1GB and 1GiB).`),
forClient(cliflags.HostName): wrapText(`
Database server host to connect to.`),
forClient(cliflags.PortName): wrapText(`
Database server port to connect to.`),
forClient(cliflags.HTTPPortName): wrapText(`
Database server port to connect to for HTTP requests.`),
cliflags.DatabaseName: wrapText(`
The name of the database to connect to.`),
cliflags.DepsName: wrapText(`
Include all dependency versions`),
cliflags.ExecuteName: wrapText(`
Execute the SQL statement(s) on the command line, then exit. This flag may be
specified multiple times and each value may contain multiple semicolon
separated statements. If an error occurs in any statement, the command exits
with a non-zero status code and further statements are not executed. The
results of each SQL statement are printed on the standard output.`),
cliflags.JoinName: wrapText(`
A comma-separated list of addresses to use when a new node is joining
an existing cluster. For the first node in a cluster, --join should
NOT be specified. Each address in the list has an optional type:
[type=]<address>. An unspecified type means ip address or dns. Type
is one of:`) + `
- tcp: (default if type is omitted): plain ip address or hostname.
- http-lb: HTTP load balancer: we query
http(s)://<address>/_status/details/local
`,
forServer(cliflags.HostName): wrapText(`
The address to listen on. The node will also advertise itself using this
hostname; it must resolve from other nodes in the cluster.`),
forServer(cliflags.PortName): wrapText(`
The port to bind to.`),
forServer(cliflags.HTTPPortName): wrapText(`
The port to bind to for HTTP requests.`),
cliflags.SocketName: wrapText(`
Unix socket file, postgresql protocol only.
Note: when given a path to a unix socket, most postgres clients will
open "<given path>/.s.PGSQL.<server port>"`),
cliflags.InsecureName: wrapText(`
Run over non-encrypted (non-TLS) connections. This is strongly discouraged for
production usage and this flag must be explicitly specified in order for the
server to listen on an external address in insecure mode.`),
cliflags.KeySizeName: wrapText(`
Key size in bits for CA/Node/Client certificates.`),
cliflags.MaxResultsName: wrapText(`
Define the maximum number of results that will be retrieved.`),
cliflags.PasswordName: wrapText(`
The created user's password. If provided, disables prompting. Pass '-' to
provide the password on standard input.`),
cliflags.CACertName: wrapText(`
Path to the CA certificate. Needed by clients and servers in secure mode.`),
cliflags.CAKeyName: wrapText(`
Path to the key protecting --ca-cert. Only needed when signing new certificates.`),
cliflags.CertName: wrapText(`
Path to the client or server certificate. Needed in secure mode.`),
cliflags.KeyName: wrapText(`
Path to the key protecting --cert. Needed in secure mode.`),
cliflags.StoreName: wrapText(`
The file path to a storage device. This flag must be specified separately for
each storage device, for example:`) + `
--store=/mnt/ssd01 --store=/mnt/ssd02 --store=/mnt/hda1
` + wrapText(`
For each store, the "attrs" and "size" fields can be used to specify device
attributes and a maximum store size (see below). When one or both of these
fields are set, the "path" field label must be used for the path to the storage
device, for example:`) + `
--store=path=/mnt/ssd01,attrs=ssd,size=20GiB
` + wrapText(`
In most cases, node-level attributes are preferable to store-level attributes.
However, the "attrs" field can be used to match capabilities for storage of
individual databases or tables. For example, an OLTP database would probably
want to allocate space for its tables only on solid state devices, whereas
append-only time series might prefer cheaper spinning drives. Typical
attributes include whether the store is flash (ssd), spinny disk (hdd), or
in-memory (mem), as well as speeds and other specs. Attributes can be arbitrary
strings separated by colons, for example: :`) + `
--store=path=/mnt/hda1,attrs=hdd:7200rpm
` + wrapText(`
The store size in the "size" field is not a guaranteed maximum but is used when
calculating free space for rebalancing purposes. The size can be specified
either in a bytes-based unit or as a percentage of hard drive space,
for example: :`) + `
--store=path=/mnt/ssd01,size=10000000000 -> 10000000000 bytes
--store-path=/mnt/ssd01,size=20GB -> 20000000000 bytes
--store-path=/mnt/ssd01,size=20GiB -> 21474836480 bytes
--store-path=/mnt/ssd01,size=0.02TiB -> 21474836480 bytes
--store=path=/mnt/ssd01,size=20% -> 20% of available space
--store=path=/mnt/ssd01,size=0.2 -> 20% of available space
--store=path=/mnt/ssd01,size=.2 -> 20% of available space
` + wrapText(`
For an in-memory store, the "type" and "size" fields are required, and the
"path" field is forbidden. The "type" field must be set to "mem", and the
"size" field must be set to the true maximum bytes or percentage of available
memory that the store may consume, for example:`) + `
--store=type=mem,size=20GiB
--store=type=mem,size=90%
` + wrapText(`
Commas are forbidden in all values, since they are used to separate fields.
Also, if you use equal signs in the file path to a store, you must use the
"path" field label.`),
"time-until-store-dead": wrapText(`
Adjusts the timeout for stores. If there's been no gossiped update
from a store after this time, the store is considered unavailable.
Replicas on an unavailable store will be moved to available ones.`),
cliflags.URLName: wrapText(`
Connection url. eg: postgresql://myuser@localhost:26257/mydb
If left empty, the connection flags are used (host, port, user,
database, insecure, certs).`),
cliflags.UserName: wrapText(`
Database user name.`),
cliflags.FromName: wrapText(`
Start key in pretty-printed format. See also --raw.`),
cliflags.ToName: wrapText(`
Exclusive end key in pretty-printed format. See also --raw.`),
cliflags.RawName: wrapText(`
Interpret keys as raw bytes.`),
cliflags.ValuesName: wrapText(`
Print values along with their associated key.`),
}
const usageIndentation = 8
const wrapWidth = 79 - usageIndentation
func wrapText(s string) string {
return text.Wrap(s, wrapWidth)
}
// usage returns the usage information for a given flag identifier. The
// identifier is always the flag's name, except in the case where a client/server
// distinction for the same flag is required.
func usage(flagID string) string {
s, ok := flagUsage[flagID]
if !ok {
panic(fmt.Sprintf("flag usage not defined for %q", flagID))
}
s = "\n" + strings.TrimSpace(s) + "\n"
// github.com/spf13/pflag appends the default value after the usage text. Add
// the correct indentation (7 spaces) here. This is admittedly fragile.
return text.Indent(s, strings.Repeat(" ", usageIndentation)) +
strings.Repeat(" ", usageIndentation-1)
}
// forServer maps a general flag name into a server-specific flag identifier.
func forServer(name string) string {
return fmt.Sprintf("server-%s", name)
}
// forClient maps a general flag name into a client-specific flag identifier.
func forClient(name string) string {
return fmt.Sprintf("client-%s", name)
}
type bytesValue struct {
val *int64
isSet bool
}
func newBytesValue(val *int64) *bytesValue {
return &bytesValue{val: val}
}
func (b *bytesValue) Set(s string) error {
v, err := util.ParseBytes(s)
if err != nil {
return err
}
*b.val = v
b.isSet = true
return nil
}
func (b *bytesValue) Type() string {
return "bytes"
}
func (b *bytesValue) String() string {
// This uses the MiB, GiB, etc suffixes. If we use humanize.Bytes() we get
// the MB, GB, etc suffixes, but the conversion is done in multiples of 1000
// vs 1024.
return util.IBytes(*b.val)
}
type insecureValue struct {
val *bool
isSet bool
}
func newInsecureValue(val *bool) *insecureValue {
return &insecureValue{val: val}
}
func (b *insecureValue) IsBoolFlag() bool {
return true
}
func (b *insecureValue) Set(s string) error {
v, err := strconv.ParseBool(s)
if err != nil {
return err
}
b.isSet = true
*b.val = v
if *b.val {
// If --insecure is specified, clear any of the existing security flags if
// they were set. This allows composition of command lines where a later
// specification of --insecure clears an earlier security specification.
cliContext.SSLCA = ""
cliContext.SSLCAKey = ""
cliContext.SSLCert = ""
cliContext.SSLCertKey = ""
}
return nil
}
func (b *insecureValue) Type() string {
return "bool"
}
func (b *insecureValue) String() string {
return fmt.Sprint(*b.val)
}
// initFlags sets the cli.Context values to flag values.
// Keep in sync with "server/context.go". Values in Context should be
// settable here.
func initFlags(ctx *Context) {
// Change the logging defaults for the main cockroach binary.
if err := flag.Lookup(logflags.LogToStderrName).Value.Set("false"); err != nil {
panic(err)
}
// Map any flags registered in the standard "flag" package into the
// top-level cockroach command.
pf := cockroachCmd.PersistentFlags()
flag.VisitAll(func(f *flag.Flag) {
pf.AddFlag(pflag.PFlagFromGoFlag(f))
})
// The --log-dir default changes depending on the command. Avoid confusion by
// simply clearing it.
pf.Lookup(logflags.LogDirName).DefValue = ""
// If no value is specified for --alsologtostderr output everything.
pf.Lookup(logflags.AlsoLogToStderrName).NoOptDefVal = "INFO"
{
f := startCmd.Flags()
// Server flags.
f.StringVar(&connHost, cliflags.HostName, "", usage(forServer(cliflags.HostName)))
f.StringVarP(&connPort, cliflags.PortName, "p", base.DefaultPort, usage(forServer(cliflags.PortName)))
f.StringVar(&httpPort, cliflags.HTTPPortName, base.DefaultHTTPPort, usage(forServer(cliflags.HTTPPortName)))
f.StringVar(&ctx.Attrs, cliflags.AttrsName, ctx.Attrs, usage(cliflags.AttrsName))
f.VarP(&ctx.Stores, cliflags.StoreName, "s", usage(cliflags.StoreName))
// Usage for the unix socket is odd as we use a real file, whereas
// postgresql and clients consider it a directory and build a filename
// inside it using the port.
// Thus, we keep it hidden and use it for testing only.
f.StringVar(&ctx.SocketFile, cliflags.SocketName, "", usage(cliflags.SocketName))
_ = f.MarkHidden(cliflags.SocketName)
// Security flags.
ctx.Insecure = true
insecure = newInsecureValue(&ctx.Insecure)
insecureF := f.VarPF(insecure, cliflags.InsecureName, "", usage(cliflags.InsecureName))
insecureF.NoOptDefVal = "true"
// Certificates.
f.StringVar(&ctx.SSLCA, cliflags.CACertName, ctx.SSLCA, usage(cliflags.CACertName))
f.StringVar(&ctx.SSLCert, cliflags.CertName, ctx.SSLCert, usage(cliflags.CertName))
f.StringVar(&ctx.SSLCertKey, cliflags.KeyName, ctx.SSLCertKey, usage(cliflags.KeyName))
// Cluster joining flags.
f.StringVar(&ctx.JoinUsing, cliflags.JoinName, ctx.JoinUsing, usage(cliflags.JoinName))
// Engine flags.
cacheSize = newBytesValue(&ctx.CacheSize)
f.Var(cacheSize, cliflags.CacheName, usage(cliflags.CacheName))
// Clear the cache default value. This flag does have a default, but
// it is set only when the "start" command is run.
f.Lookup(cliflags.CacheName).DefValue = ""
if err := startCmd.MarkFlagRequired(cliflags.StoreName); err != nil {
panic(err)
}
}
{
f := exterminateCmd.Flags()
f.Var(&ctx.Stores, cliflags.StoreName, usage(cliflags.StoreName))
if err := exterminateCmd.MarkFlagRequired(cliflags.StoreName); err != nil {
panic(err)
}
}
for _, cmd := range certCmds {
f := cmd.Flags()
// Certificate flags.
f.StringVar(&ctx.SSLCA, cliflags.CACertName, ctx.SSLCA, usage(cliflags.CACertName))
f.StringVar(&ctx.SSLCAKey, cliflags.CAKeyName, ctx.SSLCAKey, usage(cliflags.CAKeyName))
f.StringVar(&ctx.SSLCert, cliflags.CertName, ctx.SSLCert, usage(cliflags.CertName))
f.StringVar(&ctx.SSLCertKey, cliflags.KeyName, ctx.SSLCertKey, usage(cliflags.KeyName))
f.IntVar(&keySize, cliflags.KeySizeName, defaultKeySize, usage(cliflags.KeySizeName))
if err := cmd.MarkFlagRequired(cliflags.KeySizeName); err != nil {
panic(err)
}
}
setUserCmd.Flags().StringVar(&password, cliflags.PasswordName, "", usage(cliflags.PasswordName))
clientCmds := []*cobra.Command{
sqlShellCmd, exterminateCmd, quitCmd, /* startCmd is covered above */
}
clientCmds = append(clientCmds, kvCmds...)
clientCmds = append(clientCmds, rangeCmds...)
clientCmds = append(clientCmds, userCmds...)
clientCmds = append(clientCmds, zoneCmds...)
clientCmds = append(clientCmds, nodeCmds...)
for _, cmd := range clientCmds {
f := cmd.PersistentFlags()
insecureF := f.VarPF(insecure, cliflags.InsecureName, "", usage(cliflags.InsecureName))
insecureF.NoOptDefVal = "true"
f.StringVar(&connHost, cliflags.HostName, "", usage(forClient(cliflags.HostName)))
// Certificate flags.
f.StringVar(&ctx.SSLCA, cliflags.CACertName, ctx.SSLCA, usage(cliflags.CACertName))
f.StringVar(&ctx.SSLCert, cliflags.CertName, ctx.SSLCert, usage(cliflags.CertName))
f.StringVar(&ctx.SSLCertKey, cliflags.KeyName, ctx.SSLCertKey, usage(cliflags.KeyName))
}
{
f := sqlShellCmd.Flags()
f.VarP(&ctx.execStmts, cliflags.ExecuteName, "e", usage(cliflags.ExecuteName))
}
// Commands that need the cockroach port.
simpleCmds := []*cobra.Command{exterminateCmd}
simpleCmds = append(simpleCmds, kvCmds...)
simpleCmds = append(simpleCmds, rangeCmds...)
for _, cmd := range simpleCmds {
f := cmd.PersistentFlags()
f.StringVarP(&connPort, cliflags.PortName, "p", base.DefaultPort, usage(forClient(cliflags.PortName)))
}
// Commands that need an http port.
httpCmds := []*cobra.Command{quitCmd}
httpCmds = append(httpCmds, nodeCmds...)
for _, cmd := range httpCmds {
f := cmd.PersistentFlags()
f.StringVar(&httpPort, cliflags.HTTPPortName, base.DefaultHTTPPort, usage(forClient(cliflags.HTTPPortName)))
}
// Commands that establish a SQL connection.
sqlCmds := []*cobra.Command{sqlShellCmd}
sqlCmds = append(sqlCmds, zoneCmds...)
sqlCmds = append(sqlCmds, userCmds...)
for _, cmd := range sqlCmds {
f := cmd.PersistentFlags()
f.StringVar(&connURL, cliflags.URLName, "", usage(cliflags.URLName))
f.StringVarP(&connUser, cliflags.UserName, "u", security.RootUser, usage(cliflags.UserName))
f.StringVarP(&connPort, cliflags.PortName, "p", base.DefaultPort, usage(forClient(cliflags.PortName)))
f.StringVarP(&connDBName, cliflags.DatabaseName, "d", "", usage(cliflags.DatabaseName))
}
// Max results flag for scan, reverse scan, and range list.
for _, cmd := range []*cobra.Command{scanCmd, reverseScanCmd, lsRangesCmd} {
f := cmd.Flags()
f.Int64Var(&maxResults, cliflags.MaxResultsName, 1000, usage(cliflags.MaxResultsName))
}
// Debug commands.
{
f := debugKeysCmd.Flags()
f.StringVar(&cliContext.debug.startKey, cliflags.FromName, "", usage(cliflags.FromName))
f.StringVar(&cliContext.debug.endKey, cliflags.ToName, "", usage(cliflags.ToName))
f.BoolVar(&cliContext.debug.raw, cliflags.RawName, false, usage(cliflags.RawName))
f.BoolVar(&cliContext.debug.values, cliflags.ValuesName, false, usage(cliflags.ValuesName))
}
{
f := versionCmd.Flags()
f.BoolVar(&versionIncludesDeps, cliflags.DepsName, false, usage(cliflags.DepsName))
}
}
func init() {
initFlags(cliContext)
cobra.OnInitialize(func() {
// If any of the security flags have been set, clear the insecure
// setting. Note that we do the inverse when the --insecure flag is
// set. See insecureValue.Set().
if cliContext.SSLCA != "" || cliContext.SSLCAKey != "" ||
cliContext.SSLCert != "" || cliContext.SSLCertKey != "" {
cliContext.Insecure = false
}
cliContext.Addr = net.JoinHostPort(connHost, connPort)
cliContext.HTTPAddr = net.JoinHostPort(connHost, httpPort)
})
}