From 5cb574035a8b034eccb74a1f61e1388e7726e4f9 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 8 Sep 2017 09:35:09 +0200 Subject: [PATCH] backend/remote-state/gcloud: Refactor Backend.remoteClient(). This replaces stateFileName() and lockFileName() with path.Join(). Fixes: * https://github.com/golang/go/wiki/CodeReviewComments#doc-comments * https://github.com/golang/go/wiki/CodeReviewComments#doc-comments --- backend/remote-state/gcloud/backend_state.go | 40 +++++++------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/backend/remote-state/gcloud/backend_state.go b/backend/remote-state/gcloud/backend_state.go index 947a11c46..90f38cf2a 100644 --- a/backend/remote-state/gcloud/backend_state.go +++ b/backend/remote-state/gcloud/backend_state.go @@ -1,7 +1,6 @@ package gcloud import ( - "errors" "fmt" "path" "sort" @@ -15,6 +14,11 @@ import ( "google.golang.org/api/iterator" ) +const ( + stateFileSuffix = ".tfstate" + lockFileSuffix = ".tflock" +) + // States returns a list of names for the states found on GCS. The default // state is always returned as the first element in the slice. func (b *Backend) States() ([]string, error) { @@ -35,10 +39,10 @@ func (b *Backend) States() ([]string, error) { } name := path.Base(attrs.Name) - if !strings.HasSuffix(name, ".tfstate") { + if !strings.HasSuffix(name, stateFileSuffix) { continue } - st := strings.TrimSuffix(name, ".tfstate") + st := strings.TrimSuffix(name, stateFileSuffix) if st != backend.DefaultStateName { states = append(states, st) @@ -63,21 +67,19 @@ func (b *Backend) DeleteState(name string) error { return client.Delete() } -// get a remote client configured for this state +// remoteClient returns a RemoteClient for the named state. func (b *Backend) remoteClient(name string) (*RemoteClient, error) { if name == "" { - return nil, errors.New("Missing state name") + return nil, fmt.Errorf("%q is not a valid state name", name) } - client := &RemoteClient{ + return &RemoteClient{ storageContext: b.storageContext, storageClient: b.storageClient, bucketName: b.bucketName, - stateFilePath: b.stateFileName(name), - lockFilePath: b.lockFileName(name), - } - - return client, nil + stateFilePath: path.Join(b.stateDir, name+stateFileSuffix), + lockFilePath: path.Join(b.stateDir, name+lockFileSuffix), + }, nil } func (b *Backend) State(name string) (state.State, error) { @@ -129,22 +131,6 @@ func (b *Backend) State(name string) (state.State, error) { return stateMgr, nil } -func (b *Backend) stateFileName(stateName string) string { - if b.stateDir == "" { - return fmt.Sprintf("%v.tfstate", stateName) - } else { - return fmt.Sprintf("%v/%v.tfstate", b.stateDir, stateName) - } -} - -func (b *Backend) lockFileName(stateName string) string { - if b.stateDir == "" { - return fmt.Sprintf("%v.tflock", stateName) - } else { - return fmt.Sprintf("%v/%v.tflock", b.stateDir, stateName) - } -} - const errStateUnlock = ` Error unlocking Google Cloud Storage state.