command/fmt: Accept optional directory argument

So that you can operate on files in a directory other than your current
working directory.
This commit is contained in:
Dan Carley 2016-02-02 22:10:22 +00:00
parent c753390399
commit 1b967e612f
3 changed files with 47 additions and 9 deletions

View File

@ -34,13 +34,19 @@ func (c *FmtCommand) Run(args []string) int {
} }
args = cmdFlags.Args() args = cmdFlags.Args()
if len(args) > 0 { if len(args) > 1 {
c.Ui.Error("The fmt command expects no arguments.") c.Ui.Error("The fmt command expects at most one argument.")
cmdFlags.Usage() cmdFlags.Usage()
return 1 return 1
} }
dir := "." var dir string
if len(args) == 0 {
dir = "."
} else {
dir = args[0]
}
output := &cli.UiWriter{Ui: c.Ui} output := &cli.UiWriter{Ui: c.Ui}
err := fmtcmd.Run([]string{dir}, []string{fileExtension}, nil, output, c.opts) err := fmtcmd.Run([]string{dir}, []string{fileExtension}, nil, output, c.opts)
if err != nil { if err != nil {
@ -53,10 +59,11 @@ func (c *FmtCommand) Run(args []string) int {
func (c *FmtCommand) Help() string { func (c *FmtCommand) Help() string {
helpText := ` helpText := `
Usage: terraform fmt [options] Usage: terraform fmt [options] [DIR]
Rewrites all Terraform configuration files in the current working Rewrites all Terraform configuration files to a canonical format.
directory to a canonical format.
If DIR is not specified then the current working directory will be used.
Options: Options:

View File

@ -47,12 +47,15 @@ func TestFmt_tooManyArgs(t *testing.T) {
}, },
} }
args := []string{"bad"} args := []string{
"one",
"two",
}
if code := c.Run(args); code != 1 { if code := c.Run(args); code != 1 {
t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String()) 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) { if actual := ui.ErrorWriter.String(); !strings.Contains(actual, expected) {
t.Fatalf("expected:\n%s\n\nto include: %q", 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 { var fmtFixture = struct {
filename string filename string
input, golden []byte input, golden []byte

View File

@ -15,7 +15,9 @@ to a canonical format and style.
Usage: `terraform fmt [options] [DIR]` 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: The command-line flags are all optional. The list of available flags are: