Skip to content

Commit 64561c3

Browse files
committed
Apply format set in config file
1 parent f92cc22 commit 64561c3

File tree

6 files changed

+34
-1
lines changed

6 files changed

+34
-1
lines changed

cmd/cli.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func (cli *CLI) Run(args []string) int {
6464
cli.formatter = &formatter.Formatter{
6565
Stdout: cli.outStream,
6666
Stderr: cli.errStream,
67+
// NOTE: The format may be set in config file, but the flag will take precedence until it is loaded.
6768
Format: opts.Format,
6869
Fix: opts.Fix,
6970
}

cmd/inspect.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ func (cli *CLI) inspectModule(opts Options, dir string, filterFiles []string) (t
9393
return issues, changes, fmt.Errorf("Failed to load TFLint config; %w", err)
9494
}
9595
cli.config.Merge(opts.toConfig())
96+
// Apply format set in config file
97+
cli.formatter.Format = cli.config.Format
9698

9799
// Setup loader
98100
cli.loader, err = terraform.NewLoader(afero.Afero{Fs: afero.NewOsFs()}, cli.originalWorkingDir)

cmd/inspect_parallel.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ func (cli *CLI) inspectParallel(opts Options) int {
9090
force = *opts.Force
9191
}
9292

93+
// Parallel inspection ignores the format set in the config file
94+
// and the --format CLI flag always takes precedence.
9395
if err := cli.formatter.PrintParallel(issues, cli.sources); err != nil {
9496
return ExitCodeError
9597
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
config {
2+
format = "json"
3+
}

integrationtest/cli/cli_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,27 @@ func TestIntegration(t *testing.T) {
5050
stdout: "",
5151
},
5252
{
53-
name: "specify format",
53+
name: "--format option",
5454
command: "./tflint --format json",
5555
dir: "no_issues",
5656
status: cmd.ExitCodeOK,
5757
stdout: "[]",
5858
},
59+
{
60+
name: "format config",
61+
command: "./tflint",
62+
dir: "format_config",
63+
status: cmd.ExitCodeOK,
64+
stdout: `<?xml version="1.0" encoding="UTF-8"?>
65+
<checkstyle></checkstyle>`,
66+
},
67+
{
68+
name: "--format + config",
69+
command: "./tflint --format json",
70+
dir: "format_config",
71+
status: cmd.ExitCodeOK,
72+
stdout: "[]",
73+
},
5974
{
6075
name: "`--force` option with no issues",
6176
command: "./tflint --force",
@@ -379,6 +394,13 @@ func TestIntegration(t *testing.T) {
379394
status: cmd.ExitCodeIssuesFound,
380395
stdout: fmt.Sprintf("%s (aws_instance_example_type)", color.New(color.Bold).Sprint("instance type is m5.2xlarge")),
381396
},
397+
{
398+
name: "--chdir and format config",
399+
command: "./tflint --chdir=subdir", // Apply config in subdir
400+
dir: "chdir_format",
401+
status: cmd.ExitCodeOK,
402+
stdout: "[]",
403+
},
382404
{
383405
name: "invalid max workers",
384406
command: "./tflint --max-workers=0",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
config {
2+
format = "checkstyle"
3+
}

0 commit comments

Comments
 (0)