diff --git a/cmd/cli53/main.go b/cmd/cli53/main.go index 1d2c1989..8b757468 100644 --- a/cmd/cli53/main.go +++ b/cmd/cli53/main.go @@ -7,5 +7,6 @@ import ( ) func main() { - cli53.Main(os.Args) + exitCode := cli53.Main(os.Args) + os.Exit(exitCode) } diff --git a/cmd/cli53/main_test.go b/cmd/cli53/main_test.go index 17766c25..acfed783 100644 --- a/cmd/cli53/main_test.go +++ b/cmd/cli53/main_test.go @@ -10,5 +10,8 @@ import ( // Test started when the test binary is started. Only calls main. func TestSystem(t *testing.T) { args := append([]string{"cli53"}, flag.Args()...) - cli53.Main(args) + exitCode := cli53.Main(args) + if exitCode != 0 { + t.Errorf("exit code: %d\n", exitCode) + } } diff --git a/commands.go b/commands.go index ce99e96c..46e3d672 100644 --- a/commands.go +++ b/commands.go @@ -250,9 +250,10 @@ type createArgs struct { continentCode string } -func (args createArgs) validate() { +func (args createArgs) validate() bool { if args.failover != "" && args.failover != "PRIMARY" && args.failover != "SECONDARY" { - errorAndExit("failover must be PRIMARY or SECONDARY") + fmt.Println("failover must be PRIMARY or SECONDARY") + return false } extcount := 0 if args.failover != "" { @@ -271,14 +272,18 @@ func (args createArgs) validate() { extcount += 1 } if extcount > 0 && args.identifier == "" { - errorAndExit("identifier must be set when creating an extended record") + fmt.Println("identifier must be set when creating an extended record") + return false } if extcount == 0 && args.identifier != "" { - errorAndExit("identifier should only be set when creating an extended record") + fmt.Println("identifier should only be set when creating an extended record") + return false } if extcount > 1 { - errorAndExit("failover, weight, region, country-code and continent-code are mutually exclusive") + fmt.Println("failover, weight, region, country-code and continent-code are mutually exclusive") + return false } + return true } func equalStringPtrs(a, b *string) bool { @@ -292,7 +297,6 @@ func equalStringPtrs(a, b *string) bool { } func createRecord(args createArgs) { - args.validate() zone := lookupZone(args.name) origin := fmt.Sprintf("$ORIGIN %s\n", *zone.Name) diff --git a/internal/features/validation.feature b/internal/features/validation.feature index 008d0e3d..d82b42ab 100644 --- a/internal/features/validation.feature +++ b/internal/features/validation.feature @@ -23,3 +23,31 @@ Feature: parameter validation Scenario: failover and weight are mutually exclusive When I execute "cli53 rrcreate -i id --failover PRIMARY --weight 10 $domain 'a A 127.0.0.1'" Then the exit code was 1 + + Scenario: create requires one argument + When I execute "cli53 create a b" + Then the exit code was 1 + + Scenario: delete requires one argument + When I execute "cli53 delete a b" + Then the exit code was 1 + + Scenario: import requires one argument + When I execute "cli53 import a b" + Then the exit code was 1 + + Scenario: export requires one argument + When I execute "cli53 export a b" + Then the exit code was 1 + + Scenario: rrcreate requires two arguments + When I execute "cli53 import a b c" + Then the exit code was 1 + + Scenario: rrdelete requires three arguments + When I execute "cli53 import a b c d" + Then the exit code was 1 + + Scenario: rrpurge requires one argument + When I execute "cli53 rrpurge a b" + Then the exit code was 1 \ No newline at end of file diff --git a/main.go b/main.go index d027f7f8..ca0df298 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,6 @@ package cli53 import ( - "os" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/route53" "github.com/codegangsta/cli" @@ -12,7 +10,8 @@ var r53 *route53.Route53 var version string /* passed in by go build */ // Entry point for cli53 application -func Main(args []string) { +func Main(args []string) int { + exitCode := 0 app := cli.NewApp() app.Name = "cli53" app.Usage = "manage route53 DNS" @@ -46,7 +45,8 @@ func Main(args []string) { r53 = getService(c.Bool("debug")) if len(c.Args()) != 1 { cli.ShowCommandHelp(c, "create") - os.Exit(1) + exitCode = 1 + return } createZone(c.Args().First(), c.String("comment")) }, @@ -69,7 +69,8 @@ func Main(args []string) { r53 = getService(c.Bool("debug")) if len(c.Args()) != 1 { cli.ShowCommandHelp(c, "delete") - os.Exit(1) + exitCode = 1 + return } domain := c.Args().First() deleteZone(domain, c.Bool("purge")) @@ -106,7 +107,8 @@ func Main(args []string) { r53 = getService(c.Bool("debug")) if len(c.Args()) != 1 { cli.ShowCommandHelp(c, "import") - os.Exit(1) + exitCode = 1 + return } importBind(c.Args().First(), c.String("file"), c.Bool("wait"), c.Bool("editauth"), c.Bool("replace")) }, @@ -129,7 +131,8 @@ func Main(args []string) { r53 = getService(c.Bool("debug")) if len(c.Args()) != 1 { cli.ShowCommandHelp(c, "export") - os.Exit(1) + exitCode = 1 + return } exportBind(c.Args().First(), c.Bool("full")) }, @@ -185,7 +188,8 @@ func Main(args []string) { r53 = getService(c.Bool("debug")) if len(c.Args()) != 2 { cli.ShowCommandHelp(c, "rrcreate") - os.Exit(1) + exitCode = 1 + return } var weight *int if c.IsSet("weight") { @@ -204,7 +208,11 @@ func Main(args []string) { countryCode: c.String("country-code"), continentCode: c.String("continent-code"), } - createRecord(args) + if args.validate() { + createRecord(args) + } else { + exitCode = 1 + } }, }, { @@ -230,7 +238,8 @@ func Main(args []string) { r53 = getService(c.Bool("debug")) if len(c.Args()) != 3 { cli.ShowCommandHelp(c, "rrdelete") - os.Exit(1) + exitCode = 1 + return } deleteRecord(c.Args()[0], c.Args()[1], c.Args()[2], c.Bool("wait"), c.String("identifier")) }, @@ -257,7 +266,8 @@ func Main(args []string) { r53 = getService(c.Bool("debug")) if len(c.Args()) != 1 { cli.ShowCommandHelp(c, "rrpurge") - os.Exit(1) + exitCode = 1 + return } if !c.Bool("confirm") { errorAndExit("You must --confirm this action") @@ -267,4 +277,5 @@ func Main(args []string) { }, } app.Run(args) + return exitCode }