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

Extract `func (c *CommandArgumentValidator) validate(args []strin… #86

Merged
merged 26 commits into from
Nov 5, 2019

Conversation

tangrufus
Copy link
Member

  • simplify argument count validation logic
  • normalize argument count error message

Example usage:

# $ trellis deploy [options] ENVIRONMENT [SITE]

args = c.flags.Args()

argCountErr := validateArgumentCount(args, 1, 1)
if argCountErr != nil {
	c.UI.Error(argCountErr.Error())
	c.UI.Output(c.Help())
	return 1
}

environment := args[0]

siteName := ""
if len(args) == 2 {
	siteName = args[1]
}

Note: validateArgumentCount doesn't support unlimited arguments, i.e:

trellis-cli/cmd/exec.go

Lines 28 to 36 in c043cfd

switch len(args) {
case 0:
c.UI.Error("Error: missing COMMAND argument\n")
c.UI.Output(c.Help())
return 1
default:
command = args[0]
cmdArgs = args
}

cmd/alias.go Outdated
default:
c.UI.Error(fmt.Sprintf("Error: too many arguments (expected 0, got %d)\n", len(args)))
argCountErr := validateArgumentCount(args, 0, 0)
if argCountErr != nil {
Copy link
Member

@swalkinshaw swalkinshaw Nov 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we made this a struct with methods too? It could look something like:

type CommandArgumentValidator struct {
  args []string{}
  required Int
  optional Int
  // or maybe initialize with UI too
}

func (c *CommandArgumentValidator) validate(ui cli.Ui) int {
  // etc
  ui.Error(argCountErr.Error())
  ui.Output(c.Help())
  return 1
}

// in a command

validator := CommandArgumentValidator{args: args, required: 0, optional: 0}
validator.validate(c.UI)

One reason I like this is for the named parameters

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Usage:

commandArgumentValidator := &CommandArgumentValidator{required: 1, optional: 1}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
	c.UI.Error(commandArgumentErr.Error())
	c.UI.Output(c.Help())
	return 1
}

To keep CommandArgumentValidator simple., I am not passing c.UI & c.Help() to CommandArgumentValidator.

Polluting CommandArgumentValidator with c.UI & c.Help() to make 2 less lines of code doesn't worth and looks like a violation of single responsibility principle to me. :

commandArgumentValidator := &CommandArgumentValidator{required: 1, optional: 1, UI: c.UI, Help: c.Help()}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
	return 1
}

What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I agree 👍

@tangrufus tangrufus changed the title Extract validateArgumentCount Extract func (c *DotEnvCommand) Run(args []string) int Nov 4, 2019
@tangrufus tangrufus changed the title Extract func (c *DotEnvCommand) Run(args []string) int Extract func (c *CommandArgumentValidator) validate(args []string) (err error) Nov 4, 2019

expectedCount := fmt.Sprintf("exactly %d", c.required)
if (c.optional > 0) {
expectedCount = fmt.Sprintf("between %d and %d", c.required, c.required+c.optional)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just declare totalArgs := c.required+c.optional up front?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

if argCount > c.required+c.optional {
err = fmt.Errorf("Error: too many arguments (expected %s, got %d)\n", expectedCount, len(args))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can re-use argCount in both places here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

err = fmt.Errorf("Error: missing arguments (expected %s, got %d)\n", expectedCount, len(args))
}

return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 does it complain if you explicitly do return err? Makes more sense to me if not (even though this works)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return fmt.Errorf("Error: too many arguments (expected %s, got %d)\n", expectedCount, len(args))
}
if argCount < c.required {
return fmt.Errorf("Error: missing arguments (expected %s, got %d)\n", expectedCount, len(args))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I meant you can use argCount instead of len(args) in both these error messages?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i need coffee

@tangrufus tangrufus changed the title Extract func (c *CommandArgumentValidator) validate(args []string) (err error) Extract `func (c *CommandArgumentValidator) validate(args []strin… Nov 5, 2019
@tangrufus tangrufus merged commit fde6ff2 into roots:master Nov 5, 2019
@tangrufus tangrufus deleted the args-validation branch November 5, 2019 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants