~mna/straw

6be2eb071742b0838001e6891c9829c197d3b704 — Martin Angers 1 year, 28 days ago 3074231
exit with success if -no-apply is set and generation was successful
4 files changed, 43 insertions(+), 8 deletions(-)

M cmd/straw/straw.go
M pkg/errors/errors.go
M pkg/exec/exec.go
M straw.go
M cmd/straw/straw.go => cmd/straw/straw.go +16 -3
@@ 38,6 38,14 @@ type messager interface {
	Message() string
}

// successer is the interface implemented by errors that
// are used to propagate a message to display, but other
// than that the command should exit with a success code
// if Success returns true.
type successer interface {
	Success() bool
}

// osenv provides the OS-specific environment, such
// as standard output and input and exit call.
type osenv struct {


@@ 180,8 188,9 @@ func run(oe osenv, args []string) {
		TerraformStageDir:     stageDir,
		TerraformBootstrapDir: bootstrapDir,

		RunTerraformApply: (fl.Apply || !fl.NoApply),
		RunTerraformInit:  !fl.NoInit,
		RunTerraformApply:       (fl.Apply || !fl.NoApply),
		RunTerraformInit:        !fl.NoInit,
		TerraformApplyRequested: (fl.Apply || !fl.NoApply),

		TerraformVersionCondition:            fl.TerraformVersion,
		TerraformAWSProviderVersionCondition: fl.AWSProviderVersion,


@@ 262,7 271,11 @@ func run(oe osenv, args []string) {
	}
	if err := cmd.Run(); err != nil {
		if em, ok := err.(messager); ok {
			oe.exitWithMsg(em, 4)
			exitCode := 4
			if sc, ok := err.(successer); ok && sc.Success() {
				exitCode = 0
			}
			oe.exitWithMsg(em, exitCode)
			return
		}
		oe.exit(err, 3)

M pkg/errors/errors.go => pkg/errors/errors.go +18 -4
@@ 20,7 20,7 @@ or, if using aws-vault to provide AWS credentials (recommended):

  $ (cd %[2]s && terraform init)

or, if using aws-vault to provide AWS credentials (recommended):
or, if using aws-vault to provide AWS credentials:

  $ (cd %[2]s && aws-vault exec $AWS_PROFILE -- terraform init)



@@ 28,7 28,7 @@ and apply with:

  $ (cd %[2]s && terraform apply)

or
or, using aws-vault:

  $ (cd %[2]s && aws-vault exec $AWS_PROFILE -- terraform apply)
`


@@ 75,8 75,9 @@ func NewStage(err error, stage, dir string) *ApplyError {
// ApplyError indicates that a terraform apply (and possibly
// init) is required to apply the configuration.
type ApplyError struct {
	Err error
	msg string
	Err     error
	success bool
	msg     string
}

// Error returns the error message for an ApplyError.


@@ 89,3 90,16 @@ func (e *ApplyError) Error() string {
func (e *ApplyError) Message() string {
	return e.msg
}

// MarkSuccess marks the "error" as a success, i.e. the
// error is returned for its message to be displayed, but the
// command should exit with a success.
func (e *ApplyError) MarkSuccess() {
	e.success = true
}

// Success returns true if the error was only returned for its
// message to display, but the command should exit with success.
func (e *ApplyError) Success() bool {
	return e.success
}

M pkg/exec/exec.go => pkg/exec/exec.go +6 -1
@@ 133,7 133,12 @@ func (c *Cmd) Run() error {
		return c.TF.Exec("apply", c.RC.TerraformStageDir)
	}

	return errors.NewStage(straw.ErrApplyRequired, c.RC.Stage, c.RC.TerraformStageDir)
	ae := errors.NewStage(straw.ErrApplyRequired, c.RC.Stage, c.RC.TerraformStageDir)
	// treat this error as success (exit code 0) if apply was not requested.
	if !c.RC.TerraformApplyRequested {
		ae.MarkSuccess()
	}
	return ae
}

func validateRunConfig(rc straw.RunConfig) error {

M straw.go => straw.go +3 -0
@@ 40,6 40,9 @@ type RunConfig struct {

	RunTerraformApply bool
	RunTerraformInit  bool
	// if apply was initially requested (may have been set to false
	// if terraform not installed)
	TerraformApplyRequested bool

	TerraformVersionCondition            string
	TerraformAWSProviderVersionCondition string