-
Notifications
You must be signed in to change notification settings - Fork 660
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
Changes from 1 commit
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,45 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
bolt "go.etcd.io/bbolt" | ||
) | ||
|
||
func newBucketsCommand() *cobra.Command { | ||
var bucketsCmd = &cobra.Command{ | ||
Use: "buckets <path>", | ||
Short: "print a list of buckets", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return bucketsFunc(cmd, args[0]) | ||
}, | ||
} | ||
return bucketsCmd | ||
} | ||
|
||
func bucketsFunc(cmd *cobra.Command, srcDBPath string) error { | ||
// Required database path. | ||
if srcDBPath == "" { | ||
return ErrPathRequired | ||
// Verify if the specified database file exists. | ||
} else if _, err := checkSourceDBPath(srcDBPath); err != nil { | ||
return err | ||
} | ||
tommyshem marked this conversation as resolved.
Show resolved
Hide resolved
tommyshem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Open database. | ||
db, err := bolt.Open(srcDBPath, 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)) | ||
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. I agree that 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. cmd.OutOrStdout() is all ready used in the cobra style converted files already 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. I see that it's only used in Overall, not a big deal. Feel free to keep it as it's 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. I must have changed the over files in my copy above when I was testing the outputs. |
||
return nil | ||
}) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"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 buckets command. | ||
t.Log("Running buckets command") | ||
rootCmd := main.NewRootCommand() | ||
var args []string = []string{"buckets", db.Path()} | ||
outputBuf := bytes.NewBufferString("") | ||
rootCmd.SetOut(outputBuf) | ||
rootCmd.SetErr(outputBuf) | ||
rootCmd.SetArgs(args) | ||
_, err := rootCmd.ExecuteC() | ||
actualOutput := outputBuf.String() | ||
require.NoError(t, err) | ||
t.Log("Verify result") | ||
expected := "bar\nbaz\nfoo\n" | ||
require.EqualValues(t, expected, actualOutput) | ||
} |
ahrtr marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
I think the
cobra.ExactArgs(1)
already guarantees that it won't be empty string? Pleasae follow the same pattern as other command.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.
This check is from the original code.
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.
"the original code" doesn't mean that it's necessary.
Please also always remember to simplify the the implementation,
bbolt/cmd/bbolt/command_inspect.go
Lines 26 to 30 in cb0618b
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.
In order to avoid long back-and-forth, please see #859 (comment)