From 79e2753e4124602d4499716f133d1e87058f5b11 Mon Sep 17 00:00:00 2001 From: Dan Carley Date: Tue, 2 Feb 2016 22:10:23 +0000 Subject: [PATCH] command/fmt: Disable list/write when using STDIN These options don't make sense when passing STDIN. `-write` will raise an error because there is no file to write to. `-list` will always say ``. So disable whenever using STDIN, making the command much simpler: cat main.tf | terraform fmt - --- command/fmt.go | 9 ++++++--- command/fmt_test.go | 6 +----- website/source/docs/commands/fmt.html.markdown | 5 +++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/command/fmt.go b/command/fmt.go index f0a7bfa79..d9ccc2643 100644 --- a/command/fmt.go +++ b/command/fmt.go @@ -51,7 +51,10 @@ func (c *FmtCommand) Run(args []string) int { var dirs []string if len(args) == 0 { dirs = []string{"."} - } else if args[0] != stdinArg { + } else if args[0] == stdinArg { + c.opts.List = false + c.opts.Write = false + } else { dirs = []string{args[0]} } @@ -76,9 +79,9 @@ Usage: terraform fmt [options] [DIR] Options: - -list List files whose formatting differs + -list List files whose formatting differs (disabled if using STDIN) - -write Write result to source file instead of STDOUT + -write Write result to source file instead of STDOUT (disabled if using STDIN) -diff Display diffs instead of rewriting files diff --git a/command/fmt_test.go b/command/fmt_test.go index de117bb2c..0a7f26fdb 100644 --- a/command/fmt_test.go +++ b/command/fmt_test.go @@ -137,11 +137,7 @@ func TestFmt_stdinArg(t *testing.T) { input: input, } - args := []string{ - "-write=false", - "-list=false", - "-", - } + args := []string{"-"} if code := c.Run(args); code != 0 { t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String()) } diff --git a/website/source/docs/commands/fmt.html.markdown b/website/source/docs/commands/fmt.html.markdown index 9f33e98e3..bb48ae957 100644 --- a/website/source/docs/commands/fmt.html.markdown +++ b/website/source/docs/commands/fmt.html.markdown @@ -22,6 +22,7 @@ input (STDIN). The command-line flags are all optional. The list of available flags are: -* `-list=true` - List files whose formatting differs -* `-write=true` - Write result to source file instead of STDOUT +* `-list=true` - List files whose formatting differs (disabled if using STDIN) +* `-write=true` - Write result to source file instead of STDOUT (disabled if + using STDIN) * `-diff=false` - Display diffs instead of rewriting files