Skip to content

Commit

Permalink
feat(core): handle map in request arguments (#1569)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sh4d1 authored Nov 27, 2020
1 parent 0aba4db commit f2614ad
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion internal/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// validArgNameRegex regex to check that args words are lower-case or digit starting and ending with a letter.
var validArgNameRegex = regexp.MustCompile(`^([a-z][a-z0-9-]*)(\.[a-z0-9-]*)*$`)
var validArgNameRegex = regexp.MustCompile(`^[a-z][a-z0-9-]*$`)

const emptySliceValue = "none"

Expand Down
27 changes: 7 additions & 20 deletions internal/args/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/karrick/tparse"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/scaleway-sdk-go/strcase"
"github.com/scaleway/scaleway-sdk-go/validation"
)

type Unmarshaler interface {
Expand Down Expand Up @@ -123,25 +122,6 @@ func UnmarshalStruct(args []string, data interface{}) error {
argName, argValue := kv[0], kv[1]
argNameWords := strings.Split(argName, ".")

// Make sure argument name is correct.
// We enforce this check to avoid not well formatted argument name to work by "accident"
// as we use ToPublicGoName on the argument name later on.
if !validArgNameRegex.MatchString(argName) {
err := error(&InvalidArgNameError{})

// Make an exception for users that try to pass resource UUID without corresponding ID argument.
// TODO: return a special error to advice user to use the ID argument.
if validation.IsUUID(argName) {
err = &UnknownArgError{}
}

return &UnmarshalArgError{
ArgName: argName,
ArgValue: argValue,
Err: err,
}
}

if processedArgNames[argName] {
return &UnmarshalArgError{
ArgName: argName,
Expand Down Expand Up @@ -325,6 +305,13 @@ func set(dest reflect.Value, argNameWords []string, value string) error {
}
}

// Make sure argument name is correct.
// We enforce this check to avoid not well formatted argument name to work by "accident"
// as we use ToPublicGoName on the argument name later on.
if !validArgNameRegex.MatchString(argNameWords[0]) {
return error(&InvalidArgNameError{})
}

// Try to find the correct field in the current struct.
fieldName := strcase.ToPublicGoName(argNameWords[0])
if fieldIndex, exist := fieldIndexByName[fieldName]; exist {
Expand Down
12 changes: 11 additions & 1 deletion internal/core/cobra_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,18 @@ Relative time error: %s
Err: fmt.Errorf("invalid value for '%s' argument: %s", unmarshalErr.ArgName, e.Err),
}
}
case *args.InvalidArgNameError:
argNames := []string(nil)
nonDeprecatedArgs := cmd.ArgSpecs.GetDeprecated(false)
for _, argSpec := range nonDeprecatedArgs {
argNames = append(argNames, argSpec.Name)
}

case *args.UnknownArgError, *args.InvalidArgNameError:
return &CliError{
Err: fmt.Errorf("invalid argument '%s': %s", unmarshalErr.ArgName, e.Error()),
Hint: fmt.Sprintf("Valid arguments are: %s", strings.Join(argNames, ", ")),
}
case *args.UnknownArgError:
argNames := []string(nil)
nonDeprecatedArgs := cmd.ArgSpecs.GetDeprecated(false)
for _, argSpec := range nonDeprecatedArgs {
Expand Down
4 changes: 2 additions & 2 deletions internal/core/cobra_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func Test_handleUnmarshalErrors(t *testing.T) {
Check: TestCheckCombine(
TestCheckExitCode(1),
TestCheckError(&CliError{
Err: fmt.Errorf("unknown argument 'name_id'"),
Err: fmt.Errorf("invalid argument 'name_id': arg name must only contain lowercase letters, numbers or dashes"),
Hint: "Valid arguments are: name-id",
}),
),
Expand All @@ -101,7 +101,7 @@ func Test_handleUnmarshalErrors(t *testing.T) {
Check: TestCheckCombine(
TestCheckExitCode(1),
TestCheckError(&CliError{
Err: fmt.Errorf("unknown argument 'ubuntu_focal'"),
Err: fmt.Errorf("invalid argument 'ubuntu_focal': arg name must only contain lowercase letters, numbers or dashes"),
Hint: "Valid arguments are: name-id",
}),
),
Expand Down

0 comments on commit f2614ad

Please sign in to comment.