Skip to content

Commit

Permalink
Added bbolt command line version flag to get runtime information.
Browse files Browse the repository at this point in the history
Signed-off-by: Ishan Tyagi <[email protected]>
  • Loading branch information
ishan16696 committed Aug 10, 2023
1 parent b7798fd commit 1244ce2
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cmd/bbolt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -60,6 +61,22 @@

## 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
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.
Expand Down
31 changes: 31 additions & 0 deletions cmd/bbolt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
33 changes: 33 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -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 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]
}

0 comments on commit 1244ce2

Please sign in to comment.