Skip to content

Commit 25dacb3

Browse files
committed
fix cli cmds panics
Signed-off-by: Mustafa Elbehery <[email protected]>
1 parent e102fcf commit 25dacb3

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

cmd/bbolt/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ var (
6060

6161
// ErrKeyNotFound is returned when a key is not found.
6262
ErrKeyNotFound = errors.New("key not found")
63+
64+
// ErrNotEnoughArgs is returned with a cmd is being executed with fewer arguments.
65+
ErrNotEnoughArgs = errors.New("not enough arguments")
6366
)
6467

6568
func main() {
@@ -908,6 +911,9 @@ func (cmd *keysCommand) Run(args ...string) error {
908911

909912
// Require database path and bucket.
910913
relevantArgs := fs.Args()
914+
if len(relevantArgs) < 2 {
915+
return ErrNotEnoughArgs
916+
}
911917
path, buckets := relevantArgs[0], relevantArgs[1:]
912918
if path == "" {
913919
return ErrPathRequired
@@ -993,6 +999,9 @@ func (cmd *getCommand) Run(args ...string) error {
993999

9941000
// Require database path, bucket and key.
9951001
relevantArgs := fs.Args()
1002+
if len(relevantArgs) < 3 {
1003+
return ErrNotEnoughArgs
1004+
}
9961005
path, buckets := relevantArgs[0], relevantArgs[1:len(relevantArgs)-1]
9971006
key, err := parseBytes(relevantArgs[len(relevantArgs)-1], parseFormat)
9981007
if err != nil {

cmd/bbolt/main_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,33 @@ func TestCompactCommand_Run(t *testing.T) {
397397
}
398398
}
399399

400+
func TestCommands_Run_NoArgs(t *testing.T) {
401+
testCases := []struct {
402+
name string
403+
cmd string
404+
expErr error
405+
}{
406+
{
407+
name: "get",
408+
cmd: "get",
409+
expErr: main.ErrNotEnoughArgs,
410+
},
411+
{
412+
name: "keys",
413+
cmd: "keys",
414+
expErr: main.ErrNotEnoughArgs,
415+
},
416+
}
417+
418+
for _, tc := range testCases {
419+
t.Run(tc.name, func(t *testing.T) {
420+
m := NewMain()
421+
err := m.Run(tc.cmd)
422+
require.ErrorIs(t, err, main.ErrNotEnoughArgs)
423+
})
424+
}
425+
}
426+
400427
func fillBucket(b *bolt.Bucket, prefix []byte) error {
401428
n := 10 + rand.Intn(50)
402429
for i := 0; i < n; i++ {

0 commit comments

Comments
 (0)