Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
cmd,task: disable stray command line args, and log the command in Log…
Browse files Browse the repository at this point in the history
…Arguments (#579) (#588)

* cmd: disallow stray args

* task: in LogArguments, log the command name too

Co-authored-by: kennytm <kennytm@gmail.com>
  • Loading branch information
ti-srebot and kennytm authored Nov 10, 2020
1 parent c2ed897 commit 2ac49a4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
6 changes: 6 additions & 0 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
},
Expand All @@ -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")
},
Expand All @@ -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")
},
Expand Down
5 changes: 5 additions & 0 deletions cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
},
Expand All @@ -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")
},
Expand All @@ -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")
},
Expand All @@ -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)
},
Expand All @@ -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")
},
Expand All @@ -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")
},
Expand Down
10 changes: 5 additions & 5 deletions pkg/task/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
Expand Down

0 comments on commit 2ac49a4

Please sign in to comment.