core: show outputs after `terraform refresh`

closes #25
This commit is contained in:
Paul Hinze 2015-06-15 10:40:56 -05:00
parent 1ebe117085
commit ad680b1832
4 changed files with 78 additions and 31 deletions

View File

@ -230,38 +230,10 @@ func (c *ApplyCommand) Run(args []string) int {
c.Meta.StateOutPath()))) c.Meta.StateOutPath())))
} }
// If we have outputs, then output those at the end. if !c.Destroy {
var outputs map[string]string if outputs := outputsAsString(state); outputs != "" {
if !c.Destroy && state != nil { c.Ui.Output(c.Colorize().Color(outputs))
outputs = state.RootModule().Outputs
}
if len(outputs) > 0 {
outputBuf := new(bytes.Buffer)
outputBuf.WriteString("[reset][bold][green]\nOutputs:\n\n")
// Output the outputs in alphabetical order
keyLen := 0
keys := make([]string, 0, len(outputs))
for key, _ := range outputs {
keys = append(keys, key)
if len(key) > keyLen {
keyLen = len(key)
}
} }
sort.Strings(keys)
for _, k := range keys {
v := outputs[k]
outputBuf.WriteString(fmt.Sprintf(
" %s%s = %s\n",
k,
strings.Repeat(" ", keyLen-len(k)),
v))
}
c.Ui.Output(c.Colorize().Color(
strings.TrimSpace(outputBuf.String())))
} }
return 0 return 0
@ -373,3 +345,38 @@ Options:
` `
return strings.TrimSpace(helpText) return strings.TrimSpace(helpText)
} }
func outputsAsString(state *terraform.State) string {
if state == nil {
return ""
}
outputs := state.RootModule().Outputs
outputBuf := new(bytes.Buffer)
if len(outputs) > 0 {
outputBuf.WriteString("[reset][bold][green]\nOutputs:\n\n")
// Output the outputs in alphabetical order
keyLen := 0
keys := make([]string, 0, len(outputs))
for key, _ := range outputs {
keys = append(keys, key)
if len(key) > keyLen {
keyLen = len(key)
}
}
sort.Strings(keys)
for _, k := range keys {
v := outputs[k]
outputBuf.WriteString(fmt.Sprintf(
" %s%s = %s\n",
k,
strings.Repeat(" ", keyLen-len(k)),
v))
}
}
return strings.TrimSpace(outputBuf.String())
}

View File

@ -105,6 +105,10 @@ func (c *RefreshCommand) Run(args []string) int {
return 1 return 1
} }
if outputs := outputsAsString(newState); outputs != "" {
c.Ui.Output(c.Colorize().Color(outputs))
}
return 0 return 0
} }

View File

@ -581,6 +581,35 @@ func TestRefresh_disableBackup(t *testing.T) {
} }
} }
func TestRefresh_displaysOutputs(t *testing.T) {
state := testState()
statePath := testStateFile(t, state)
p := testProvider()
ui := new(cli.MockUi)
c := &RefreshCommand{
Meta: Meta{
ContextOpts: testCtxConfig(p),
Ui: ui,
},
}
args := []string{
"-state", statePath,
testFixturePath("refresh-output"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
// Test that outputs were displayed
outputValue := "foo.example.com"
actual := ui.OutputWriter.String()
if !strings.Contains(actual, outputValue) {
t.Fatalf("Expected:\n%s\n\nTo include: %q", actual, outputValue)
}
}
const refreshVarFile = ` const refreshVarFile = `
foo = "bar" foo = "bar"
` `

View File

@ -0,0 +1,7 @@
resource "test_instance" "foo" {
ami = "bar"
}
output "endpoint" {
value = "foo.example.com"
}