Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Print all outputs #2920

Merged
merged 2 commits into from
Aug 12, 2015
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
36 changes: 29 additions & 7 deletions command/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"flag"
"fmt"
"sort"
"strings"
)

Expand All @@ -26,14 +27,18 @@ func (c *OutputCommand) Run(args []string) int {
}

args = cmdFlags.Args()
if len(args) != 1 || args[0] == "" {
if len(args) > 1 {
c.Ui.Error(
"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()
return 1
}
name := args[0]

name := ""
if len(args) > 0 {
name = args[0]
}

stateStore, err := c.Meta.State()
if err != nil {
Expand Down Expand Up @@ -68,6 +73,20 @@ func (c *OutputCommand) Run(args []string) int {
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]
if !ok {
c.Ui.Error(fmt.Sprintf(
Expand All @@ -84,17 +103,20 @@ func (c *OutputCommand) Run(args []string) int {

func (c *OutputCommand) Help() string {
helpText := `
Usage: terraform output [options] NAME
Usage: terraform output [options] [NAME]

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:

-state=path Path to the state file to read. Defaults to
"terraform.tfstate".
"terraform.tfstate".

-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)
Expand Down
12 changes: 10 additions & 2 deletions command/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ func TestOutput_blank(t *testing.T) {
&terraform.ModuleState{
Path: []string{"root"},
Outputs: map[string]string{
"foo": "bar",
"foo": "bar",
"name": "john-doe",
},
},
},
Expand All @@ -181,9 +182,16 @@ func TestOutput_blank(t *testing.T) {
"-state", statePath,
"",
}
if code := c.Run(args); code != 1 {

if code := c.Run(args); code != 0 {
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) {
Expand Down