Skip to content

Commit

Permalink
Fix unknow option handling in commands (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncsibra-lab49 authored Jan 11, 2023
1 parent 4f976c7 commit 980d4cc
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 40 deletions.
43 changes: 31 additions & 12 deletions deploy/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,13 @@ type CommandArgs struct {
func ParseArgs(args []string) (*CommandArgs, error) {
var result CommandArgs
var T = true
var F = false
result.StateShouldExist = &T // set default to true

for _, arg := range args {
if arg == "-p" || arg == "--plan-only" {
result.PlanOnly = true
} else if arg == "-n" || arg == "--new-state" {
result.StateShouldExist = &F
} else if result.EnvName == "" {
result.EnvName = arg
} else if result.Version == "" {
result.Version = arg
} else {
return nil, errors.New("unknown deploy option: " + arg)
i := 0
for ; i < len(args); i++ {
_, err := handleArgs(args[i], &result)
if err != nil {
return nil, err
}
}

Expand All @@ -52,6 +45,32 @@ func ParseArgs(args []string) (*CommandArgs, error) {
return &result, nil
}

func handleArgs(arg string, commandArgs *CommandArgs) (bool, error) {
if strings.HasPrefix(arg, "-") {
return handleFlag(arg, commandArgs)
} else if commandArgs.EnvName == "" {
commandArgs.EnvName = arg
} else if commandArgs.Version == "" {
commandArgs.Version = arg
} else {
return false, errors.New("unknown deploy argument: " + arg)
}
return false, nil
}

func handleFlag(arg string, commandArgs *CommandArgs) (bool, error) {
var F = false

if arg == "-p" || arg == "--plan-only" {
commandArgs.PlanOnly = true
} else if arg == "-n" || arg == "--new-state" {
commandArgs.StateShouldExist = &F
} else {
return false, errors.New("unknown deploy option: " + arg)
}
return false, nil
}

// RunCommand runs the release command.
func RunCommand(state *command.GlobalState, args *CommandArgs, env map[string]string) (returnedError error) {
prepareTerraformResponse, buildVolume, terraformImage, err := config.SetupTerraform(state, args.StateShouldExist, args.EnvName, args.Version, env)
Expand Down
36 changes: 27 additions & 9 deletions destroy/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,11 @@ func ParseArgs(args []string) (*CommandArgs, error) {
var T = true
result.StateShouldExist = &T // set default to true

for _, arg := range args {
if arg == "-p" || arg == "--plan-only" {
result.PlanOnly = true
} else if result.EnvName == "" {
result.EnvName = arg
} else if result.Version == "" {
result.Version = arg
} else {
return nil, errors.New("unknown destroy option: " + arg)
i := 0
for ; i < len(args); i++ {
_, err := handleArgs(args[i], &result)
if err != nil {
return nil, err
}
}

Expand All @@ -49,6 +45,28 @@ func ParseArgs(args []string) (*CommandArgs, error) {
return &result, nil
}

func handleArgs(arg string, commandArgs *CommandArgs) (bool, error) {
if strings.HasPrefix(arg, "-") {
return handleFlag(arg, commandArgs)
} else if commandArgs.EnvName == "" {
commandArgs.EnvName = arg
} else if commandArgs.Version == "" {
commandArgs.Version = arg
} else {
return false, errors.New("unknown destroy argument: " + arg)
}
return false, nil
}

func handleFlag(arg string, commandArgs *CommandArgs) (bool, error) {
if arg == "-p" || arg == "--plan-only" {
commandArgs.PlanOnly = true
} else {
return false, errors.New("unknown destroy option: " + arg)
}
return false, nil
}

// RunCommand runs the release command.
func RunCommand(state *command.GlobalState, args *CommandArgs, env map[string]string) (returnedError error) {
prepareTerraformResponse, buildVolume, terraformImage, err := config.SetupTerraform(state, args.StateShouldExist, args.EnvName, args.Version, env)
Expand Down
26 changes: 16 additions & 10 deletions init/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ func RunCommand(state *command.GlobalState, args *CommandArgs, env map[string]st

// ParseArgs parse command line arguments for init command.
func ParseArgs(args []string) (*CommandArgs, error) {
if len(args)%2 != 0 {
return nil, fmt.Errorf("argument length must be even, boolean arguments are not supported")
}

result := CommandArgs{Variables: map[string]string{}}
i := 0

Expand Down Expand Up @@ -130,33 +126,43 @@ func checkFolder(state *command.GlobalState, folder string) (bool, error) {
}

func handleArgs(arg string, commandArgs *CommandArgs, take func() (string, error)) (bool, error) {
switch arg {
case "--name":
if strings.HasPrefix(arg, "-") {
return handleFlag(arg, commandArgs, take)
} else {
return false, errors.New("unknown init argument: " + arg)
}
}

func handleFlag(arg string, commandArgs *CommandArgs, take func() (string, error)) (bool, error) {
if arg == "-n" || arg == "--name" {
value, err := take()
if err != nil {
return false, err
}
commandArgs.Name = value
case "--boilerplate":
} else if arg == "-b" || arg == "--boilerplate" {
value, err := take()
if err != nil {
return false, err
}
commandArgs.Boilerplate = value
default:
} else {
if !strings.HasPrefix(arg, "--") {
return false, fmt.Errorf("argument name must start with '--': %s", arg)
return false, fmt.Errorf("option name must start with '--': %s", arg)
}

name := strings.TrimPrefix(arg, "--")

if name == "" {
return false, fmt.Errorf("missing option key")
}

value, err := take()
if err != nil {
return false, err
}

commandArgs.Variables[name] = value
}

return false, nil
}
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ Usage:
Options:
--name - Name of the new project repository
--boilerplate - git URL of the git repo to copy as boilerplate. To use a specific branch (or any valid git refspec), add "?ref=branch-name" to the end of the URL.
--name | -n - Name of the new project repository
--boilerplate | -b - git URL of the git repo to copy as boilerplate. To use a specific branch (or any valid git refspec), add "?ref=branch-name" to the end of the URL.
--{boilerplate arguments} - Dynamic arguments for the templates files. E.g.: "--domain name --account test"
Boilerplate Templates:
Expand All @@ -139,8 +139,8 @@ Boilerplate Templates:
The 'name' variable is predefined, using the value passed by --name.
You can specify additional variables by passing arguments like:
--domain name"
--account test"
--domain name
--account test
` + globalOptions

Expand Down
16 changes: 13 additions & 3 deletions release/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,22 @@ func parseReleaseData(value string) (map[string]string, error) {
if len(dataStrings) == 2 {
return map[string]string{dataStrings[0]: dataStrings[1]}, nil
} else {
return nil, errors.New("Release data not in the correct format")
return nil, errors.New("release data not in the correct format")
}
}

func handleArgs(arg string, commandArgs *CommandArgs, take func() (string, error)) (bool, error) {
if strings.HasPrefix(arg, "-") {
return handleFlag(arg, commandArgs, take)
} else if commandArgs.Version == "" {
commandArgs.Version = arg
} else {
return false, errors.New("unknown release argument: " + arg)
}
return false, nil
}

func handleFlag(arg string, commandArgs *CommandArgs, take func() (string, error)) (bool, error) {
if arg == "-r" || arg == "--release-data" {
value, err := take()
if err != nil {
Expand All @@ -55,8 +66,6 @@ func handleArgs(arg string, commandArgs *CommandArgs, take func() (string, error
for k, v := range releaseData {
commandArgs.ReleaseData[k] = v
}
} else if commandArgs.Version == "" {
commandArgs.Version = arg
} else {
return false, errors.New("unknown release option: " + arg)
}
Expand All @@ -67,6 +76,7 @@ func handleArgs(arg string, commandArgs *CommandArgs, take func() (string, error
func ParseArgs(args []string) (*CommandArgs, error) {
var result CommandArgs
result.ReleaseData = make(map[string]string)

i := 0
take := func() (string, error) {
i++
Expand Down
2 changes: 1 addition & 1 deletion release/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestParseArgsWhenEnv(t *testing.T) {

_, gotError := release.ParseArgs(args)

var wantError error = errors.New("Release data not in the correct format")
var wantError = errors.New("release data not in the correct format")

assertError(t, gotError, wantError)

Expand Down
2 changes: 1 addition & 1 deletion shell/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func handleFlag(arg string, commandArgs *CommandArgs, take func() (string, error
} else if strings.HasPrefix(arg, "--version=") {
commandArgs.Version = strings.TrimPrefix(arg, "--version=")
} else {
return false, errors.New("unknown global option: " + arg)
return false, errors.New("unknown shell option: " + arg)
}
return false, nil
}
Expand Down

0 comments on commit 980d4cc

Please sign in to comment.