From 85d1d15d81deb7a486418773994bc6fe4710d33a Mon Sep 17 00:00:00 2001 From: Eric Connell Date: Thu, 25 Jun 2015 16:20:12 -0700 Subject: [PATCH 1/2] output command supports printing all outputs if no output name is specified all outputs are displayed fixed formating and added missing help for -module parameter --- command/output.go | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/command/output.go b/command/output.go index 63a6926f3c7a..f0fb096dc702 100644 --- a/command/output.go +++ b/command/output.go @@ -3,6 +3,7 @@ package command import ( "flag" "fmt" + "sort" "strings" ) @@ -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 { @@ -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( @@ -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) From 382c0ba23d66824304b134a2862eb01c52c5d26e Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Mon, 3 Aug 2015 12:44:41 +0100 Subject: [PATCH 2/2] command/output: Update test since we now allow printing everything --- command/output_test.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/command/output_test.go b/command/output_test.go index f84642269cf8..e8d469029884 100644 --- a/command/output_test.go +++ b/command/output_test.go @@ -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", }, }, }, @@ -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) {