Make the state commands use the real command.Meta

In order to use a backend for the state commands, we need an initialized
meta. Use a single Meta instance rather than temporary ones to make sure
the backends are initialized properly.
This commit is contained in:
James Bardin 2017-07-26 13:08:09 -04:00
parent 45a9edb763
commit 33ba6774e0
6 changed files with 92 additions and 67 deletions

View File

@ -11,30 +11,32 @@ import (
)
// StateMeta is the meta struct that should be embedded in state subcommands.
type StateMeta struct{}
type StateMeta struct {
Meta
}
// State returns the state for this meta. This gets the appropriate state from
// the backend, but changes the way that backups are done. This configures
// backups to be timestamped rather than just the original state path plus a
// backup path.
func (c *StateMeta) State(m *Meta) (state.State, error) {
func (c *StateMeta) State() (state.State, error) {
var realState state.State
backupPath := m.backupPath
stateOutPath := m.statePath
backupPath := c.backupPath
stateOutPath := c.statePath
// use the specified state
if m.statePath != "" {
if c.statePath != "" {
realState = &state.LocalState{
Path: m.statePath,
Path: c.statePath,
}
} else {
// Load the backend
b, err := m.Backend(nil)
b, err := c.Backend(nil)
if err != nil {
return nil, err
}
env := m.Workspace()
env := c.Workspace()
// Get the state
s, err := b.State(env)
if err != nil {
@ -42,7 +44,7 @@ func (c *StateMeta) State(m *Meta) (state.State, error) {
}
// Get a local backend
localRaw, err := m.Backend(&BackendOpts{ForceLocal: true})
localRaw, err := c.Backend(&BackendOpts{ForceLocal: true})
if err != nil {
// This should never fail
panic(err)

View File

@ -10,7 +10,6 @@ import (
// StateMvCommand is a Command implementation that shows a single resource.
type StateMvCommand struct {
Meta
StateMeta
}
@ -21,12 +20,13 @@ func (c *StateMvCommand) Run(args []string) int {
}
// We create two metas to track the two states
var meta1, meta2 Meta
var backupPathOut, statePathOut string
cmdFlags := c.Meta.flagSet("state mv")
cmdFlags.StringVar(&meta1.backupPath, "backup", "-", "backup")
cmdFlags.StringVar(&meta1.statePath, "state", "", "path")
cmdFlags.StringVar(&meta2.backupPath, "backup-out", "-", "backup")
cmdFlags.StringVar(&meta2.statePath, "state-out", "", "path")
cmdFlags.StringVar(&c.backupPath, "backup", "-", "backup")
cmdFlags.StringVar(&c.statePath, "state", "", "path")
cmdFlags.StringVar(&backupPathOut, "backup-out", "-", "backup")
cmdFlags.StringVar(&statePathOut, "state-out", "", "path")
if err := cmdFlags.Parse(args); err != nil {
return cli.RunResultHelp
}
@ -36,13 +36,8 @@ func (c *StateMvCommand) Run(args []string) int {
return cli.RunResultHelp
}
// Copy the `-state` flag for output if we weren't given a custom one
if meta2.statePath == "" {
meta2.statePath = meta1.statePath
}
// Read the from state
stateFrom, err := c.StateMeta.State(&meta1)
stateFrom, err := c.State()
if err != nil {
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
return cli.RunResultHelp
@ -62,8 +57,11 @@ func (c *StateMvCommand) Run(args []string) int {
// Read the destination state
stateTo := stateFrom
stateToReal := stateFromReal
if meta2.statePath != meta1.statePath {
stateTo, err = c.StateMeta.State(&meta2)
if statePathOut != "" {
c.statePath = statePathOut
c.backupPath = backupPathOut
stateTo, err = c.State()
if err != nil {
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
return cli.RunResultHelp

View File

@ -47,9 +47,11 @@ func TestStateMv(t *testing.T) {
p := testProvider()
ui := new(cli.MockUi)
c := &StateMvCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
StateMeta{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
},
}
@ -133,9 +135,11 @@ func TestStateMv_explicitWithBackend(t *testing.T) {
p := testProvider()
ui = new(cli.MockUi)
c := &StateMvCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
StateMeta{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
},
}
@ -194,9 +198,11 @@ func TestStateMv_backupExplicit(t *testing.T) {
p := testProvider()
ui := new(cli.MockUi)
c := &StateMvCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
StateMeta{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
},
}
@ -244,9 +250,11 @@ func TestStateMv_stateOutNew(t *testing.T) {
p := testProvider()
ui := new(cli.MockUi)
c := &StateMvCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
StateMeta{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
},
}
@ -316,9 +324,11 @@ func TestStateMv_stateOutExisting(t *testing.T) {
p := testProvider()
ui := new(cli.MockUi)
c := &StateMvCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
StateMeta{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
},
}
@ -357,9 +367,11 @@ func TestStateMv_noState(t *testing.T) {
p := testProvider()
ui := new(cli.MockUi)
c := &StateMvCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
StateMeta{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
},
}
@ -418,9 +430,11 @@ func TestStateMv_stateOutNew_count(t *testing.T) {
p := testProvider()
ui := new(cli.MockUi)
c := &StateMvCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
StateMeta{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
},
}
@ -596,9 +610,11 @@ func TestStateMv_stateOutNew_largeCount(t *testing.T) {
p := testProvider()
ui := new(cli.MockUi)
c := &StateMvCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
StateMeta{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
},
}
@ -677,9 +693,11 @@ func TestStateMv_stateOutNew_nestedModule(t *testing.T) {
p := testProvider()
ui := new(cli.MockUi)
c := &StateMvCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
StateMeta{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
},
}

View File

@ -9,7 +9,6 @@ import (
// StateRmCommand is a Command implementation that shows a single resource.
type StateRmCommand struct {
Meta
StateMeta
}
@ -20,8 +19,8 @@ func (c *StateRmCommand) Run(args []string) int {
}
cmdFlags := c.Meta.flagSet("state show")
cmdFlags.StringVar(&c.Meta.backupPath, "backup", "-", "backup")
cmdFlags.StringVar(&c.Meta.statePath, "state", "", "path")
cmdFlags.StringVar(&c.backupPath, "backup", "-", "backup")
cmdFlags.StringVar(&c.statePath, "state", "", "path")
if err := cmdFlags.Parse(args); err != nil {
return cli.RunResultHelp
}
@ -32,7 +31,7 @@ func (c *StateRmCommand) Run(args []string) int {
return 1
}
state, err := c.StateMeta.State(&c.Meta)
state, err := c.State()
if err != nil {
c.Ui.Error(fmt.Sprintf(errStateLoadingState, err))
return cli.RunResultHelp

View File

@ -47,9 +47,11 @@ func TestStateRm(t *testing.T) {
p := testProvider()
ui := new(cli.MockUi)
c := &StateRmCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
StateMeta{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
},
}
@ -109,9 +111,11 @@ func TestStateRmNoArgs(t *testing.T) {
p := testProvider()
ui := new(cli.MockUi)
c := &StateRmCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
StateMeta{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
},
}
@ -169,9 +173,11 @@ func TestStateRm_backupExplicit(t *testing.T) {
p := testProvider()
ui := new(cli.MockUi)
c := &StateRmCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
StateMeta{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
},
}
@ -198,9 +204,11 @@ func TestStateRm_noState(t *testing.T) {
p := testProvider()
ui := new(cli.MockUi)
c := &StateRmCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
StateMeta{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
},
},
}

View File

@ -28,7 +28,7 @@ func TestStateDefaultBackupExtension(t *testing.T) {
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
s, err := (&StateMeta{}).State(&Meta{})
s, err := (&StateMeta{}).State()
if err != nil {
t.Fatal(err)
}