Skip to content

Commit cc22218

Browse files
authored
Merge pull request #728 from Elbehery/use_cobra_exactArgs
fix: use cobra exactArgs
2 parents 818cc9c + 2fe6b63 commit cc22218

4 files changed

+9
-84
lines changed

cmd/bbolt/command_inspect.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"os"
87

@@ -15,15 +14,7 @@ func newInspectCobraCommand() *cobra.Command {
1514
inspectCmd := &cobra.Command{
1615
Use: "inspect",
1716
Short: "inspect the structure of the database",
18-
Args: func(cmd *cobra.Command, args []string) error {
19-
if len(args) == 0 {
20-
return errors.New("db file path not provided")
21-
}
22-
if len(args) > 1 {
23-
return errors.New("too many arguments")
24-
}
25-
return nil
26-
},
17+
Args: cobra.ExactArgs(1),
2718
RunE: func(cmd *cobra.Command, args []string) error {
2819
return inspectFunc(args[0])
2920
},

cmd/bbolt/command_surgery.go

+4-36
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,7 @@ func newSurgeryRevertMetaPageCommand() *cobra.Command {
5454
revertMetaPageCmd := &cobra.Command{
5555
Use: "revert-meta-page <bbolt-file> [options]",
5656
Short: "Revert the meta page to revert the changes performed by the latest transaction",
57-
Args: func(cmd *cobra.Command, args []string) error {
58-
if len(args) == 0 {
59-
return errors.New("db file path not provided")
60-
}
61-
if len(args) > 1 {
62-
return errors.New("too many arguments")
63-
}
64-
return nil
65-
},
57+
Args: cobra.ExactArgs(1),
6658
RunE: func(cmd *cobra.Command, args []string) error {
6759
if err := o.Validate(); err != nil {
6860
return err
@@ -121,15 +113,7 @@ func newSurgeryCopyPageCommand() *cobra.Command {
121113
copyPageCmd := &cobra.Command{
122114
Use: "copy-page <bbolt-file> [options]",
123115
Short: "Copy page from the source page Id to the destination page Id",
124-
Args: func(cmd *cobra.Command, args []string) error {
125-
if len(args) == 0 {
126-
return errors.New("db file path not provided")
127-
}
128-
if len(args) > 1 {
129-
return errors.New("too many arguments")
130-
}
131-
return nil
132-
},
116+
Args: cobra.ExactArgs(1),
133117
RunE: func(cmd *cobra.Command, args []string) error {
134118
if err := o.Validate(); err != nil {
135119
return err
@@ -193,15 +177,7 @@ func newSurgeryClearPageCommand() *cobra.Command {
193177
clearPageCmd := &cobra.Command{
194178
Use: "clear-page <bbolt-file> [options]",
195179
Short: "Clears all elements from the given page, which can be a branch or leaf page",
196-
Args: func(cmd *cobra.Command, args []string) error {
197-
if len(args) == 0 {
198-
return errors.New("db file path not provided")
199-
}
200-
if len(args) > 1 {
201-
return errors.New("too many arguments")
202-
}
203-
return nil
204-
},
180+
Args: cobra.ExactArgs(1),
205181
RunE: func(cmd *cobra.Command, args []string) error {
206182
if err := o.Validate(); err != nil {
207183
return err
@@ -268,15 +244,7 @@ func newSurgeryClearPageElementsCommand() *cobra.Command {
268244
clearElementCmd := &cobra.Command{
269245
Use: "clear-page-elements <bbolt-file> [options]",
270246
Short: "Clears elements from the given page, which can be a branch or leaf page",
271-
Args: func(cmd *cobra.Command, args []string) error {
272-
if len(args) == 0 {
273-
return errors.New("db file path not provided")
274-
}
275-
if len(args) > 1 {
276-
return errors.New("too many arguments")
277-
}
278-
return nil
279-
},
247+
Args: cobra.ExactArgs(1),
280248
RunE: func(cmd *cobra.Command, args []string) error {
281249
if err := o.Validate(); err != nil {
282250
return err

cmd/bbolt/command_surgery_freelist.go

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"errors"
54
"fmt"
65
"os"
76

@@ -29,15 +28,7 @@ func newSurgeryFreelistAbandonCommand() *cobra.Command {
2928
abandonFreelistCmd := &cobra.Command{
3029
Use: "abandon <bbolt-file> [options]",
3130
Short: "Abandon the freelist from both meta pages",
32-
Args: func(cmd *cobra.Command, args []string) error {
33-
if len(args) == 0 {
34-
return errors.New("db file path not provided")
35-
}
36-
if len(args) > 1 {
37-
return errors.New("too many arguments")
38-
}
39-
return nil
40-
},
31+
Args: cobra.ExactArgs(1),
4132
RunE: func(cmd *cobra.Command, args []string) error {
4233
if err := o.Validate(); err != nil {
4334
return err
@@ -72,15 +63,7 @@ func newSurgeryFreelistRebuildCommand() *cobra.Command {
7263
rebuildFreelistCmd := &cobra.Command{
7364
Use: "rebuild <bbolt-file> [options]",
7465
Short: "Rebuild the freelist",
75-
Args: func(cmd *cobra.Command, args []string) error {
76-
if len(args) == 0 {
77-
return errors.New("db file path not provided")
78-
}
79-
if len(args) > 1 {
80-
return errors.New("too many arguments")
81-
}
82-
return nil
83-
},
66+
Args: cobra.ExactArgs(1),
8467
RunE: func(cmd *cobra.Command, args []string) error {
8568
if err := o.Validate(); err != nil {
8669
return err

cmd/bbolt/command_surgery_meta.go

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"errors"
54
"fmt"
65
"io"
76
"os"
@@ -37,15 +36,7 @@ func newSurgeryMetaValidateCommand() *cobra.Command {
3736
metaValidateCmd := &cobra.Command{
3837
Use: "validate <bbolt-file> [options]",
3938
Short: "Validate both meta pages",
40-
Args: func(cmd *cobra.Command, args []string) error {
41-
if len(args) == 0 {
42-
return errors.New("db file path not provided")
43-
}
44-
if len(args) > 1 {
45-
return errors.New("too many arguments")
46-
}
47-
return nil
48-
},
39+
Args: cobra.ExactArgs(1),
4940
RunE: func(cmd *cobra.Command, args []string) error {
5041
return surgeryMetaValidateFunc(args[0])
5142
},
@@ -131,15 +122,7 @@ func newSurgeryMetaUpdateCommand() *cobra.Command {
131122
metaUpdateCmd := &cobra.Command{
132123
Use: "update <bbolt-file> [options]",
133124
Short: "Update fields in meta pages",
134-
Args: func(cmd *cobra.Command, args []string) error {
135-
if len(args) == 0 {
136-
return errors.New("db file path not provided")
137-
}
138-
if len(args) > 1 {
139-
return errors.New("too many arguments")
140-
}
141-
return nil
142-
},
125+
Args: cobra.ExactArgs(1),
143126
RunE: func(cmd *cobra.Command, args []string) error {
144127
if err := o.Validate(); err != nil {
145128
return err

0 commit comments

Comments
 (0)