mirror of https://github.com/status-im/consul.git
fix: provide meaningful error messages and add test (#18772)
* fix: provide meaningful error messages and add test * fix: return error instead of warning when extra args are provided
This commit is contained in:
parent
0018b7e5a8
commit
bf4e0b1aa9
|
@ -85,7 +85,7 @@ func (c *cmd) Run(args []string) int {
|
|||
}
|
||||
parsedResource = data
|
||||
} else {
|
||||
c.UI.Error("Flag -f is required")
|
||||
c.UI.Error("Incorrect argument format: Flag -f with file path argument is required")
|
||||
return 1
|
||||
}
|
||||
|
||||
|
|
|
@ -78,7 +78,7 @@ func TestResourceApplyInvalidArgs(t *testing.T) {
|
|||
"missing required flag": {
|
||||
args: []string{},
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("Flag -f is required"),
|
||||
expectedErr: errors.New("Incorrect argument format: Flag -f with file path argument is required"),
|
||||
},
|
||||
"file parsing failure": {
|
||||
args: []string{"-f=../testdata/invalid.hcl"},
|
||||
|
|
|
@ -84,13 +84,13 @@ func (c *cmd) Run(args []string) int {
|
|||
}
|
||||
} else {
|
||||
if len(args) < 2 {
|
||||
c.UI.Error("Your argument format is incorrect: Must specify two arguments: resource type and resource name")
|
||||
c.UI.Error("Incorrect argument format: Must specify two arguments: resource type and resource name")
|
||||
return 1
|
||||
}
|
||||
var err error
|
||||
gvk, resourceName, err = resource.GetTypeAndResourceName(args)
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Sprintf("Your argument format is incorrect: %s", err))
|
||||
c.UI.Error(fmt.Sprintf("Incorrect argument format: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
|
@ -101,7 +101,8 @@ func (c *cmd) Run(args []string) int {
|
|||
return 1
|
||||
}
|
||||
if c.filePath != "" {
|
||||
c.UI.Warn("We ignored the -f flag if you provide gvk and resource name")
|
||||
c.UI.Error("Incorrect argument format: File argument is not needed when resource information is provided with the command")
|
||||
return 1
|
||||
}
|
||||
opts = &api.QueryOptions{
|
||||
Namespace: c.http.Namespace(),
|
||||
|
|
|
@ -27,12 +27,12 @@ func TestResourceDeleteInvalidArgs(t *testing.T) {
|
|||
"nil args": {
|
||||
args: nil,
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("Your argument format is incorrect: Must specify two arguments: resource type and resource name"),
|
||||
expectedErr: errors.New("Incorrect argument format: Must specify two arguments: resource type and resource name"),
|
||||
},
|
||||
"empty args": {
|
||||
args: []string{},
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("Your argument format is incorrect: Must specify two arguments: resource type and resource name"),
|
||||
expectedErr: errors.New("Incorrect argument format: Must specify two arguments: resource type and resource name"),
|
||||
},
|
||||
"missing file path": {
|
||||
args: []string{"-f"},
|
||||
|
@ -47,27 +47,27 @@ func TestResourceDeleteInvalidArgs(t *testing.T) {
|
|||
"provide type and name": {
|
||||
args: []string{"a.b.c"},
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("Your argument format is incorrect: Must specify two arguments: resource type and resource name"),
|
||||
expectedErr: errors.New("Incorrect argument format: Must specify two arguments: resource type and resource name"),
|
||||
},
|
||||
"provide type and name with -f": {
|
||||
args: []string{"a.b.c", "name", "-f", "test.hcl"},
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("We ignored the -f flag if you provide gvk and resource name"),
|
||||
expectedErr: errors.New("Incorrect argument format: File argument is not needed when resource information is provided with the command"),
|
||||
},
|
||||
"provide type and name with -f and other flags": {
|
||||
args: []string{"a.b.c", "name", "-f", "test.hcl", "-namespace", "default"},
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("We ignored the -f flag if you provide gvk and resource name"),
|
||||
expectedErr: errors.New("Incorrect argument format: File argument is not needed when resource information is provided with the command"),
|
||||
},
|
||||
"does not provide resource name after type": {
|
||||
args: []string{"a.b.c", "-namespace", "default"},
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("Your argument format is incorrect: Must provide resource name right after type"),
|
||||
expectedErr: errors.New("Incorrect argument format: Must provide resource name right after type"),
|
||||
},
|
||||
"invalid resource type format": {
|
||||
args: []string{"a.", "name", "-namespace", "default"},
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("Your argument format is incorrect: Must include resource type argument in group.verion.kind format"),
|
||||
expectedErr: errors.New("Incorrect argument format: Must include resource type argument in group.verion.kind format"),
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -47,11 +47,6 @@ func (c *cmd) Run(args []string) int {
|
|||
var gvk *api.GVK
|
||||
var opts *api.QueryOptions
|
||||
|
||||
if len(args) == 0 {
|
||||
c.UI.Error("Please provide required arguments")
|
||||
return 1
|
||||
}
|
||||
|
||||
if err := c.flags.Parse(args); err != nil {
|
||||
if !errors.Is(err, flag.ErrHelp) {
|
||||
c.UI.Error(fmt.Sprintf("Failed to parse args: %v", err))
|
||||
|
@ -93,7 +88,7 @@ func (c *cmd) Run(args []string) int {
|
|||
// extract resource type
|
||||
gvk, err = getResourceType(c.flags.Args())
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Sprintf("Your argument format is incorrect: %v", err))
|
||||
c.UI.Error(fmt.Sprintf("Incorrect argument format: %v", err))
|
||||
return 1
|
||||
}
|
||||
// skip resource type to parse remaining args
|
||||
|
@ -104,7 +99,8 @@ func (c *cmd) Run(args []string) int {
|
|||
return 1
|
||||
}
|
||||
if c.filePath != "" {
|
||||
c.UI.Warn(fmt.Sprintf("File argument is ignored when resource definition is provided with the command"))
|
||||
c.UI.Error("Incorrect argument format: File argument is not needed when resource information is provided with the command")
|
||||
return 1
|
||||
}
|
||||
|
||||
opts = &api.QueryOptions{
|
||||
|
@ -142,6 +138,10 @@ func getResourceType(args []string) (gvk *api.GVK, e error) {
|
|||
if len(args) < 1 {
|
||||
return nil, fmt.Errorf("Must include resource type argument")
|
||||
}
|
||||
// it should not have resource name
|
||||
if len(args) > 1 && !strings.HasPrefix(args[1], "-") {
|
||||
return nil, fmt.Errorf("Must include flag arguments after resource type")
|
||||
}
|
||||
|
||||
s := strings.Split(args[0], ".")
|
||||
if len(s) < 3 {
|
||||
|
|
|
@ -99,12 +99,12 @@ func TestResourceListInvalidArgs(t *testing.T) {
|
|||
"nil args": {
|
||||
args: nil,
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("Please provide required arguments"),
|
||||
expectedErr: errors.New("Incorrect argument format: Must include resource type argument"),
|
||||
},
|
||||
"minimum args required": {
|
||||
args: []string{},
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("Please provide required arguments"),
|
||||
expectedErr: errors.New("Incorrect argument format: Must include resource type argument"),
|
||||
},
|
||||
"no file path": {
|
||||
args: []string{
|
||||
|
@ -127,7 +127,7 @@ func TestResourceListInvalidArgs(t *testing.T) {
|
|||
expectedCode: 1,
|
||||
expectedErr: errors.New("Failed to decode resource from input file"),
|
||||
},
|
||||
"file argument is ignored": {
|
||||
"file argument with resource type": {
|
||||
args: []string{
|
||||
"demo.v2.artist",
|
||||
"-namespace=default",
|
||||
|
@ -137,8 +137,8 @@ func TestResourceListInvalidArgs(t *testing.T) {
|
|||
"-token=root",
|
||||
"-f=demo.hcl",
|
||||
},
|
||||
expectedCode: 0,
|
||||
expectedErr: errors.New("File argument is ignored when resource definition is provided with the command"),
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("Incorrect argument format: File argument is not needed when resource information is provided with the command"),
|
||||
},
|
||||
"resource type invalid": {
|
||||
args: []string{
|
||||
|
@ -150,6 +150,17 @@ func TestResourceListInvalidArgs(t *testing.T) {
|
|||
expectedCode: 1,
|
||||
expectedErr: errors.New("Must include resource type argument in group.verion.kind format"),
|
||||
},
|
||||
"resource name is provided": {
|
||||
args: []string{
|
||||
"demo.v2.artist",
|
||||
"test",
|
||||
"-namespace=default",
|
||||
"-peer=local",
|
||||
"-partition=default",
|
||||
},
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("Must include flag arguments after resource type"),
|
||||
},
|
||||
}
|
||||
|
||||
for desc, tc := range cases {
|
||||
|
|
|
@ -86,13 +86,13 @@ func (c *cmd) Run(args []string) int {
|
|||
}
|
||||
} else {
|
||||
if len(args) < 2 {
|
||||
c.UI.Error("Your argument format is incorrect: Must specify two arguments: resource type and resource name")
|
||||
c.UI.Error("Incorrect argument format: Must specify two arguments: resource type and resource name")
|
||||
return 1
|
||||
}
|
||||
var err error
|
||||
gvk, resourceName, err = resource.GetTypeAndResourceName(args)
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Sprintf("Your argument format is incorrect: %s", err))
|
||||
c.UI.Error(fmt.Sprintf("Incorrect argument format: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
|
@ -103,7 +103,8 @@ func (c *cmd) Run(args []string) int {
|
|||
return 1
|
||||
}
|
||||
if c.filePath != "" {
|
||||
c.UI.Warn("We ignored the -f flag if you provide gvk and resource name")
|
||||
c.UI.Error("Incorrect argument format: File argument is not needed when resource information is provided with the command")
|
||||
return 1
|
||||
}
|
||||
opts = &api.QueryOptions{
|
||||
Namespace: c.http.Namespace(),
|
||||
|
|
|
@ -27,12 +27,12 @@ func TestResourceReadInvalidArgs(t *testing.T) {
|
|||
"nil args": {
|
||||
args: nil,
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("Your argument format is incorrect: Must specify two arguments: resource type and resource name"),
|
||||
expectedErr: errors.New("Incorrect argument format: Must specify two arguments: resource type and resource name"),
|
||||
},
|
||||
"empty args": {
|
||||
args: []string{},
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("Your argument format is incorrect: Must specify two arguments: resource type and resource name"),
|
||||
expectedErr: errors.New("Incorrect argument format: Must specify two arguments: resource type and resource name"),
|
||||
},
|
||||
"missing file path": {
|
||||
args: []string{"-f"},
|
||||
|
@ -47,27 +47,27 @@ func TestResourceReadInvalidArgs(t *testing.T) {
|
|||
"provide type and name": {
|
||||
args: []string{"a.b.c"},
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("Your argument format is incorrect: Must specify two arguments: resource type and resource name"),
|
||||
expectedErr: errors.New("Incorrect argument format: Must specify two arguments: resource type and resource name"),
|
||||
},
|
||||
"provide type and name with -f": {
|
||||
args: []string{"a.b.c", "name", "-f", "test.hcl"},
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("We ignored the -f flag if you provide gvk and resource name"),
|
||||
expectedErr: errors.New("Incorrect argument format: File argument is not needed when resource information is provided with the command"),
|
||||
},
|
||||
"provide type and name with -f and other flags": {
|
||||
args: []string{"a.b.c", "name", "-f", "test.hcl", "-namespace", "default"},
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("We ignored the -f flag if you provide gvk and resource name"),
|
||||
expectedErr: errors.New("Incorrect argument format: File argument is not needed when resource information is provided with the command"),
|
||||
},
|
||||
"does not provide resource name after type": {
|
||||
args: []string{"a.b.c", "-namespace", "default"},
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("Your argument format is incorrect: Must provide resource name right after type"),
|
||||
expectedErr: errors.New("Incorrect argument format: Must provide resource name right after type"),
|
||||
},
|
||||
"invalid resource type format": {
|
||||
args: []string{"a.", "name", "-namespace", "default"},
|
||||
expectedCode: 1,
|
||||
expectedErr: errors.New("Your argument format is incorrect: Must include resource type argument in group.verion.kind format"),
|
||||
expectedErr: errors.New("Incorrect argument format: Must include resource type argument in group.verion.kind format"),
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue