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 16, 2023
1 parent a7a791c commit 96c8323
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 57 deletions.
45 changes: 45 additions & 0 deletions cmd/bbolt/command_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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: "bolt info PATH",
Short: "Info prints basic information about the Bolt database at PATH.",
Args: cobra.ExactArgs(1),
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(),
newInfoCommand(),
)

return rootCmd
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

0 comments on commit 96c8323

Please sign in to comment.