Skip to content

Commit

Permalink
migrate bbolt info command to cobra style
Browse files Browse the repository at this point in the history
Signed-off-by: charles-chenzz <[email protected]>
  • Loading branch information
charles-chenzz committed Dec 17, 2023
1 parent a7a791c commit 8d7ebe6
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 59 deletions.
56 changes: 56 additions & 0 deletions cmd/bbolt/command_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"errors"
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

bolt "go.etcd.io/bbolt"
)

func newInfoCobraCommand() *cobra.Command {
infoCmd := &cobra.Command{
Use: "info PATH",
Short: "Info prints basic information about the Bolt database at PATH.",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("db file path not provided")
}
if len(args) > 1 {
return errors.New("too many arguments, only accept db file path")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
path := args[0]
if path == "" {
return ErrPathRequired
} else if _, err := os.Stat(path); os.IsNotExist(err) {
return ErrFileNotFound
}

// Open the database.
db, err := bolt.Open(path, 0666, &bolt.Options{ReadOnly: true})
if err != nil {
return err
}
defer db.Close()

// Print basic database info.
info := db.Info()
fmt.Fprintf(os.Stdout, "Page size: %d\n", info.PageSize)

return nil
},
}

fs := pflag.NewFlagSet("", pflag.ContinueOnError)
fs.Bool("h", false, "")

infoCmd.Flags().AddFlagSet(fs)

return infoCmd
}
1 change: 1 addition & 0 deletions cmd/bbolt/command_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func NewRootCommand() *cobra.Command {
rootCmd.AddCommand(
newVersionCobraCommand(),
newSurgeryCobraCommand(),
newInfoCobraCommand(),
)

return rootCmd
Expand Down
1 change: 1 addition & 0 deletions cmd/bbolt/command_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"runtime"

"github.com/spf13/cobra"

"go.etcd.io/bbolt/version"
)

Expand Down
57 changes: 0 additions & 57 deletions cmd/bbolt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ func (m *Main) Run(args ...string) error {
return newPageItemCommand(m).Run(args[1:]...)
case "get":
return newGetCommand(m).Run(args[1:]...)
case "info":
return newInfoCommand(m).Run(args[1:]...)
case "keys":
return newKeysCommand(m).Run(args[1:]...)
case "page":
Expand Down Expand Up @@ -252,61 +250,6 @@ return after all pages have been checked.
`, "\n")
}

// infoCommand represents the "info" command execution.
type infoCommand struct {
baseCommand
}

// newInfoCommand returns a infoCommand.
func newInfoCommand(m *Main) *infoCommand {
c := &infoCommand{}
c.baseCommand = m.baseCommand
return c
}

// Run executes the command.
func (cmd *infoCommand) Run(args ...string) error {
// Parse flags.
fs := flag.NewFlagSet("", flag.ContinueOnError)
help := fs.Bool("h", false, "")
if err := fs.Parse(args); err != nil {
return err
} else if *help {
fmt.Fprintln(cmd.Stderr, cmd.Usage())
return ErrUsage
}

// Require database path.
path := fs.Arg(0)
if path == "" {
return ErrPathRequired
} else if _, err := os.Stat(path); os.IsNotExist(err) {
return ErrFileNotFound
}

// Open the database.
db, err := bolt.Open(path, 0600, &bolt.Options{ReadOnly: true})
if err != nil {
return err
}
defer db.Close()

// Print basic database info.
info := db.Info()
fmt.Fprintf(cmd.Stdout, "Page Size: %d\n", info.PageSize)

return nil
}

// Usage returns the help message.
func (cmd *infoCommand) Usage() string {
return strings.TrimLeft(`
usage: bolt info PATH
Info prints basic information about the Bolt database at PATH.
`, "\n")
}

// dumpCommand represents the "dump" command execution.
type dumpCommand struct {
baseCommand
Expand Down
5 changes: 3 additions & 2 deletions cmd/bbolt/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ func TestInfoCommand_Run(t *testing.T) {
defer requireDBNoChange(t, dbData(t, db.Path()), db.Path())

// Run the info command.
m := NewMain()
if err := m.Run("info", db.Path()); err != nil {
m := main.NewRootCommand()
m.SetArgs([]string{"info", db.Path()})
if err := m.Execute(); err != nil {
t.Fatal(err)
}
}
Expand Down

0 comments on commit 8d7ebe6

Please sign in to comment.