Merge pull request #2920 from TimeIncOSS/all-outputs

core: Print all outputs
This commit is contained in:
Paul Hinze 2015-08-12 18:23:55 -05:00
commit 6e79494fc6
2 changed files with 39 additions and 9 deletions

View File

@ -3,6 +3,7 @@ package command
import ( import (
"flag" "flag"
"fmt" "fmt"
"sort"
"strings" "strings"
) )
@ -26,14 +27,18 @@ func (c *OutputCommand) Run(args []string) int {
} }
args = cmdFlags.Args() args = cmdFlags.Args()
if len(args) != 1 || args[0] == "" { if len(args) > 1 {
c.Ui.Error( c.Ui.Error(
"The output command expects exactly one argument with the name\n" + "The output command expects exactly one argument with the name\n" +
"of an output variable.\n") "of an output variable or no arguments to show all outputs.\n")
cmdFlags.Usage() cmdFlags.Usage()
return 1 return 1
} }
name := args[0]
name := ""
if len(args) > 0 {
name = args[0]
}
stateStore, err := c.Meta.State() stateStore, err := c.Meta.State()
if err != nil { if err != nil {
@ -68,6 +73,20 @@ func (c *OutputCommand) Run(args []string) int {
return 1 return 1
} }
if name == "" {
ks := make([]string, 0, len(mod.Outputs))
for k, _ := range mod.Outputs {
ks = append(ks, k)
}
sort.Strings(ks)
for _, k := range ks {
v := mod.Outputs[k]
c.Ui.Output(fmt.Sprintf("%s = %s", k, v))
}
return 0
}
v, ok := mod.Outputs[name] v, ok := mod.Outputs[name]
if !ok { if !ok {
c.Ui.Error(fmt.Sprintf( c.Ui.Error(fmt.Sprintf(
@ -84,10 +103,10 @@ func (c *OutputCommand) Run(args []string) int {
func (c *OutputCommand) Help() string { func (c *OutputCommand) Help() string {
helpText := ` helpText := `
Usage: terraform output [options] NAME Usage: terraform output [options] [NAME]
Reads an output variable from a Terraform state file and prints Reads an output variable from a Terraform state file and prints
the value. the value. If NAME is not specified, all outputs are printed.
Options: Options:
@ -96,6 +115,9 @@ Options:
-no-color If specified, output won't contain any color. -no-color If specified, output won't contain any color.
-module=name If specified, returns the outputs for a
specific module
` `
return strings.TrimSpace(helpText) return strings.TrimSpace(helpText)
} }

View File

@ -162,6 +162,7 @@ func TestOutput_blank(t *testing.T) {
Path: []string{"root"}, Path: []string{"root"},
Outputs: map[string]string{ Outputs: map[string]string{
"foo": "bar", "foo": "bar",
"name": "john-doe",
}, },
}, },
}, },
@ -181,9 +182,16 @@ func TestOutput_blank(t *testing.T) {
"-state", statePath, "-state", statePath,
"", "",
} }
if code := c.Run(args); code != 1 {
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
} }
expectedOutput := "foo = bar\nname = john-doe\n"
output := ui.OutputWriter.String()
if output != expectedOutput {
t.Fatalf("Expected output: %#v\ngiven: %#v", expectedOutput, output)
}
} }
func TestOutput_manyArgs(t *testing.T) { func TestOutput_manyArgs(t *testing.T) {