Merge pull request #28191 from zimbatm/terraform-fmt-manyargs

command/fmt: support formatting multiple files
This commit is contained in:
Alisdair McDiarmid 2022-08-12 13:53:23 -04:00 committed by GitHub
commit 27966044ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 20 deletions

View File

@ -55,11 +55,6 @@ func (c *FmtCommand) Run(args []string) int {
}
args = cmdFlags.Args()
if len(args) > 1 {
c.Ui.Error("The fmt command expects at most one argument.")
cmdFlags.Usage()
return 1
}
var paths []string
if len(args) == 0 {
@ -68,7 +63,7 @@ func (c *FmtCommand) Run(args []string) int {
c.list = false
c.write = false
} else {
paths = []string{args[0]}
paths = args
}
var output io.Writer
@ -528,15 +523,17 @@ func (c *FmtCommand) trimNewlines(tokens hclwrite.Tokens) hclwrite.Tokens {
func (c *FmtCommand) Help() string {
helpText := `
Usage: terraform [global options] fmt [options] [TARGET]
Usage: terraform [global options] fmt [options] [target...]
Rewrites all Terraform configuration files to a canonical format. Both
configuration files (.tf) and variables files (.tfvars) are updated.
JSON files (.tf.json or .tfvars.json) are not modified.
If TARGET is not specified, the command uses the current working directory.
If TARGET is a file, the command only uses the specified file. If TARGET
is "-" then the command reads from STDIN.
By default, fmt scans the current directory for configuration files. If 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).
The content must be in the Terraform language native syntax; JSON is not
supported.

View File

@ -166,7 +166,16 @@ func TestFmt_snippetInError(t *testing.T) {
}
}
func TestFmt_tooManyArgs(t *testing.T) {
func TestFmt_manyArgs(t *testing.T) {
tempDir := fmtFixtureWriteDir(t)
// Add a second file
secondSrc := `locals { x = 1 }`
err := ioutil.WriteFile(filepath.Join(tempDir, "second.tf"), []byte(secondSrc), 0644)
if err != nil {
t.Fatal(err)
}
ui := new(cli.MockUi)
c := &FmtCommand{
Meta: Meta{
@ -176,16 +185,21 @@ func TestFmt_tooManyArgs(t *testing.T) {
}
args := []string{
"one",
"two",
filepath.Join(tempDir, "main.tf"),
filepath.Join(tempDir, "second.tf"),
}
if code := c.Run(args); code != 1 {
if code := c.Run(args); code != 0 {
t.Fatalf("wrong exit code. errors: \n%s", ui.ErrorWriter.String())
}
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)
got, err := filepath.Abs(strings.TrimSpace(ui.OutputWriter.String()))
if err != nil {
t.Fatal(err)
}
want := filepath.Join(tempDir, fmtFixture.filename)
if got != want {
t.Fatalf("wrong output\ngot: %s\nwant: %s", got, want)
}
}

View File

@ -45,10 +45,13 @@ and the generated files.
## Usage
Usage: `terraform fmt [options] [TARGET]`
Usage: `terraform fmt [options] [target...]`
By default, `fmt` scans the current directory for configuration files. If
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).
By default, `fmt` scans the current directory for configuration files. If 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).
The command-line flags are all optional. The following flags are available: