Skip to content

Commit

Permalink
Merge pull request hashicorp#27609 from hashicorp/alisdair/remove-out…
Browse files Browse the repository at this point in the history
…put-module-option

cli: Remove dead code for output -module flag
  • Loading branch information
alisdair authored Jan 27, 2021
2 parents dc757c4 + 2e62e03 commit 7cb5685
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 67 deletions.
11 changes: 3 additions & 8 deletions command/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"sort"
"strings"

"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/repl"
"github.com/hashicorp/terraform/states"
Expand Down Expand Up @@ -178,7 +177,7 @@ func (c *ApplyCommand) Run(args []string) int {
}

if !c.Destroy {
if outputs := outputsAsString(op.State, addrs.RootModuleInstance, true); outputs != "" {
if outputs := outputsAsString(op.State, true); outputs != "" {
c.Ui.Output(c.Colorize().Color(outputs))
}
}
Expand Down Expand Up @@ -315,16 +314,12 @@ Options:
return strings.TrimSpace(helpText)
}

func outputsAsString(state *states.State, modPath addrs.ModuleInstance, includeHeader bool) string {
func outputsAsString(state *states.State, includeHeader bool) string {
if state == nil {
return ""
}

ms := state.Module(modPath)
if ms == nil {
return ""
}

ms := state.RootModule()
outputs := ms.OutputValues
outputBuf := new(bytes.Buffer)
if len(outputs) > 0 {
Expand Down
31 changes: 3 additions & 28 deletions command/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/zclconf/go-cty/cty/convert"
ctyjson "github.com/zclconf/go-cty/cty/json"

"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/repl"
"github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/tfdiags"
Expand All @@ -27,13 +26,12 @@ type OutputCommand struct {

func (c *OutputCommand) Run(args []string) int {
args = c.Meta.process(args)
var module, statePath string
var statePath string
var jsonOutput, rawOutput bool
cmdFlags := c.Meta.defaultFlagSet("output")
cmdFlags.BoolVar(&jsonOutput, "json", false, "json")
cmdFlags.BoolVar(&rawOutput, "raw", false, "raw")
cmdFlags.StringVar(&statePath, "state", "", "path")
cmdFlags.StringVar(&module, "module", "", "module")
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
Expand Down Expand Up @@ -101,35 +99,12 @@ func (c *OutputCommand) Run(args []string) int {
return 1
}

moduleAddr := addrs.RootModuleInstance
if module != "" {
// This option was supported prior to 0.12.0, but no longer supported
// because we only persist the root module outputs in state.
// (We could perhaps re-introduce this by doing an eval walk here to
// repopulate them, similar to how "terraform console" does it, but
// that requires more thought since it would imply this command
// supporting remote operations, which is a big change.)
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Unsupported option",
"The -module option is no longer supported since Terraform 0.12, because now only root outputs are persisted in the state.",
))
c.showDiagnostics(diags)
return 1
}

state := stateStore.State()
if state == nil {
state = states.NewState()
}

mod := state.Module(moduleAddr)
if mod == nil {
c.Ui.Error(fmt.Sprintf(
"The module %s could not be found. There is nothing to output.",
module))
return 1
}
mod := state.RootModule()

if !jsonOutput && (state.Empty() || len(mod.OutputValues) == 0) {
diags = diags.Append(tfdiags.Sourceless(
Expand Down Expand Up @@ -190,7 +165,7 @@ func (c *OutputCommand) Run(args []string) int {
c.Ui.Output(string(jsonOutputs))
return 0
} else {
c.Ui.Output(outputsAsString(state, moduleAddr, false))
c.Ui.Output(outputsAsString(state, false))
return 0
}
}
Expand Down
29 changes: 0 additions & 29 deletions command/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,35 +268,6 @@ func TestOutput_jsonEmptyOutputs(t *testing.T) {
}
}

func TestMissingModuleOutput(t *testing.T) {
originalState := states.BuildState(func(s *states.SyncState) {
s.SetOutputValue(
addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("bar"),
false,
)
})
statePath := testStateFile(t, originalState)

ui := new(cli.MockUi)
c := &OutputCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
},
}

args := []string{
"-state", statePath,
"-module", "not_existing_module",
"blah",
}

if code := c.Run(args); code != 1 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
}
}

func TestOutput_badVar(t *testing.T) {
originalState := states.BuildState(func(s *states.SyncState) {
s.SetOutputValue(
Expand Down
3 changes: 1 addition & 2 deletions command/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"strings"

"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/tfdiags"
)
Expand Down Expand Up @@ -96,7 +95,7 @@ func (c *RefreshCommand) Run(args []string) int {
return op.Result.ExitStatus()
}

if outputs := outputsAsString(op.State, addrs.RootModuleInstance, true); outputs != "" {
if outputs := outputsAsString(op.State, true); outputs != "" {
c.Ui.Output(c.Colorize().Color(outputs))
}

Expand Down

0 comments on commit 7cb5685

Please sign in to comment.