Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 8 additions & 46 deletions command/apply.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package command

import (
"bytes"
"fmt"
"sort"
"strings"

"github.com/hashicorp/terraform/backend"
"github.com/hashicorp/terraform/command/arguments"
"github.com/hashicorp/terraform/command/views"
"github.com/hashicorp/terraform/plans/planfile"
"github.com/hashicorp/terraform/repl"
"github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/tfdiags"
)

Expand Down Expand Up @@ -230,9 +228,12 @@ func (c *ApplyCommand) Run(args []string) int {
c.Meta.stateOutPath)))
}

if !c.Destroy {
if outputs := outputsAsString(op.State, true); outputs != "" {
c.Ui.Output(c.Colorize().Color(outputs))
if !c.Destroy && op.State != nil {
outputValues := op.State.RootModule().OutputValues
if len(outputValues) > 0 {
c.Ui.Output(c.Colorize().Color("[reset][bold][green]\nOutputs:\n\n"))
view := views.NewOutput(arguments.ViewHuman, c.View)
view.Output("", outputValues)
}
}

Expand Down Expand Up @@ -366,45 +367,6 @@ Options:
return strings.TrimSpace(helpText)
}

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

ms := state.RootModule()
outputs := ms.OutputValues
outputBuf := new(bytes.Buffer)
if len(outputs) > 0 {
if includeHeader {
outputBuf.WriteString("[reset][bold][green]\nOutputs:\n\n")
}

// Output the outputs in alphabetical order
keyLen := 0
ks := make([]string, 0, len(outputs))
for key := range outputs {
ks = append(ks, key)
if len(key) > keyLen {
keyLen = len(key)
}
}
sort.Strings(ks)

for _, k := range ks {
v := outputs[k]
if v.Sensitive {
outputBuf.WriteString(fmt.Sprintf("%s = <sensitive>\n", k))
continue
}

result := repl.FormatValue(v.Value, 0)
outputBuf.WriteString(fmt.Sprintf("%s = %s\n", k, result))
}
}

return strings.TrimSpace(outputBuf.String())
}

const outputInterrupt = `Interrupt received.
Please wait for Terraform to exit or data loss may occur.
Gracefully shutting down...`
16 changes: 16 additions & 0 deletions command/apply_destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ func TestApply_destroy(t *testing.T) {
}

ui := new(cli.MockUi)
view, _ := testView(t)
c := &ApplyCommand{
Destroy: true,
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
View: view,
},
}

Expand Down Expand Up @@ -157,11 +159,13 @@ func TestApply_destroyApproveNo(t *testing.T) {

p := applyFixtureProvider()
ui := new(cli.MockUi)
view, _ := testView(t)
c := &ApplyCommand{
Destroy: true,
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
View: view,
},
}

Expand Down Expand Up @@ -206,11 +210,13 @@ func TestApply_destroyApproveYes(t *testing.T) {
defaultInputWriter = new(bytes.Buffer)

ui := new(cli.MockUi)
view, _ := testView(t)
c := &ApplyCommand{
Destroy: true,
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
View: view,
},
}

Expand Down Expand Up @@ -271,11 +277,13 @@ func TestApply_destroyLockedState(t *testing.T) {

p := testProvider()
ui := new(cli.MockUi)
view, _ := testView(t)
c := &ApplyCommand{
Destroy: true,
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
View: view,
},
}

Expand Down Expand Up @@ -306,11 +314,13 @@ func TestApply_destroyPlan(t *testing.T) {

p := testProvider()
ui := new(cli.MockUi)
view, _ := testView(t)
c := &ApplyCommand{
Destroy: true,
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
View: view,
},
}

Expand All @@ -337,11 +347,13 @@ func TestApply_destroyPath(t *testing.T) {
p := applyFixtureProvider()

ui := new(cli.MockUi)
view, _ := testView(t)
c := &ApplyCommand{
Destroy: true,
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
View: view,
},
}

Expand Down Expand Up @@ -430,11 +442,13 @@ func TestApply_destroyTargetedDependencies(t *testing.T) {
}

ui := new(cli.MockUi)
view, _ := testView(t)
c := &ApplyCommand{
Destroy: true,
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
View: view,
},
}

Expand Down Expand Up @@ -579,11 +593,13 @@ func TestApply_destroyTargeted(t *testing.T) {
}

ui := new(cli.MockUi)
view, _ := testView(t)
c := &ApplyCommand{
Destroy: true,
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
View: view,
},
}

Expand Down
Loading