diff --git a/cmd/bbolt/command_info.go b/cmd/bbolt/command_info.go new file mode 100644 index 000000000..63db06f95 --- /dev/null +++ b/cmd/bbolt/command_info.go @@ -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 +} diff --git a/cmd/bbolt/command_root.go b/cmd/bbolt/command_root.go index 31a174080..e3f43ca7c 100644 --- a/cmd/bbolt/command_root.go +++ b/cmd/bbolt/command_root.go @@ -19,6 +19,7 @@ func NewRootCommand() *cobra.Command { rootCmd.AddCommand( newVersionCobraCommand(), newSurgeryCobraCommand(), + newInfoCobraCommand(), ) return rootCmd diff --git a/cmd/bbolt/command_version.go b/cmd/bbolt/command_version.go index 4434c515f..df8b3c19f 100644 --- a/cmd/bbolt/command_version.go +++ b/cmd/bbolt/command_version.go @@ -5,6 +5,7 @@ import ( "runtime" "github.com/spf13/cobra" + "go.etcd.io/bbolt/version" ) diff --git a/cmd/bbolt/main.go b/cmd/bbolt/main.go index ea284539e..a27968dec 100644 --- a/cmd/bbolt/main.go +++ b/cmd/bbolt/main.go @@ -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": @@ -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 diff --git a/cmd/bbolt/main_test.go b/cmd/bbolt/main_test.go index e137db3e9..134b551c6 100644 --- a/cmd/bbolt/main_test.go +++ b/cmd/bbolt/main_test.go @@ -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) } }