Skip to content
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

fix: change getCommand default format auto #598

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/bbolt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ func (cmd *getCommand) Run(args ...string) error {
var parseFormat string
var format string
fs.StringVar(&parseFormat, "parse-format", "ascii-encoded", "Input format. One of: ascii-encoded|hex (default: ascii-encoded)")
fs.StringVar(&format, "format", "bytes", "Output format. One of: "+FORMAT_MODES+" (default: bytes)")
fs.StringVar(&format, "format", "auto", "Output format. One of: "+FORMAT_MODES+" (default: auto)")
help := fs.Bool("h", false, "")
if err := fs.Parse(args); err != nil {
return err
Expand Down Expand Up @@ -1062,7 +1062,7 @@ Print the value of the given key in the given (sub)bucket.
Additional options include:

--format
Output format. One of: `+FORMAT_MODES+` (default=bytes)
Output format. One of: `+FORMAT_MODES+` (default=auto)
--parse-format
Input format (of key). One of: ascii-encoded|hex (default=ascii-encoded)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no auto for parse-format to the input. So, I think we should not touch this line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be Line 1065

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, my bad 👍🏽 thanks tons 🙏🏽

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed 👍🏽

`, "\n")
Expand Down
79 changes: 53 additions & 26 deletions cmd/bbolt/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"go.etcd.io/bbolt/internal/btesting"
"go.etcd.io/bbolt/internal/guts_cli"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

bolt "go.etcd.io/bbolt"
Expand Down Expand Up @@ -355,38 +356,64 @@ func TestKeysCommand_Run(t *testing.T) {

// Ensure the "get" command can print the value of a key in a bucket.
func TestGetCommand_Run(t *testing.T) {
db := btesting.MustCreateDB(t)
testCases := []struct {
name string
printable bool
testBucket string
testKey string
expectedValue string
}{
{
name: "printable data",
printable: true,
testBucket: "foo",
testKey: "foo-1",
expectedValue: "val-foo-1\n",
},
{
name: "non printable data",
printable: false,
testBucket: "bar",
testKey: "100001",
expectedValue: hex.EncodeToString(convertInt64IntoBytes(100001)) + "\n",
},
}

if err := db.Update(func(tx *bolt.Tx) error {
for _, name := range []string{"foo", "bar"} {
b, err := tx.CreateBucket([]byte(name))
if err != nil {
return err
}
for i := 0; i < 3; i++ {
key := fmt.Sprintf("%s-%d", name, i)
val := fmt.Sprintf("val-%s-%d", name, i)
if err := b.Put([]byte(key), []byte(val)); err != nil {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
db := btesting.MustCreateDB(t)

if err := db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucket([]byte(tc.testBucket))
if err != nil {
return err
}
if tc.printable {
val := fmt.Sprintf("val-%s", tc.testKey)
if err := b.Put([]byte(tc.testKey), []byte(val)); err != nil {
return err
}
} else {
if err := b.Put([]byte(tc.testKey), convertInt64IntoBytes(100001)); err != nil {
return err
}
}
return nil
}); err != nil {
t.Fatal(err)
}
}
return nil
}); err != nil {
t.Fatal(err)
}
db.Close()
db.Close()

defer requireDBNoChange(t, dbData(t, db.Path()), db.Path())

expected := "val-foo-1\n"
defer requireDBNoChange(t, dbData(t, db.Path()), db.Path())

// Run the command.
m := NewMain()
if err := m.Run("get", db.Path(), "foo", "foo-1"); err != nil {
t.Fatal(err)
} else if actual := m.Stdout.String(); actual != expected {
t.Fatalf("unexpected stdout:\n\n%s", actual)
// Run the command.
m := NewMain()
if err := m.Run("get", db.Path(), tc.testBucket, tc.testKey); err != nil {
t.Fatal(err)
}
actual := m.Stdout.String()
assert.Equal(t, tc.expectedValue, actual)
})
}
}

Expand Down
Loading