diff --git a/command/fmt.go b/command/fmt.go index 49e06c08e..9c9e6c22f 100644 --- a/command/fmt.go +++ b/command/fmt.go @@ -34,13 +34,19 @@ func (c *FmtCommand) Run(args []string) int { } args = cmdFlags.Args() - if len(args) > 0 { - c.Ui.Error("The fmt command expects no arguments.") + if len(args) > 1 { + c.Ui.Error("The fmt command expects at most one argument.") cmdFlags.Usage() return 1 } - dir := "." + var dir string + if len(args) == 0 { + dir = "." + } else { + dir = args[0] + } + output := &cli.UiWriter{Ui: c.Ui} err := fmtcmd.Run([]string{dir}, []string{fileExtension}, nil, output, c.opts) if err != nil { @@ -53,10 +59,11 @@ func (c *FmtCommand) Run(args []string) int { func (c *FmtCommand) Help() string { helpText := ` -Usage: terraform fmt [options] +Usage: terraform fmt [options] [DIR] - Rewrites all Terraform configuration files in the current working - directory to a canonical format. + Rewrites all Terraform configuration files to a canonical format. + + If DIR is not specified then the current working directory will be used. Options: diff --git a/command/fmt_test.go b/command/fmt_test.go index d38d54644..b9d69fe66 100644 --- a/command/fmt_test.go +++ b/command/fmt_test.go @@ -47,12 +47,15 @@ func TestFmt_tooManyArgs(t *testing.T) { }, } - args := []string{"bad"} + args := []string{ + "one", + "two", + } if code := c.Run(args); code != 1 { t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String()) } - expected := "The fmt command expects no arguments." + expected := "The fmt command expects at most one argument." if actual := ui.ErrorWriter.String(); !strings.Contains(actual, expected) { t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected) } @@ -94,6 +97,32 @@ func TestFmt_workingDirectory(t *testing.T) { } } +func TestFmt_directoryArg(t *testing.T) { + tempDir, err := fmtFixtureWriteDir() + if err != nil { + t.Fatalf("err: %s", err) + } + defer os.RemoveAll(tempDir) + + ui := new(cli.MockUi) + c := &FmtCommand{ + Meta: Meta{ + ContextOpts: testCtxConfig(testProvider()), + Ui: ui, + }, + } + + args := []string{tempDir} + if code := c.Run(args); code != 0 { + t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String()) + } + + expected := fmt.Sprintf("%s\n", filepath.Join(tempDir, fmtFixture.filename)) + if actual := ui.OutputWriter.String(); actual != expected { + t.Fatalf("got: %q\nexpected: %q", actual, expected) + } +} + var fmtFixture = struct { filename string input, golden []byte diff --git a/website/source/docs/commands/fmt.html.markdown b/website/source/docs/commands/fmt.html.markdown index 7e5d7647b..51802784b 100644 --- a/website/source/docs/commands/fmt.html.markdown +++ b/website/source/docs/commands/fmt.html.markdown @@ -15,7 +15,9 @@ to a canonical format and style. Usage: `terraform fmt [options] [DIR]` -`fmt` scans the current directory for configuration files. +By default, `fmt` scans the current directory for configuration files. If +the `dir` argument is provided then it will scan that given directory +instead. The command-line flags are all optional. The list of available flags are: