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

Commit

Permalink
cmd: convert version command to flags (#144)
Browse files Browse the repository at this point in the history
* cmd: convert version command to flags

Signed-off-by: Neil Shen <overvenus@gmail.com>

* address comments

Signed-off-by: Neil Shen <overvenus@gmail.com>
  • Loading branch information
overvenus authored and 3pointer committed Feb 11, 2020
1 parent 10adf0f commit f0bc562
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 34 deletions.
9 changes: 8 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/pingcap/br/pkg/conn"
"github.com/pingcap/br/pkg/storage"
"github.com/pingcap/br/pkg/utils"
)

var (
Expand Down Expand Up @@ -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")
Expand Down
20 changes: 0 additions & 20 deletions cmd/version.go

This file was deleted.

1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func main() {
cmd.AddFlags(rootCmd)
cmd.SetDefaultContext(ctx)
rootCmd.AddCommand(
cmd.NewVersionCommand(),
cmd.NewValidateCommand(),
cmd.NewBackupCommand(),
cmd.NewRestoreCommand(),
Expand Down
23 changes: 15 additions & 8 deletions pkg/utils/version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package utils

import (
"bytes"
"fmt"
"runtime"

"github.com/pingcap/log"
"github.com/pingcap/tidb/util/israce"
Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions tests/br_debug_meta/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,3 @@ then
fi

run_sql "DROP DATABASE $DB;"

# Test version
run_br version
3 changes: 2 additions & 1 deletion tests/br_other/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ fi
run_sql "DROP DATABASE $DB;"

# Test version
run_br version
run_br --version
run_br -V

0 comments on commit f0bc562

Please sign in to comment.