From c90e5f5851ca570c3a9e40fdb72fc770278fe7fa Mon Sep 17 00:00:00 2001 From: Ishan Tyagi Date: Thu, 10 Aug 2023 17:45:54 +0530 Subject: [PATCH] Added bbolt command line version flag to get runtime information. Signed-off-by: Ishan Tyagi --- cmd/bbolt/README.md | 16 ++++++++++++++++ cmd/bbolt/main.go | 31 +++++++++++++++++++++++++++++++ version/version.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 version/version.go diff --git a/cmd/bbolt/README.md b/cmd/bbolt/README.md index 851f380ba..ab6b6e8be 100644 --- a/cmd/bbolt/README.md +++ b/cmd/bbolt/README.md @@ -40,6 +40,7 @@ The commands are: + version prints the current version of bbolt bench run synthetic benchmark against bbolt buckets print a list of buckets check verifies integrity of bbolt database @@ -60,6 +61,21 @@ ## Analyse bbolt database with bbolt command line +### version + +- `version` prints the current version information of bbolt command-line +- usage: + `bbolt version` + + Example: + + ```bash + bbolt version: 1.3.7 + Git SHA: b7798fd9c1 + Go Version: go1.20.7 + Go OS/Arch: darwin/arm64 + ``` + ### info - `info` print the basic information about the given Bbolt database. diff --git a/cmd/bbolt/main.go b/cmd/bbolt/main.go index 364b1fe21..8fa9d6844 100644 --- a/cmd/bbolt/main.go +++ b/cmd/bbolt/main.go @@ -24,6 +24,7 @@ import ( berrors "go.etcd.io/bbolt/errors" "go.etcd.io/bbolt/internal/common" "go.etcd.io/bbolt/internal/guts_cli" + "go.etcd.io/bbolt/version" ) var ( @@ -116,6 +117,8 @@ func (m *Main) Run(args ...string) error { case "help": fmt.Fprintln(m.Stderr, m.Usage()) return ErrUsage + case "version": + return newVersionCommand(m).Run() case "bench": return newBenchCommand(m).Run(args[1:]...) case "buckets": @@ -156,6 +159,7 @@ Usage: The commands are: + version prints the current version of bbolt bench run synthetic benchmark against bbolt buckets print a list of buckets check verifies integrity of bbolt database @@ -175,6 +179,33 @@ Use "bbolt [command] -h" for more information about a command. `, "\n") } +// versionCommand represents the "version" command execution. +type versionCommand struct { + baseCommand +} + +// newVersionCommand returns a versionCommand. +func newVersionCommand(m *Main) *versionCommand { + v := &versionCommand{} + v.baseCommand = m.baseCommand + return v +} + +// Run executes the command. +func (cmd *versionCommand) Run() error { + fmt.Fprintln(cmd.Stdout, version.PrintVersionInfo()) + return nil +} + +// Usage returns the help message. +func (cmd *versionCommand) Usage() string { + return strings.TrimLeft(` + usage: bolt version + + Prints the current version of bbolt command-line. + `, "\n") +} + // checkCommand represents the "check" command execution. type checkCommand struct { baseCommand diff --git a/version/version.go b/version/version.go new file mode 100644 index 000000000..d069418b5 --- /dev/null +++ b/version/version.go @@ -0,0 +1,33 @@ +package version + +import ( + "fmt" + "os/exec" + "runtime" + "strings" +) + +var ( + // Version shows the last bbolt binary version released. + Version = "1.3.7" +) + +// PrintVersionInfo prints the information regrading version +func PrintVersionInfo() string { + return fmt.Sprintf( + "bbolt version: %s \nGit SHA: %s \nGo Version: %s \nGo OS/Arch: %s/%s", + Version, getGitSHA(), runtime.Version(), runtime.GOOS, runtime.GOARCH, + ) +} + +// getGitSHA returns the bbolt binary code commit SHA on git. +func getGitSHA() string { + cmd := exec.Command("git", "rev-parse", "HEAD") + output, err := cmd.Output() + if err != nil { + return "" + } + + commitSHA := strings.TrimSpace(string(output)) + return commitSHA[:10] +}