diff --git a/cmd/cmd.go b/cmd/cmd.go index e97adec43..468c35232 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -19,6 +19,7 @@ import ( "github.com/pingcap/br/pkg/conn" "github.com/pingcap/br/pkg/storage" + "github.com/pingcap/br/pkg/utils" ) var ( @@ -52,12 +53,18 @@ const ( FlagSlowLogFile = "slow-log-file" flagDatabase = "db" + flagTable = "table" - flagTable = "table" + flagVersion = "version" + flagVersionShort = "V" ) // AddFlags adds flags to the given cmd. func AddFlags(cmd *cobra.Command) { + cmd.Version = utils.BRInfo() + cmd.Flags().BoolP(flagVersion, flagVersionShort, false, "Display version information about BR") + cmd.SetVersionTemplate("{{printf \"%s\" .Version}}\n") + cmd.PersistentFlags().StringP(FlagPD, "u", "127.0.0.1:2379", "PD address") cmd.PersistentFlags().String(FlagCA, "", "CA certificate path for TLS connection") cmd.PersistentFlags().String(FlagCert, "", "Certificate path for TLS connection") diff --git a/cmd/version.go b/cmd/version.go deleted file mode 100644 index af4f7d386..000000000 --- a/cmd/version.go +++ /dev/null @@ -1,20 +0,0 @@ -package cmd - -import ( - "github.com/spf13/cobra" - - "github.com/pingcap/br/pkg/utils" -) - -// NewVersionCommand returns a restore subcommand -func NewVersionCommand() *cobra.Command { - bp := &cobra.Command{ - Use: "version", - Short: "output version information", - Args: cobra.NoArgs, - Run: func(cmd *cobra.Command, args []string) { - utils.PrintBRInfo() - }, - } - return bp -} diff --git a/main.go b/main.go index 8ae652f86..103699614 100644 --- a/main.go +++ b/main.go @@ -47,7 +47,6 @@ func main() { cmd.AddFlags(rootCmd) cmd.SetDefaultContext(ctx) rootCmd.AddCommand( - cmd.NewVersionCommand(), cmd.NewValidateCommand(), cmd.NewBackupCommand(), cmd.NewRestoreCommand(), diff --git a/pkg/utils/version.go b/pkg/utils/version.go index bed19ffa9..13a3c7a92 100644 --- a/pkg/utils/version.go +++ b/pkg/utils/version.go @@ -1,7 +1,9 @@ package utils import ( + "bytes" "fmt" + "runtime" "github.com/pingcap/log" "github.com/pingcap/tidb/util/israce" @@ -16,25 +18,30 @@ var ( BRBuildTS = "None" BRGitHash = "None" BRGitBranch = "None" + goVersion = runtime.Version() ) -// LogBRInfo prints the BR version information. +// LogBRInfo logs version information about BR. func LogBRInfo() { log.Info("Welcome to Backup & Restore (BR)") log.Info("BR", zap.String("release-version", BRReleaseVersion)) log.Info("BR", zap.String("git-hash", BRGitHash)) log.Info("BR", zap.String("git-branch", BRGitBranch)) + log.Info("BR", zap.String("go-version", goVersion)) log.Info("BR", zap.String("utc-build-time", BRBuildTS)) log.Info("BR", zap.Bool("race-enabled", israce.RaceEnabled)) } -// PrintBRInfo prints the BR version information without log info. -func PrintBRInfo() { - fmt.Println("Release Version:", BRReleaseVersion) - fmt.Println("Git Commit Hash:", BRGitHash) - fmt.Println("Git Branch:", BRGitBranch) - fmt.Println("UTC Build Time: ", BRBuildTS) - fmt.Println("Race Enabled: ", israce.RaceEnabled) +// BRInfo returns version information about BR. +func BRInfo() string { + buf := bytes.Buffer{} + fmt.Fprintf(&buf, "Release Version: %s\n", BRReleaseVersion) + fmt.Fprintf(&buf, "Git Commit Hash: %s\n", BRGitHash) + fmt.Fprintf(&buf, "Git Branch: %s\n", BRGitBranch) + fmt.Fprintf(&buf, "Go Version: %s\n", goVersion) + fmt.Fprintf(&buf, "UTC Build Time: %s\n", BRBuildTS) + fmt.Fprintf(&buf, "Race Enabled: %t", israce.RaceEnabled) + return buf.String() } // LogArguments prints origin command arguments diff --git a/tests/br_debug_meta/run.sh b/tests/br_debug_meta/run.sh index 679602deb..1dcfccefe 100644 --- a/tests/br_debug_meta/run.sh +++ b/tests/br_debug_meta/run.sh @@ -57,6 +57,3 @@ then fi run_sql "DROP DATABASE $DB;" - -# Test version -run_br version \ No newline at end of file diff --git a/tests/br_other/run.sh b/tests/br_other/run.sh index 579c630df..e25dd2eae 100644 --- a/tests/br_other/run.sh +++ b/tests/br_other/run.sh @@ -55,4 +55,5 @@ fi run_sql "DROP DATABASE $DB;" # Test version -run_br version +run_br --version +run_br -V