From f49d4282417e906446d6e60ad3892dbdc32985de Mon Sep 17 00:00:00 2001 From: charles-chenzz Date: Tue, 12 Dec 2023 19:02:33 +0600 Subject: [PATCH] migrate bbolt info command to cobra style Signed-off-by: charles-chenzz --- cmd/bbolt/command_info.go | 46 +++++++++++++++++++++++++++++ cmd/bbolt/command_root.go | 1 + cmd/bbolt/command_version.go | 1 + cmd/bbolt/main.go | 57 ------------------------------------ cmd/bbolt/main_test.go | 5 ++-- 5 files changed, 51 insertions(+), 59 deletions(-) create mode 100644 cmd/bbolt/command_info.go diff --git a/cmd/bbolt/command_info.go b/cmd/bbolt/command_info.go new file mode 100644 index 000000000..fa6db9163 --- /dev/null +++ b/cmd/bbolt/command_info.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" + + bolt "go.etcd.io/bbolt" +) + +func newInfoCommand() *cobra.Command { + infoCmd := &cobra.Command{ + Use: "info PATH", + Short: "Info prints basic information about the Bolt database at PATH.", + 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..9cdc8cee2 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(), + newInfoCommand(), ) 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) } }