From 7febe57ae618fb370a6e309d0053ad96bb5b4608 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Wed, 8 Oct 2014 12:05:26 -0700 Subject: [PATCH] remote: Avoid a panic by re-ordering cases --- remote/remote.go | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/remote/remote.go b/remote/remote.go index a204a2f1a..7b18712bc 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -293,20 +293,18 @@ func RefreshState(conf *terraform.RemoteState) (StateChangeResult, error) { return StateChangeInit, nil case remoteState == nil && localState != nil: - fallthrough - case remoteState.Serial < localState.Serial: // User should probably do a push, nothing to do return StateChangeLocalNewer, nil case remoteState != nil && localState == nil: - fallthrough + goto PERSIST + + case remoteState.Serial < localState.Serial: + // User should probably do a push, nothing to do + return StateChangeLocalNewer, nil + case remoteState.Serial > localState.Serial: - // Update the local state from the remote state - if err := Persist(bytes.NewReader(payload.State)); err != nil { - return StateChangeNoop, - fmt.Errorf("Failed to persist state: %v", err) - } - return StateChangeUpdateLocal, nil + goto PERSIST case remoteState.Serial == localState.Serial: // Check for a hash collision on the local/remote state @@ -321,10 +319,18 @@ func RefreshState(conf *terraform.RemoteState) (StateChangeResult, error) { // requires a manual reconciliation. return StateChangeConflict, nil } + default: + // We should not reach this point + panic("Unhandled remote update case") } - // We should not reach this point - panic("Unhandled remote update case") +PERSIST: + // Update the local state from the remote state + if err := Persist(bytes.NewReader(payload.State)); err != nil { + return StateChangeNoop, + fmt.Errorf("Failed to persist state: %v", err) + } + return StateChangeUpdateLocal, nil } // PushState is used to read the local state and