command: plan supports detailed exit code

This commit is contained in:
Ryan Uber 2015-04-01 09:38:19 -07:00
parent 3f5829dc78
commit 9a091ffa78
3 changed files with 61 additions and 1 deletions

View File

@ -16,7 +16,7 @@ type PlanCommand struct {
} }
func (c *PlanCommand) Run(args []string) int { func (c *PlanCommand) Run(args []string) int {
var destroy, refresh bool var destroy, refresh, detailed bool
var outPath string var outPath string
var moduleDepth int var moduleDepth int
@ -29,6 +29,7 @@ func (c *PlanCommand) Run(args []string) int {
cmdFlags.StringVar(&outPath, "out", "", "path") cmdFlags.StringVar(&outPath, "out", "", "path")
cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path") cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path") cmdFlags.StringVar(&c.Meta.backupPath, "backup", "", "path")
cmdFlags.BoolVar(&detailed, "detailed-exitcode", false, "detailed-exitcode")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil { if err := cmdFlags.Parse(args); err != nil {
return 1 return 1
@ -129,6 +130,9 @@ func (c *PlanCommand) Run(args []string) int {
ModuleDepth: moduleDepth, ModuleDepth: moduleDepth,
})) }))
if detailed {
return 2
}
return 0 return 0
} }
@ -152,6 +156,12 @@ Options:
-destroy If set, a plan will be generated to destroy all resources -destroy If set, a plan will be generated to destroy all resources
managed by the given configuration and state. managed by the given configuration and state.
-detailed-exitcode Return detailed exit codes when the command exits. This
will change the meaning of exit codes to:
0 - Succeeded, diff is empty (no changes)
1 - Errored
2 - Succeeded, there is a diff
-input=true Ask for input for variables if not directly set. -input=true Ask for input for variables if not directly set.
-module-depth=n Specifies the depth of modules to show in the output. -module-depth=n Specifies the depth of modules to show in the output.

View File

@ -567,6 +567,56 @@ func TestPlan_disableBackup(t *testing.T) {
} }
} }
func TestPlan_detailedExitcode(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(testFixturePath("plan")); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
p := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
ContextOpts: testCtxConfig(p),
Ui: ui,
},
}
args := []string{"-detailed-exitcode"}
if code := c.Run(args); code != 2 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
}
func TestPlan_detailedExitcode_emptyDiff(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(testFixturePath("plan-emptydiff")); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
p := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
ContextOpts: testCtxConfig(p),
Ui: ui,
},
}
args := []string{"-detailed-exitcode"}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
}
const planVarFile = ` const planVarFile = `
foo = "bar" foo = "bar"
` `