Skip to content

Commit 2796604

Browse files
authored
Merge pull request #28191 from zimbatm/terraform-fmt-manyargs
command/fmt: support formatting multiple files
2 parents 2aff678 + 7a9ccc0 commit 2796604

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

internal/command/fmt.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ func (c *FmtCommand) Run(args []string) int {
5555
}
5656

5757
args = cmdFlags.Args()
58-
if len(args) > 1 {
59-
c.Ui.Error("The fmt command expects at most one argument.")
60-
cmdFlags.Usage()
61-
return 1
62-
}
6358

6459
var paths []string
6560
if len(args) == 0 {
@@ -68,7 +63,7 @@ func (c *FmtCommand) Run(args []string) int {
6863
c.list = false
6964
c.write = false
7065
} else {
71-
paths = []string{args[0]}
66+
paths = args
7267
}
7368

7469
var output io.Writer
@@ -528,15 +523,17 @@ func (c *FmtCommand) trimNewlines(tokens hclwrite.Tokens) hclwrite.Tokens {
528523

529524
func (c *FmtCommand) Help() string {
530525
helpText := `
531-
Usage: terraform [global options] fmt [options] [TARGET]
526+
Usage: terraform [global options] fmt [options] [target...]
532527
533528
Rewrites all Terraform configuration files to a canonical format. Both
534529
configuration files (.tf) and variables files (.tfvars) are updated.
535530
JSON files (.tf.json or .tfvars.json) are not modified.
536531
537-
If TARGET is not specified, the command uses the current working directory.
538-
If TARGET is a file, the command only uses the specified file. If TARGET
539-
is "-" then the command reads from STDIN.
532+
By default, fmt scans the current directory for configuration files. If you
533+
provide a directory for the target argument, then fmt will scan that
534+
directory instead. If you provide a file, then fmt will process just that
535+
file. If you provide a single dash ("-"), then fmt will read from standard
536+
input (STDIN).
540537
541538
The content must be in the Terraform language native syntax; JSON is not
542539
supported.

internal/command/fmt_test.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,16 @@ func TestFmt_snippetInError(t *testing.T) {
166166
}
167167
}
168168

169-
func TestFmt_tooManyArgs(t *testing.T) {
169+
func TestFmt_manyArgs(t *testing.T) {
170+
tempDir := fmtFixtureWriteDir(t)
171+
// Add a second file
172+
secondSrc := `locals { x = 1 }`
173+
174+
err := ioutil.WriteFile(filepath.Join(tempDir, "second.tf"), []byte(secondSrc), 0644)
175+
if err != nil {
176+
t.Fatal(err)
177+
}
178+
170179
ui := new(cli.MockUi)
171180
c := &FmtCommand{
172181
Meta: Meta{
@@ -176,16 +185,21 @@ func TestFmt_tooManyArgs(t *testing.T) {
176185
}
177186

178187
args := []string{
179-
"one",
180-
"two",
188+
filepath.Join(tempDir, "main.tf"),
189+
filepath.Join(tempDir, "second.tf"),
181190
}
182-
if code := c.Run(args); code != 1 {
191+
if code := c.Run(args); code != 0 {
183192
t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
184193
}
185194

186-
expected := "The fmt command expects at most one argument."
187-
if actual := ui.ErrorWriter.String(); !strings.Contains(actual, expected) {
188-
t.Fatalf("expected:\n%s\n\nto include: %q", actual, expected)
195+
got, err := filepath.Abs(strings.TrimSpace(ui.OutputWriter.String()))
196+
if err != nil {
197+
t.Fatal(err)
198+
}
199+
want := filepath.Join(tempDir, fmtFixture.filename)
200+
201+
if got != want {
202+
t.Fatalf("wrong output\ngot: %s\nwant: %s", got, want)
189203
}
190204
}
191205

website/docs/cli/commands/fmt.mdx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ and the generated files.
4545

4646
## Usage
4747

48-
Usage: `terraform fmt [options] [TARGET]`
48+
Usage: `terraform fmt [options] [target...]`
4949

50-
By default, `fmt` scans the current directory for configuration files. If
51-
you provide a directory for the `target` argument, then `fmt` will scan that directory instead. If you provide a file, then `fmt` will process just that file. If you provide a single dash (`-`), then `fmt` will read from standard input (STDIN).
50+
By default, `fmt` scans the current directory for configuration files. If you
51+
provide a directory for the `target` argument, then `fmt` will scan that
52+
directory instead. If you provide a file, then `fmt` will process just that
53+
file. If you provide a single dash (`-`), then `fmt` will read from standard
54+
input (STDIN).
5255

5356
The command-line flags are all optional. The following flags are available:
5457

0 commit comments

Comments
 (0)