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

feat(core): handle map in request arguments #1569

Merged
merged 4 commits into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
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]) {
Sh4d1 marked this conversation as resolved.
Show resolved Hide resolved
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
3 changes: 3 additions & 0 deletions internal/core/arg_specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ type ArgSpec struct {
// Only one argument of the same OneOfGroup could be specified
OneOfGroup string

// CanUseFile defines whether the arg can be loaded as a file with `@`
CanUseFile bool
Sh4d1 marked this conversation as resolved.
Show resolved Hide resolved

// Deprecated is used to flag an argument as deprecated.
// Use the short field to indicate migration tips for users.
Deprecated bool
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