From 39a5ddd381fc5f80a798d822b3f11f2896136c90 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 1 Mar 2017 10:10:47 -0500 Subject: [PATCH] Split Meta back out of StateMeta Removing the call to StateMeta.Env, so that it doesn't need an embedded Meta field. Embed Meta and StateMeta separately in all State commands. --- command/env_delete.go | 2 +- command/state_list.go | 1 + command/state_list_test.go | 24 ++++++-------- command/state_meta.go | 13 ++++---- command/state_mv.go | 1 + command/state_mv_test.go | 64 ++++++++++++++------------------------ command/state_pull.go | 1 + command/state_pull_test.go | 8 ++--- command/state_push.go | 1 + command/state_push_test.go | 40 +++++++++--------------- command/state_rm.go | 1 + command/state_rm_test.go | 24 ++++++-------- command/state_show.go | 1 + command/state_show_test.go | 40 +++++++++--------------- commands.go | 21 +++++-------- 15 files changed, 95 insertions(+), 147 deletions(-) diff --git a/command/env_delete.go b/command/env_delete.go index 1a773c0cc..ad484a3f9 100644 --- a/command/env_delete.go +++ b/command/env_delete.go @@ -90,7 +90,7 @@ func (c *EnvDeleteCommand) Run(args []string) int { // Lock the state if we can lockInfo := state.NewLockInfo() - lockInfo.Operation = "env new" + lockInfo.Operation = "env delete" lockID, err := clistate.Lock(sMgr, lockInfo, c.Ui, c.Colorize()) if err != nil { c.Ui.Error(fmt.Sprintf("Error locking state: %s", err)) diff --git a/command/state_list.go b/command/state_list.go index 0e7436397..d7087d1b5 100644 --- a/command/state_list.go +++ b/command/state_list.go @@ -11,6 +11,7 @@ import ( // StateListCommand is a Command implementation that lists the resources // within a state file. type StateListCommand struct { + Meta StateMeta } diff --git a/command/state_list_test.go b/command/state_list_test.go index b439592e9..86c4f7194 100644 --- a/command/state_list_test.go +++ b/command/state_list_test.go @@ -16,11 +16,9 @@ func TestStateList(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateListCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -49,11 +47,9 @@ func TestStateList_backendState(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateListCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -77,11 +73,9 @@ func TestStateList_noState(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateListCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } diff --git a/command/state_meta.go b/command/state_meta.go index 9381c6a45..879f8f641 100644 --- a/command/state_meta.go +++ b/command/state_meta.go @@ -11,13 +11,12 @@ import ( ) // StateMeta is the meta struct that should be embedded in state subcommands. -type StateMeta struct { - Meta -} +type StateMeta struct{} -// State returns the state for this meta. This is different then Meta.State -// in the way that backups are done. This configures backups to be timestamped -// rather than just the original state path plus a backup path. +// 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) { // Load the backend b, err := m.Backend(nil) @@ -25,7 +24,7 @@ func (c *StateMeta) State(m *Meta) (state.State, error) { return nil, err } - env := c.Env() + env := m.Env() // Get the state s, err := b.State(env) if err != nil { diff --git a/command/state_mv.go b/command/state_mv.go index ad6b5c048..7982d7b92 100644 --- a/command/state_mv.go +++ b/command/state_mv.go @@ -10,6 +10,7 @@ import ( // StateMvCommand is a Command implementation that shows a single resource. type StateMvCommand struct { + Meta StateMeta } diff --git a/command/state_mv_test.go b/command/state_mv_test.go index e804b3529..d479b4ccb 100644 --- a/command/state_mv_test.go +++ b/command/state_mv_test.go @@ -46,11 +46,9 @@ func TestStateMv(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateMvCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -115,11 +113,9 @@ func TestStateMv_backupExplicit(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateMvCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -172,11 +168,9 @@ func TestStateMv_stateOutNew(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateMvCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -246,11 +240,9 @@ func TestStateMv_stateOutExisting(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateMvCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -289,11 +281,9 @@ func TestStateMv_noState(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateMvCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -352,11 +342,9 @@ func TestStateMv_stateOutNew_count(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateMvCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -532,11 +520,9 @@ func TestStateMv_stateOutNew_largeCount(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateMvCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -615,11 +601,9 @@ func TestStateMv_stateOutNew_nestedModule(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateMvCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } diff --git a/command/state_pull.go b/command/state_pull.go index a3679a26a..a51cf5c6a 100644 --- a/command/state_pull.go +++ b/command/state_pull.go @@ -11,6 +11,7 @@ import ( // StatePullCommand is a Command implementation that shows a single resource. type StatePullCommand struct { + Meta StateMeta } diff --git a/command/state_pull_test.go b/command/state_pull_test.go index d468dbae4..3176a4c54 100644 --- a/command/state_pull_test.go +++ b/command/state_pull_test.go @@ -20,11 +20,9 @@ func TestStatePull(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StatePullCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } diff --git a/command/state_push.go b/command/state_push.go index b6e7a90d6..d23f8b7e3 100644 --- a/command/state_push.go +++ b/command/state_push.go @@ -11,6 +11,7 @@ import ( // StatePushCommand is a Command implementation that shows a single resource. type StatePushCommand struct { + Meta StateMeta } diff --git a/command/state_push_test.go b/command/state_push_test.go index 43e0e14c6..d63b193f6 100644 --- a/command/state_push_test.go +++ b/command/state_push_test.go @@ -20,11 +20,9 @@ func TestStatePush_empty(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StatePushCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -51,11 +49,9 @@ func TestStatePush_replaceMatch(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StatePushCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -82,11 +78,9 @@ func TestStatePush_lineageMismatch(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StatePushCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -113,11 +107,9 @@ func TestStatePush_serialNewer(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StatePushCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -144,11 +136,9 @@ func TestStatePush_serialOlder(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StatePushCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } diff --git a/command/state_rm.go b/command/state_rm.go index a80a0d80d..b2491c4e8 100644 --- a/command/state_rm.go +++ b/command/state_rm.go @@ -9,6 +9,7 @@ import ( // StateRmCommand is a Command implementation that shows a single resource. type StateRmCommand struct { + Meta StateMeta } diff --git a/command/state_rm_test.go b/command/state_rm_test.go index 178aa8c5d..bb3734502 100644 --- a/command/state_rm_test.go +++ b/command/state_rm_test.go @@ -46,11 +46,9 @@ func TestStateRm(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateRmCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -114,11 +112,9 @@ func TestStateRm_backupExplicit(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateRmCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -150,11 +146,9 @@ func TestStateRm_noState(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateRmCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } diff --git a/command/state_show.go b/command/state_show.go index a7a939bff..235481f2c 100644 --- a/command/state_show.go +++ b/command/state_show.go @@ -12,6 +12,7 @@ import ( // StateShowCommand is a Command implementation that shows a single resource. type StateShowCommand struct { + Meta StateMeta } diff --git a/command/state_show_test.go b/command/state_show_test.go index f8b56c25f..9e0ede396 100644 --- a/command/state_show_test.go +++ b/command/state_show_test.go @@ -34,11 +34,9 @@ func TestStateShow(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateShowCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -94,11 +92,9 @@ func TestStateShow_multi(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateShowCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -118,11 +114,9 @@ func TestStateShow_noState(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateShowCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -140,11 +134,9 @@ func TestStateShow_emptyState(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateShowCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } @@ -171,11 +163,9 @@ func TestStateShow_emptyStateWithModule(t *testing.T) { p := testProvider() ui := new(cli.MockUi) c := &StateShowCommand{ - StateMeta: StateMeta{ - Meta: Meta{ - ContextOpts: testCtxConfig(p), - Ui: ui, - }, + Meta: Meta{ + ContextOpts: testCtxConfig(p), + Ui: ui, }, } diff --git a/commands.go b/commands.go index e1618003e..b9343301c 100644 --- a/commands.go +++ b/commands.go @@ -46,11 +46,6 @@ func init() { "debug": struct{}{}, // includes all subcommands } - // meta struct used in state commands - stateMeta := command.StateMeta{ - Meta: meta, - } - Commands = map[string]cli.CommandFactory{ "apply": func() (cli.Command, error) { return &command.ApplyCommand{ @@ -221,44 +216,42 @@ func init() { }, "state": func() (cli.Command, error) { - return &command.StateCommand{ - StateMeta: stateMeta, - }, nil + return &command.StateCommand{}, nil }, "state list": func() (cli.Command, error) { return &command.StateListCommand{ - StateMeta: stateMeta, + Meta: meta, }, nil }, "state rm": func() (cli.Command, error) { return &command.StateRmCommand{ - StateMeta: stateMeta, + Meta: meta, }, nil }, "state mv": func() (cli.Command, error) { return &command.StateMvCommand{ - StateMeta: stateMeta, + Meta: meta, }, nil }, "state pull": func() (cli.Command, error) { return &command.StatePullCommand{ - StateMeta: stateMeta, + Meta: meta, }, nil }, "state push": func() (cli.Command, error) { return &command.StatePushCommand{ - StateMeta: stateMeta, + Meta: meta, }, nil }, "state show": func() (cli.Command, error) { return &command.StateShowCommand{ - StateMeta: stateMeta, + Meta: meta, }, nil }, }