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:
parent
c753390399
commit
1b967e612f
|
@ -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:
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
||||
|
|
Loading…
Reference in New Issue