Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate bbolt info command to cobra style #636

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest validating args cobra style, if I don't supply the DB path currently and just run bbolt info all I get is a panic and it's unclear from a user perspective what was done wrong.

Example arg validation:

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")
}
return nil
},

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can use

Args: cobra.ExactArgs(1)

but I've use the example that you list

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
Loading