Skip to content

Commit

Permalink
Extract `func (c *CommandArgumentValidator) validate(args []string) (…
Browse files Browse the repository at this point in the history
…err error)`
  • Loading branch information
tangrufus committed Nov 4, 2019
1 parent 5bcfd42 commit 1427619
Show file tree
Hide file tree
Showing 23 changed files with 111 additions and 86 deletions.
7 changes: 4 additions & 3 deletions cmd/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ func (c *AliasCommand) Run(args []string) int {

args = c.flags.Args()

argCountErr := validateArgumentCount(args, 0, 0)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
commandArgumentValidator := &CommandArgumentValidator{required: 0, optional: 0}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ var Requirements = []trellis.Requirement{
}

func (c *CheckCommand) Run(args []string) int {
argCountErr := validateArgumentCount(args, 0, 0)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
commandArgumentValidator := &CommandArgumentValidator{required: 0, optional: 0}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}
Expand Down
25 changes: 25 additions & 0 deletions cmd/command_argument_validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import "fmt"

type CommandArgumentValidator struct {
required int
optional int
}

func (c *CommandArgumentValidator) validate(args []string) (err error) {
argCount := len(args)

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

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

return
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

func TestValidateArgumentCount(t *testing.T) {
func TestCommandArgumentValidatorValidate(t *testing.T) {
cases := []struct {
name string
args []string
Expand Down Expand Up @@ -58,7 +58,12 @@ func TestValidateArgumentCount(t *testing.T) {
}

for _, tc := range cases {
actual := validateArgumentCount(tc.args, tc.requiredArgCount, tc.optionalArgCount)
validator := &CommandArgumentValidator{
required: tc.requiredArgCount,
optional: tc.optionalArgCount,
}

actual := validator.validate(tc.args)

if "" == tc.expectedMessage && actual != nil {
t.Errorf("expected result to be valid, got %s", actual)
Expand Down
7 changes: 4 additions & 3 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ func (c *DeployCommand) Run(args []string) int {

args = c.flags.Args()

argCountErr := validateArgumentCount(args, 1, 1)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
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
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/dot_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ func (c *DotEnvCommand) Run(args []string) int {
return 1
}

argCountErr := validateArgumentCount(args, 0, 1)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
commandArgumentValidator := &CommandArgumentValidator{required: 0, optional: 1}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ func (c *DownCommand) Run(args []string) int {
return 1
}

argCountErr := validateArgumentCount(args, 0, 0)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
commandArgumentValidator := &CommandArgumentValidator{required: 0, optional: 0}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/droplet_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ func (c *DropletCreateCommand) Run(args []string) int {

args = c.flags.Args()

argCountErr := validateArgumentCount(args, 0, 1)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
commandArgumentValidator := &CommandArgumentValidator{required: 0, optional: 1}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/galaxy_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ func (c *GalaxyInstallCommand) Run(args []string) int {
return 1
}

argCountErr := validateArgumentCount(args, 0, 0)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
commandArgumentValidator := &CommandArgumentValidator{required: 0, optional: 0}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ func (c *InfoCommand) Run(args []string) int {
return 1
}

argCountErr := validateArgumentCount(args, 0, 0)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
commandArgumentValidator := &CommandArgumentValidator{required: 0, optional: 0}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ func (c *InitCommand) Run(args []string) int {
return 1
}

argCountErr := validateArgumentCount(args, 0, 0)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
commandArgumentValidator := &CommandArgumentValidator{required: 0, optional: 0}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}
Expand Down
15 changes: 6 additions & 9 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,16 @@ func (c *NewCommand) Run(args []string) int {

args = c.flags.Args()

switch len(args) {
case 0:
c.UI.Error("Error: missing PATH argument\n")
c.UI.Output(c.Help())
return 1
case 1:
path = args[0]
default:
c.UI.Error(fmt.Sprintf("Error: too many arguments (expected 1, got %d)\n", len(args)))
commandArgumentValidator := &CommandArgumentValidator{required: 1, optional: 0}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}

path = args[0]

path, _ = filepath.Abs(path)
fi, statErr := os.Stat(path)

Expand Down
2 changes: 1 addition & 1 deletion cmd/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestNewRunValidations(t *testing.T) {
"no_args",
false,
nil,
"Error: missing PATH argument",
"Error: missing arguments (expected exactly 1, got 0)",
1,
},
{
Expand Down
7 changes: 4 additions & 3 deletions cmd/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ func (c *ProvisionCommand) Run(args []string) int {

args = c.flags.Args()

argCountErr := validateArgumentCount(args, 1, 0)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
commandArgumentValidator := &CommandArgumentValidator{required: 1, optional: 0}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ func (c *RollbackCommand) Run(args []string) int {

args = c.flags.Args()

argCountErr := validateArgumentCount(args, 1, 1)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
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
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ func (c *SshCommand) Run(args []string) int {
return 1
}

argCountErr := validateArgumentCount(args, 1, 1)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
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
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ func (c *UpCommand) Run(args []string) int {

args = c.flags.Args()

argCountErr := validateArgumentCount(args, 0, 0)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
commandArgumentValidator := &CommandArgumentValidator{required: 0, optional: 0}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/valet_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ func (c *ValetLinkCommand) Run(args []string) int {
return 1
}

argCountErr := validateArgumentCount(args, 0, 1)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
commandArgumentValidator := &CommandArgumentValidator{required: 0, optional: 1}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}
Expand Down
20 changes: 0 additions & 20 deletions cmd/validate_argument_count.go

This file was deleted.

7 changes: 4 additions & 3 deletions cmd/vault_decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ func (c *VaultDecryptCommand) Run(args []string) int {

args = c.flags.Args()

argCountErr := validateArgumentCount(args, 1, 0)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
commandArgumentValidator := &CommandArgumentValidator{required: 1, optional: 0}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/vault_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ func (c *VaultEditCommand) Run(args []string) int {
return 1
}

argCountErr := validateArgumentCount(args, 1, 0)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
commandArgumentValidator := &CommandArgumentValidator{required: 1, optional: 0}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/vault_encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ func (c *VaultEncryptCommand) Run(args []string) int {

args = c.flags.Args()

argCountErr := validateArgumentCount(args, 1, 0)
if argCountErr != nil {
c.UI.Error(argCountErr.Error())
commandArgumentValidator := &CommandArgumentValidator{required: 1, optional: 0}
commandArgumentErr := commandArgumentValidator.validate(args)
if commandArgumentErr != nil {
c.UI.Error(commandArgumentErr.Error())
c.UI.Output(c.Help())
return 1
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/vault_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ func (c *VaultViewCommand) Run(args []string) int {

args = c.flags.Args()

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

0 comments on commit 1427619

Please sign in to comment.