From 65982bd4128f66fc404d5936a9e0d1763ff29362 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 14 Feb 2017 11:17:28 -0800 Subject: [PATCH] backend/local: use new command/state package for better UX --- backend/local/backend_apply.go | 24 ++++++++---------------- backend/local/backend_local.go | 6 ++++-- backend/local/backend_plan.go | 18 +++++++++--------- backend/local/backend_refresh.go | 19 +++++++++---------- 4 files changed, 30 insertions(+), 37 deletions(-) diff --git a/backend/local/backend_apply.go b/backend/local/backend_apply.go index 4c0804717..62c4fcd1f 100644 --- a/backend/local/backend_apply.go +++ b/backend/local/backend_apply.go @@ -8,7 +8,7 @@ import ( "github.com/hashicorp/errwrap" "github.com/hashicorp/go-multierror" "github.com/hashicorp/terraform/backend" - "github.com/hashicorp/terraform/state" + clistate "github.com/hashicorp/terraform/command/state" "github.com/hashicorp/terraform/terraform" ) @@ -35,22 +35,14 @@ func (b *Local) opApply( return } - // context acquired the state, and therefor the lock. - // Unlock it when the operation is complete - defer func() { - if s, ok := opState.(state.Locker); op.LockState && ok { - if err := s.Unlock(); err != nil { - runningOp.Err = multierror.Append(runningOp.Err, - errwrap.Wrapf("Error unlocking state:\n\n"+ - "{{err}}\n\n"+ - "The Terraform operation completed but there was an error unlocking the state.\n"+ - "This may require unlocking the state manually with the `terraform unlock` command\n", - err, - ), - ) + // If we're locking state, unlock when we're done + if op.LockState { + defer func() { + if err := clistate.Unlock(opState, b.CLI, b.Colorize()); err != nil { + runningOp.Err = multierror.Append(runningOp.Err, err) } - } - }() + }() + } // Setup the state runningOp.State = tfCtx.State() diff --git a/backend/local/backend_local.go b/backend/local/backend_local.go index 5aca70dd5..cdcaac1d1 100644 --- a/backend/local/backend_local.go +++ b/backend/local/backend_local.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/errwrap" "github.com/hashicorp/go-multierror" "github.com/hashicorp/terraform/backend" + clistate "github.com/hashicorp/terraform/command/state" "github.com/hashicorp/terraform/state" "github.com/hashicorp/terraform/terraform" ) @@ -28,8 +29,9 @@ func (b *Local) context(op *backend.Operation) (*terraform.Context, state.State, return nil, nil, errwrap.Wrapf("Error loading state: {{err}}", err) } - if s, ok := s.(state.Locker); op.LockState && ok { - if err := s.Lock(op.Type.String()); err != nil { + if op.LockState { + err := clistate.Lock(s, op.Type.String(), b.CLI, b.Colorize()) + if err != nil { return nil, nil, errwrap.Wrapf("Error locking state: {{err}}", err) } } diff --git a/backend/local/backend_plan.go b/backend/local/backend_plan.go index 2c117aae2..11faa8999 100644 --- a/backend/local/backend_plan.go +++ b/backend/local/backend_plan.go @@ -8,10 +8,11 @@ import ( "strings" "github.com/hashicorp/errwrap" + "github.com/hashicorp/go-multierror" "github.com/hashicorp/terraform/backend" "github.com/hashicorp/terraform/command/format" + clistate "github.com/hashicorp/terraform/command/state" "github.com/hashicorp/terraform/config/module" - "github.com/hashicorp/terraform/state" "github.com/hashicorp/terraform/terraform" ) @@ -58,15 +59,14 @@ func (b *Local) opPlan( return } - // context acquired the state, and therefor the lock. - // Unlock it when the operation is complete - defer func() { - if s, ok := opState.(state.Locker); op.LockState && ok { - if err := s.Unlock(); err != nil { - log.Printf("[ERROR]: %s", err) + // If we're locking state, unlock when we're done + if op.LockState { + defer func() { + if err := clistate.Unlock(opState, b.CLI, b.Colorize()); err != nil { + runningOp.Err = multierror.Append(runningOp.Err, err) } - } - }() + }() + } // Setup the state runningOp.State = tfCtx.State() diff --git a/backend/local/backend_refresh.go b/backend/local/backend_refresh.go index d3f41cc21..dc5c9a6e4 100644 --- a/backend/local/backend_refresh.go +++ b/backend/local/backend_refresh.go @@ -3,12 +3,12 @@ package local import ( "context" "fmt" - "log" "os" "github.com/hashicorp/errwrap" + "github.com/hashicorp/go-multierror" "github.com/hashicorp/terraform/backend" - "github.com/hashicorp/terraform/state" + clistate "github.com/hashicorp/terraform/command/state" ) func (b *Local) opRefresh( @@ -48,15 +48,14 @@ func (b *Local) opRefresh( return } - // context acquired the state, and therefor the lock. - // Unlock it when the operation is complete - defer func() { - if s, ok := opState.(state.Locker); op.LockState && ok { - if err := s.Unlock(); err != nil { - log.Printf("[ERROR]: %s", err) + // If we're locking state, unlock when we're done + if op.LockState { + defer func() { + if err := clistate.Unlock(opState, b.CLI, b.Colorize()); err != nil { + runningOp.Err = multierror.Append(runningOp.Err, err) } - } - }() + }() + } // Set our state runningOp.State = opState.State()