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:
parent
9ae45e320f
commit
5cb574035a
|
@ -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.
|
||||
|
||||
|
|
Loading…
Reference in New Issue