From 639bf356aa5c04b41e899a05113bf8df591ba6cb Mon Sep 17 00:00:00 2001 From: Orivej Desh Date: Sat, 17 Sep 2016 14:34:37 +0000 Subject: [PATCH] Fail on unknown values of -on-error --- command/build.go | 4 +++- common/multistep_runner.go | 4 +--- helper/enumflag/flag.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 helper/enumflag/flag.go diff --git a/command/build.go b/command/build.go index 55f3e5b92..89e5101df 100644 --- a/command/build.go +++ b/command/build.go @@ -10,6 +10,7 @@ import ( "strings" "sync" + "github.com/mitchellh/packer/helper/enumflag" "github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/template" ) @@ -26,7 +27,8 @@ func (c BuildCommand) Run(args []string) int { flags.BoolVar(&cfgColor, "color", true, "") flags.BoolVar(&cfgDebug, "debug", false, "") flags.BoolVar(&cfgForce, "force", false, "") - flags.StringVar(&cfgOnError, "on-error", "cleanup", "") + flagOnError := enumflag.New(&cfgOnError, "cleanup", "abort", "ask") + flags.Var(flagOnError, "on-error", "") flags.BoolVar(&cfgParallel, "parallel", true, "") if err := flags.Parse(args); err != nil { return 1 diff --git a/common/multistep_runner.go b/common/multistep_runner.go index 77e29dcf4..edacee6a0 100644 --- a/common/multistep_runner.go +++ b/common/multistep_runner.go @@ -14,7 +14,7 @@ import ( func newRunner(steps []multistep.Step, config PackerConfig, ui packer.Ui) (multistep.Runner, multistep.DebugPauseFn) { switch config.PackerOnError { - case "cleanup": + case "", "cleanup": case "abort": for i, step := range steps { steps[i] = abortStep{step, ui} @@ -23,8 +23,6 @@ func newRunner(steps []multistep.Step, config PackerConfig, ui packer.Ui) (multi for i, step := range steps { steps[i] = askStep{step, ui} } - default: - ui.Error(fmt.Sprintf("Ignoring -on-error=%q argument: unknown on-error value", config.PackerOnError)) } if config.PackerDebug { diff --git a/helper/enumflag/flag.go b/helper/enumflag/flag.go new file mode 100644 index 000000000..ab6dce29b --- /dev/null +++ b/helper/enumflag/flag.go @@ -0,0 +1,28 @@ +package enumflag + +import "fmt" + +type enumFlag struct { + target *string + options []string +} + +// New returns a flag.Value implementation for parsing flags with a one-of-a-set value +func New(target *string, options ...string) *enumFlag { + return &enumFlag{target: target, options: options} +} + +func (f *enumFlag) String() string { + return *f.target +} + +func (f *enumFlag) Set(value string) error { + for _, v := range f.options { + if v == value { + *f.target = value + return nil + } + } + + return fmt.Errorf("expected one of %q", f.options) +}