-
Notifications
You must be signed in to change notification settings - Fork 646
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
command buckets converted to the cobra style #859
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,51 @@ | ||||||||||||||||||||||||
package main | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
import ( | ||||||||||||||||||||||||
"fmt" | ||||||||||||||||||||||||
"os" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
"github.com/spf13/cobra" | ||||||||||||||||||||||||
bolt "go.etcd.io/bbolt" | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// newBucketsCommand creates a new command that prints a list of buckets in the given Bolt database. | ||||||||||||||||||||||||
// | ||||||||||||||||||||||||
// The path to a Bolt database must be specified as an argument. | ||||||||||||||||||||||||
func newBucketsCommand() *cobra.Command { | ||||||||||||||||||||||||
var bucketsCmd = &cobra.Command{ | ||||||||||||||||||||||||
Use: "buckets <path>", | ||||||||||||||||||||||||
Short: "Print a list of buckets", | ||||||||||||||||||||||||
Long: "Print a list of buckets in the given Bolt database\nThe path to a Bolt database must be specified as an argument", | ||||||||||||||||||||||||
Args: cobra.ExactArgs(1), | ||||||||||||||||||||||||
RunE: func(cmd *cobra.Command, args []string) error { | ||||||||||||||||||||||||
return printBucketsList(cmd, args[0]) | ||||||||||||||||||||||||
}, | ||||||||||||||||||||||||
Comment on lines
+17
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please try to keep consistent with other commands implementation.
Suggested change
|
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
return bucketsCmd | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// printBucketsList prints a list of buckets in the given Bolt database. | ||||||||||||||||||||||||
func printBucketsList(cmd *cobra.Command, path string) error { | ||||||||||||||||||||||||
// Required database path. | ||||||||||||||||||||||||
if path == "" { | ||||||||||||||||||||||||
return ErrPathRequired | ||||||||||||||||||||||||
// Verify if the specified database file exists. | ||||||||||||||||||||||||
} else if _, err := os.Stat(path); os.IsNotExist(err) { | ||||||||||||||||||||||||
return ErrFileNotFound | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use bbolt/cmd/bbolt/command_inspect.go Line 27 in cb0618b
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
// Open database. | ||||||||||||||||||||||||
db, err := bolt.Open(path, 0600, &bolt.Options{ReadOnly: true}) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
return err | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
defer db.Close() | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// Print the list of buckets in the database. | ||||||||||||||||||||||||
return db.View(func(tx *bolt.Tx) error { | ||||||||||||||||||||||||
return tx.ForEach(func(name []byte, _ *bolt.Bucket) error { | ||||||||||||||||||||||||
fmt.Fprintln(cmd.OutOrStdout(), string(name)) | ||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||
}) | ||||||||||||||||||||||||
}) | ||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/require" | ||
|
||
bolt "go.etcd.io/bbolt" | ||
main "go.etcd.io/bbolt/cmd/bbolt" | ||
"go.etcd.io/bbolt/internal/btesting" | ||
) | ||
|
||
// Ensure the "buckets" command can print a list of buckets. | ||
func TestBuckets(t *testing.T) { | ||
// Create a test database and populate it with sample buckets. | ||
t.Log("Creating sample DB") | ||
db := btesting.MustCreateDB(t) | ||
t.Log("Creating sample Buckets") | ||
if err := db.Update(func(tx *bolt.Tx) error { | ||
for _, name := range []string{"foo", "bar", "baz"} { | ||
_, err := tx.CreateBucket([]byte(name)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
}); err != nil { | ||
t.Fatal(err) | ||
} | ||
db.Close() | ||
defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) | ||
|
||
// setup the bucketscommand. | ||
t.Log("Running buckets command") | ||
rootCmd := main.NewRootCommand() | ||
_, actualOutput, err := executeCommand(rootCmd, "buckets", db.Path()) //rootCmd, "buckets", db.Path()) | ||
require.NoError(t, err) | ||
t.Log("Verify result") | ||
expected := "bar\nbaz\nfoo\n" | ||
require.EqualValues(t, expected, actualOutput) | ||
} | ||
|
||
// executeCommand runs the given command and returns the output and error. | ||
func executeCommand(rootCmd *cobra.Command, args ...string) (*cobra.Command, string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just remove this function and get the implementation inlined in the test? If you want to make it as a generic test utility function (i.e. included in utils_test.go), let's do it in a followup PR and we can discuss it separately. |
||
outputBuf := bytes.NewBufferString("") | ||
rootCmd.SetOut(outputBuf) | ||
rootCmd.SetErr(outputBuf) | ||
rootCmd.SetArgs(args) | ||
c, err := rootCmd.ExecuteC() | ||
return c, outputBuf.String(), err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unnecessary comment to me. The command is self-explanation.