diff --git a/cmd/backup.go b/cmd/backup.go index 18309017f..4e4317cfc 100644 --- a/cmd/backup.go +++ b/cmd/backup.go @@ -79,6 +79,9 @@ func newFullBackupCommand() *cobra.Command { command := &cobra.Command{ Use: "full", Short: "backup all database", + // prevents incorrect usage like `--checksum false` instead of `--checksum=false`. + // the former, according to pflag parsing rules, means `--checksum=true false`. + Args: cobra.NoArgs, RunE: func(command *cobra.Command, _ []string) error { // empty db/table means full backup. return runBackupCommand(command, "Full backup") @@ -93,6 +96,7 @@ func newDBBackupCommand() *cobra.Command { command := &cobra.Command{ Use: "db", Short: "backup a database", + Args: cobra.NoArgs, RunE: func(command *cobra.Command, _ []string) error { return runBackupCommand(command, "Database backup") }, @@ -106,6 +110,7 @@ func newTableBackupCommand() *cobra.Command { command := &cobra.Command{ Use: "table", Short: "backup a table", + Args: cobra.NoArgs, RunE: func(command *cobra.Command, _ []string) error { return runBackupCommand(command, "Table backup") }, @@ -120,6 +125,7 @@ func newRawBackupCommand() *cobra.Command { command := &cobra.Command{ Use: "raw", Short: "(experimental) backup a raw kv range from TiKV cluster", + Args: cobra.NoArgs, RunE: func(command *cobra.Command, _ []string) error { return runBackupRawCommand(command, "Raw backup") }, diff --git a/cmd/debug.go b/cmd/debug.go index 35f6e502d..65ad9ea6d 100644 --- a/cmd/debug.go +++ b/cmd/debug.go @@ -60,6 +60,7 @@ func newCheckSumCommand() *cobra.Command { command := &cobra.Command{ Use: "checksum", Short: "check the backup data", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx, cancel := context.WithCancel(GetDefaultContext()) defer cancel() @@ -144,6 +145,7 @@ func newBackupMetaCommand() *cobra.Command { command := &cobra.Command{ Use: "backupmeta", Short: "check the backup meta", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx, cancel := context.WithCancel(GetDefaultContext()) defer cancel() @@ -240,6 +242,7 @@ func decodeBackupMetaCommand() *cobra.Command { decodeBackupMetaCmd := &cobra.Command{ Use: "decode", Short: "decode backupmeta to json", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { ctx, cancel := context.WithCancel(GetDefaultContext()) defer cancel() @@ -300,6 +303,7 @@ func encodeBackupMetaCommand() *cobra.Command { encodeBackupMetaCmd := &cobra.Command{ Use: "encode", Short: "encode backupmeta json file to backupmeta", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { ctx, cancel := context.WithCancel(GetDefaultContext()) defer cancel() @@ -347,6 +351,7 @@ func setPDConfigCommand() *cobra.Command { pdConfigCmd := &cobra.Command{ Use: "reset-pd-config-as-default", Short: "reset pd config adjusted by BR to default value", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { ctx, cancel := context.WithCancel(GetDefaultContext()) defer cancel() diff --git a/cmd/restore.go b/cmd/restore.go index 58cbedb29..cb4d8d786 100644 --- a/cmd/restore.go +++ b/cmd/restore.go @@ -106,6 +106,7 @@ func newFullRestoreCommand() *cobra.Command { command := &cobra.Command{ Use: "full", Short: "restore all tables", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { return runRestoreCommand(cmd, "Full restore") }, @@ -118,6 +119,7 @@ func newDBRestoreCommand() *cobra.Command { command := &cobra.Command{ Use: "db", Short: "restore tables in a database", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { return runRestoreCommand(cmd, "Database restore") }, @@ -130,6 +132,7 @@ func newTableRestoreCommand() *cobra.Command { command := &cobra.Command{ Use: "table", Short: "restore a table", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { return runRestoreCommand(cmd, "Table restore") }, @@ -142,6 +145,7 @@ func newLogRestoreCommand() *cobra.Command { command := &cobra.Command{ Use: "cdclog", Short: "(experimental) restore data from cdc log backup", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { return runLogRestoreCommand(cmd) }, @@ -155,6 +159,7 @@ func newTiflashReplicaRestoreCommand() *cobra.Command { command := &cobra.Command{ Use: "tiflash-replica", Short: "restore the tiflash replica removed by a failed restore of the older version BR", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { return runRestoreTiflashReplicaCommand(cmd, "Restore TiFlash Replica") }, @@ -166,6 +171,7 @@ func newRawRestoreCommand() *cobra.Command { command := &cobra.Command{ Use: "raw", Short: "(experimental) restore a raw kv range to TiKV cluster", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { return runRestoreRawCommand(cmd, "Raw restore") }, diff --git a/pkg/task/common.go b/pkg/task/common.go index 1f5e6aa64..569be489c 100644 --- a/pkg/task/common.go +++ b/pkg/task/common.go @@ -420,11 +420,11 @@ func flagToZapField(f *pflag.Flag) zap.Field { // LogArguments prints origin command arguments. func LogArguments(cmd *cobra.Command) { - var fields []zap.Field - cmd.Flags().VisitAll(func(f *pflag.Flag) { - if f.Changed { - fields = append(fields, flagToZapField(f)) - } + flags := cmd.Flags() + fields := make([]zap.Field, 1, flags.NFlag()+1) + fields[0] = zap.String("__command", cmd.CommandPath()) + flags.Visit(func(f *pflag.Flag) { + fields = append(fields, flagToZapField(f)) }) log.Info("arguments", fields...) }