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:
parent
45a9edb763
commit
33ba6774e0
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -47,10 +47,12 @@ func TestStateMv(t *testing.T) {
|
|||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &StateMvCommand{
|
||||
StateMeta{
|
||||
Meta: Meta{
|
||||
testingOverrides: metaOverridesForProvider(p),
|
||||
Ui: ui,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
|
@ -133,10 +135,12 @@ func TestStateMv_explicitWithBackend(t *testing.T) {
|
|||
p := testProvider()
|
||||
ui = new(cli.MockUi)
|
||||
c := &StateMvCommand{
|
||||
StateMeta{
|
||||
Meta: Meta{
|
||||
testingOverrides: metaOverridesForProvider(p),
|
||||
Ui: ui,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
args = []string{
|
||||
|
@ -194,10 +198,12 @@ func TestStateMv_backupExplicit(t *testing.T) {
|
|||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &StateMvCommand{
|
||||
StateMeta{
|
||||
Meta: Meta{
|
||||
testingOverrides: metaOverridesForProvider(p),
|
||||
Ui: ui,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
|
@ -244,10 +250,12 @@ func TestStateMv_stateOutNew(t *testing.T) {
|
|||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &StateMvCommand{
|
||||
StateMeta{
|
||||
Meta: Meta{
|
||||
testingOverrides: metaOverridesForProvider(p),
|
||||
Ui: ui,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
|
@ -316,10 +324,12 @@ func TestStateMv_stateOutExisting(t *testing.T) {
|
|||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &StateMvCommand{
|
||||
StateMeta{
|
||||
Meta: Meta{
|
||||
testingOverrides: metaOverridesForProvider(p),
|
||||
Ui: ui,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
|
@ -357,10 +367,12 @@ func TestStateMv_noState(t *testing.T) {
|
|||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &StateMvCommand{
|
||||
StateMeta{
|
||||
Meta: Meta{
|
||||
testingOverrides: metaOverridesForProvider(p),
|
||||
Ui: ui,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{"from", "to"}
|
||||
|
@ -418,10 +430,12 @@ func TestStateMv_stateOutNew_count(t *testing.T) {
|
|||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &StateMvCommand{
|
||||
StateMeta{
|
||||
Meta: Meta{
|
||||
testingOverrides: metaOverridesForProvider(p),
|
||||
Ui: ui,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
|
@ -596,10 +610,12 @@ func TestStateMv_stateOutNew_largeCount(t *testing.T) {
|
|||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &StateMvCommand{
|
||||
StateMeta{
|
||||
Meta: Meta{
|
||||
testingOverrides: metaOverridesForProvider(p),
|
||||
Ui: ui,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
|
@ -677,10 +693,12 @@ func TestStateMv_stateOutNew_nestedModule(t *testing.T) {
|
|||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &StateMvCommand{
|
||||
StateMeta{
|
||||
Meta: Meta{
|
||||
testingOverrides: metaOverridesForProvider(p),
|
||||
Ui: ui,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -47,10 +47,12 @@ func TestStateRm(t *testing.T) {
|
|||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &StateRmCommand{
|
||||
StateMeta{
|
||||
Meta: Meta{
|
||||
testingOverrides: metaOverridesForProvider(p),
|
||||
Ui: ui,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
|
@ -109,10 +111,12 @@ func TestStateRmNoArgs(t *testing.T) {
|
|||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &StateRmCommand{
|
||||
StateMeta{
|
||||
Meta: Meta{
|
||||
testingOverrides: metaOverridesForProvider(p),
|
||||
Ui: ui,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
|
@ -169,10 +173,12 @@ func TestStateRm_backupExplicit(t *testing.T) {
|
|||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &StateRmCommand{
|
||||
StateMeta{
|
||||
Meta: Meta{
|
||||
testingOverrides: metaOverridesForProvider(p),
|
||||
Ui: ui,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
|
@ -198,10 +204,12 @@ func TestStateRm_noState(t *testing.T) {
|
|||
p := testProvider()
|
||||
ui := new(cli.MockUi)
|
||||
c := &StateRmCommand{
|
||||
StateMeta{
|
||||
Meta: Meta{
|
||||
testingOverrides: metaOverridesForProvider(p),
|
||||
Ui: ui,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue