Skip to content

Commit 8d9a51a

Browse files
authored
cmd,task: disable stray command line args, and log the command in LogArguments (pingcap#579)
* cmd: disallow stray args * task: in LogArguments, log the command name too
1 parent 412dc71 commit 8d9a51a

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

cmd/backup.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ func newFullBackupCommand() *cobra.Command {
7979
command := &cobra.Command{
8080
Use: "full",
8181
Short: "backup all database",
82+
// prevents incorrect usage like `--checksum false` instead of `--checksum=false`.
83+
// the former, according to pflag parsing rules, means `--checksum=true false`.
84+
Args: cobra.NoArgs,
8285
RunE: func(command *cobra.Command, _ []string) error {
8386
// empty db/table means full backup.
8487
return runBackupCommand(command, "Full backup")
@@ -93,6 +96,7 @@ func newDBBackupCommand() *cobra.Command {
9396
command := &cobra.Command{
9497
Use: "db",
9598
Short: "backup a database",
99+
Args: cobra.NoArgs,
96100
RunE: func(command *cobra.Command, _ []string) error {
97101
return runBackupCommand(command, "Database backup")
98102
},
@@ -106,6 +110,7 @@ func newTableBackupCommand() *cobra.Command {
106110
command := &cobra.Command{
107111
Use: "table",
108112
Short: "backup a table",
113+
Args: cobra.NoArgs,
109114
RunE: func(command *cobra.Command, _ []string) error {
110115
return runBackupCommand(command, "Table backup")
111116
},
@@ -120,6 +125,7 @@ func newRawBackupCommand() *cobra.Command {
120125
command := &cobra.Command{
121126
Use: "raw",
122127
Short: "(experimental) backup a raw kv range from TiKV cluster",
128+
Args: cobra.NoArgs,
123129
RunE: func(command *cobra.Command, _ []string) error {
124130
return runBackupRawCommand(command, "Raw backup")
125131
},

cmd/debug.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func newCheckSumCommand() *cobra.Command {
6060
command := &cobra.Command{
6161
Use: "checksum",
6262
Short: "check the backup data",
63+
Args: cobra.NoArgs,
6364
RunE: func(cmd *cobra.Command, _ []string) error {
6465
ctx, cancel := context.WithCancel(GetDefaultContext())
6566
defer cancel()
@@ -144,6 +145,7 @@ func newBackupMetaCommand() *cobra.Command {
144145
command := &cobra.Command{
145146
Use: "backupmeta",
146147
Short: "check the backup meta",
148+
Args: cobra.NoArgs,
147149
RunE: func(cmd *cobra.Command, _ []string) error {
148150
ctx, cancel := context.WithCancel(GetDefaultContext())
149151
defer cancel()
@@ -240,6 +242,7 @@ func decodeBackupMetaCommand() *cobra.Command {
240242
decodeBackupMetaCmd := &cobra.Command{
241243
Use: "decode",
242244
Short: "decode backupmeta to json",
245+
Args: cobra.NoArgs,
243246
RunE: func(cmd *cobra.Command, args []string) error {
244247
ctx, cancel := context.WithCancel(GetDefaultContext())
245248
defer cancel()
@@ -300,6 +303,7 @@ func encodeBackupMetaCommand() *cobra.Command {
300303
encodeBackupMetaCmd := &cobra.Command{
301304
Use: "encode",
302305
Short: "encode backupmeta json file to backupmeta",
306+
Args: cobra.NoArgs,
303307
RunE: func(cmd *cobra.Command, args []string) error {
304308
ctx, cancel := context.WithCancel(GetDefaultContext())
305309
defer cancel()
@@ -347,6 +351,7 @@ func setPDConfigCommand() *cobra.Command {
347351
pdConfigCmd := &cobra.Command{
348352
Use: "reset-pd-config-as-default",
349353
Short: "reset pd config adjusted by BR to default value",
354+
Args: cobra.NoArgs,
350355
RunE: func(cmd *cobra.Command, args []string) error {
351356
ctx, cancel := context.WithCancel(GetDefaultContext())
352357
defer cancel()

cmd/restore.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func newFullRestoreCommand() *cobra.Command {
106106
command := &cobra.Command{
107107
Use: "full",
108108
Short: "restore all tables",
109+
Args: cobra.NoArgs,
109110
RunE: func(cmd *cobra.Command, _ []string) error {
110111
return runRestoreCommand(cmd, "Full restore")
111112
},
@@ -118,6 +119,7 @@ func newDBRestoreCommand() *cobra.Command {
118119
command := &cobra.Command{
119120
Use: "db",
120121
Short: "restore tables in a database",
122+
Args: cobra.NoArgs,
121123
RunE: func(cmd *cobra.Command, _ []string) error {
122124
return runRestoreCommand(cmd, "Database restore")
123125
},
@@ -130,6 +132,7 @@ func newTableRestoreCommand() *cobra.Command {
130132
command := &cobra.Command{
131133
Use: "table",
132134
Short: "restore a table",
135+
Args: cobra.NoArgs,
133136
RunE: func(cmd *cobra.Command, _ []string) error {
134137
return runRestoreCommand(cmd, "Table restore")
135138
},
@@ -142,6 +145,7 @@ func newLogRestoreCommand() *cobra.Command {
142145
command := &cobra.Command{
143146
Use: "cdclog",
144147
Short: "(experimental) restore data from cdc log backup",
148+
Args: cobra.NoArgs,
145149
RunE: func(cmd *cobra.Command, _ []string) error {
146150
return runLogRestoreCommand(cmd)
147151
},
@@ -155,6 +159,7 @@ func newTiflashReplicaRestoreCommand() *cobra.Command {
155159
command := &cobra.Command{
156160
Use: "tiflash-replica",
157161
Short: "restore the tiflash replica removed by a failed restore of the older version BR",
162+
Args: cobra.NoArgs,
158163
RunE: func(cmd *cobra.Command, _ []string) error {
159164
return runRestoreTiflashReplicaCommand(cmd, "Restore TiFlash Replica")
160165
},
@@ -166,6 +171,7 @@ func newRawRestoreCommand() *cobra.Command {
166171
command := &cobra.Command{
167172
Use: "raw",
168173
Short: "(experimental) restore a raw kv range to TiKV cluster",
174+
Args: cobra.NoArgs,
169175
RunE: func(cmd *cobra.Command, _ []string) error {
170176
return runRestoreRawCommand(cmd, "Raw restore")
171177
},

pkg/task/common.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,11 @@ func flagToZapField(f *pflag.Flag) zap.Field {
420420

421421
// LogArguments prints origin command arguments.
422422
func LogArguments(cmd *cobra.Command) {
423-
var fields []zap.Field
424-
cmd.Flags().VisitAll(func(f *pflag.Flag) {
425-
if f.Changed {
426-
fields = append(fields, flagToZapField(f))
427-
}
423+
flags := cmd.Flags()
424+
fields := make([]zap.Field, 1, flags.NFlag()+1)
425+
fields[0] = zap.String("__command", cmd.CommandPath())
426+
flags.Visit(func(f *pflag.Flag) {
427+
fields = append(fields, flagToZapField(f))
428428
})
429429
log.Info("arguments", fields...)
430430
}

0 commit comments

Comments
 (0)