-
Notifications
You must be signed in to change notification settings - Fork 649
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate bbolt info command to cobra style
Signed-off-by: charles-chenzz <[email protected]>
- Loading branch information
1 parent
a7a791c
commit 8d7ebe6
Showing
5 changed files
with
61 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"runtime" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"go.etcd.io/bbolt/version" | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters