mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-24 08:00:17 -06:00
Merge pull request #28191 from zimbatm/terraform-fmt-manyargs
command/fmt: support formatting multiple files
This commit is contained in:
commit
27966044ba
@ -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.
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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:
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user