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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
591e0d0
Add `validateArgumentCount`
tangrufus Nov 3, 2019
12308d5
AliasCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
ca4ab0f
CheckCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
a1e20a9
DeployCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
e5212b3
DotEnvCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
6b3c52c
DownCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
12a9d5d
DropletCreateCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
4245ee3
GalaxyInstallCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
58d6bdd
InfoCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
1b8feb5
InitCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
aa081b4
ProvisionCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
b21ec54
ProvisionCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
1f79293
RollbackCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
a88de7a
Apply code style
tangrufus Nov 3, 2019
ad7193a
SshCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
0234c6b
UpCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
0ae2da6
ValetLinkCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
365e05e
VaultDecryptCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
415afa3
VaultEditCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
9f23f8f
VaultEncryptCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
5bcfd42
VaultViewCommand: Use `validateArgumentCount`
tangrufus Nov 3, 2019
1427619
Extract `func (c *CommandArgumentValidator) validate(args []string) (…
tangrufus Nov 4, 2019
bc981c6
Apply code style
tangrufus Nov 4, 2019
34e8688
Declare `totalArgs := c.required + c.optional` up front
tangrufus Nov 4, 2019
9656455
Remove naked return
tangrufus Nov 4, 2019
2c833c8
Use `argCount`
tangrufus Nov 4, 2019
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
8 changes: 4 additions & 4 deletions cmd/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func (c *AliasCommand) Run(args []string) int {

args = c.flags.Args()

switch len(args) {
case 0:
default:
c.UI.Error(fmt.Sprintf("Error: too many arguments (expected 0, got %d)\n", len(args)))
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
5 changes: 4 additions & 1 deletion cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ var Requirements = []trellis.Requirement{
}

func (c *CheckCommand) Run(args []string) int {
if len(args) > 0 {
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
27 changes: 27 additions & 0 deletions cmd/command_argument_validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import "fmt"

type CommandArgumentValidator struct {
required int
optional int
}

func (c *CommandArgumentValidator) validate(args []string) (err error) {
argCount := len(args)
totalArgs := c.required + c.optional

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

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

return nil
}
82 changes: 82 additions & 0 deletions cmd/command_argument_validator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package cmd

import (
"strings"
"testing"
)

func TestCommandArgumentValidatorValidate(t *testing.T) {
cases := []struct {
name string
args []string
requiredArgCount int
optionalArgCount int
expectedMessage string
}{
{
"valid_without_optional",
[]string{"a", "b", "c"},
3,
0,
"",
},
{
"valid_with_optional",
[]string{"a", "b", "c"},
1,
2,
"",
},
{
"missing_without_optional",
[]string{"a", "b", "c"},
4,
0,
"missing arguments (expected exactly 4, got 3)",
},
{
"missing_with_optional",
[]string{"a", "b", "c"},
4,
2,
"missing arguments (expected between 4 and 6, got 3)",
},
{
"too_many_without_optional",
[]string{"a", "b", "c"},
2,
0,
"too many arguments (expected exactly 2, got 3)",
},
{
"too_many_with_optional",
[]string{"a", "b", "c"},
1,
1,
"too many arguments (expected between 1 and 2, got 3)",
},
}

for _, tc := range cases {
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)
}

if "" != tc.expectedMessage && actual == nil {
t.Errorf("expected result to be invalid, got nil")
}

if "" != tc.expectedMessage && actual != nil {
if !strings.Contains(actual.Error(), tc.expectedMessage) {
t.Errorf("expected error to contains %s, got %s", tc.expectedMessage, actual)
}
}
}
}
23 changes: 10 additions & 13 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,25 @@ func (c *DeployCommand) Run(args []string) int {
return 1
}

var environment string
var siteName string

if err := c.flags.Parse(args); err != nil {
return 1
}

args = c.flags.Args()

switch len(args) {
case 0:
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
case 1:
environment = args[0]
case 2:
environment = args[0]
}

environment := args[0]

siteName := ""
if len(args) == 2 {
siteName = args[1]
default:
c.UI.Error(fmt.Sprintf("Error: too many arguments (expected 2, got %d)\n", len(args)))
c.UI.Output(c.Help())
return 1
}

_, ok := c.Trellis.Environments[environment]
Expand Down
18 changes: 9 additions & 9 deletions cmd/dot_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ func (c *DotEnvCommand) Run(args []string) int {
return 1
}

var environment string

switch len(args) {
case 0:
environment = "development"
case 1:
environment = args[0]
default:
c.UI.Error(fmt.Sprintf("Error: too many arguments (expected 0 or 1, got %d)\n", len(args)))
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
}

environment := "development"
if len(args) == 1 {
environment = args[0]
}

_, ok := c.Trellis.Environments[environment]
if !ok {
c.UI.Error(fmt.Sprintf("Error: %s is not a valid environment", environment))
Expand Down
9 changes: 4 additions & 5 deletions cmd/down.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"strings"

"trellis-cli/trellis"
Expand All @@ -20,10 +19,10 @@ func (c *DownCommand) Run(args []string) int {
return 1
}

switch len(args) {
case 0:
default:
c.UI.Error(fmt.Sprintf("Error: too many arguments (expected 0, got %d)\n", len(args)))
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
16 changes: 6 additions & 10 deletions cmd/droplet_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,22 @@ func (c *DropletCreateCommand) Run(args []string) int {
return 1
}

var environment string

if err := c.flags.Parse(args); err != nil {
return 1
}

args = c.flags.Args()

switch len(args) {
case 0:
c.UI.Output(c.Help())
return 1
case 1:
environment = args[0]
default:
c.UI.Error(fmt.Sprintf("Error: too many arguments (expected 1, got %d)\n", len(args)))
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
}

environment := args[0]

if environment == "development" {
c.UI.Error("create command only supports staging/production environments")
return 1
Expand Down
6 changes: 4 additions & 2 deletions cmd/galaxy_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ func (c *GalaxyInstallCommand) Run(args []string) int {
return 1
}

if len(args) > 0 {
c.UI.Error(fmt.Sprintf("Error: too many arguments (expected 0, got %d)\n", len(args)))
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
8 changes: 8 additions & 0 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ func (c *InfoCommand) Run(args []string) int {
return 1
}

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
}

for name, config := range c.Trellis.Environments {
var siteNames []string

Expand Down
8 changes: 4 additions & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func (c *InitCommand) Run(args []string) int {
return 1
}

switch len(args) {
case 0:
default:
c.UI.Error(fmt.Sprintf("Error: too many arguments (expected 0, got %d)\n", len(args)))
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
17 changes: 6 additions & 11 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,22 @@ func (c *NewCommand) init() {
}

func (c *NewCommand) Run(args []string) int {
var path string

if err := c.flags.Parse(args); err != nil {
return 1
}

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
17 changes: 6 additions & 11 deletions cmd/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,22 @@ func (c *ProvisionCommand) Run(args []string) int {
return 1
}

var environment string

if err := c.flags.Parse(args); err != nil {
return 1
}

args = c.flags.Args()

switch len(args) {
case 0:
c.UI.Error("Error: missing ENVIRONMENT argument\n")
c.UI.Output(c.Help())
return 1
case 1:
environment = 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
}

environment := args[0]

_, ok := c.Trellis.Environments[environment]
if !ok {
c.UI.Error(fmt.Sprintf("Error: %s is not a valid environment", environment))
Expand Down
2 changes: 1 addition & 1 deletion cmd/provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestProvisionRunValidations(t *testing.T) {
"no_args",
true,
nil,
"Error: missing ENVIRONMENT argument",
"Error: missing arguments (expected exactly 1, got 0)",
1,
},
{
Expand Down
Loading