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
This commit is contained in:
Florian Forster 2017-09-08 09:35:09 +02:00 committed by James Bardin
parent 9ae45e320f
commit 5cb574035a
1 changed files with 13 additions and 27 deletions

View File

@ -1,7 +1,6 @@
package gcloud package gcloud
import ( import (
"errors"
"fmt" "fmt"
"path" "path"
"sort" "sort"
@ -15,6 +14,11 @@ import (
"google.golang.org/api/iterator" "google.golang.org/api/iterator"
) )
const (
stateFileSuffix = ".tfstate"
lockFileSuffix = ".tflock"
)
// States returns a list of names for the states found on GCS. The default // 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. // state is always returned as the first element in the slice.
func (b *Backend) States() ([]string, error) { func (b *Backend) States() ([]string, error) {
@ -35,10 +39,10 @@ func (b *Backend) States() ([]string, error) {
} }
name := path.Base(attrs.Name) name := path.Base(attrs.Name)
if !strings.HasSuffix(name, ".tfstate") { if !strings.HasSuffix(name, stateFileSuffix) {
continue continue
} }
st := strings.TrimSuffix(name, ".tfstate") st := strings.TrimSuffix(name, stateFileSuffix)
if st != backend.DefaultStateName { if st != backend.DefaultStateName {
states = append(states, st) states = append(states, st)
@ -63,21 +67,19 @@ func (b *Backend) DeleteState(name string) error {
return client.Delete() 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) { func (b *Backend) remoteClient(name string) (*RemoteClient, error) {
if name == "" { 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, storageContext: b.storageContext,
storageClient: b.storageClient, storageClient: b.storageClient,
bucketName: b.bucketName, bucketName: b.bucketName,
stateFilePath: b.stateFileName(name), stateFilePath: path.Join(b.stateDir, name+stateFileSuffix),
lockFilePath: b.lockFileName(name), lockFilePath: path.Join(b.stateDir, name+lockFileSuffix),
} }, nil
return client, nil
} }
func (b *Backend) State(name string) (state.State, error) { 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 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 = ` const errStateUnlock = `
Error unlocking Google Cloud Storage state. Error unlocking Google Cloud Storage state.