Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: Drop support for CLI arguments #1779

Merged
merged 1 commit into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,22 @@ func (cli *CLI) Run(args []string) int {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Failed to parse CLI options; %w", err), map[string][]byte{})
return ExitCodeError
}
if len(args) > 1 {
cli.formatter.Print(tflint.Issues{}, fmt.Errorf("Command line arguments support was dropped in v0.47. Use --chdir or --filter instead."), map[string][]byte{})
return ExitCodeError
}

switch {
case opts.Version:
if len(args) > 1 {
fmt.Fprintln(cli.errStream, `WARNING: Arguments are not used in version mode and will error in a future version. Use --chdir instead.`)
}
return cli.printVersion(opts)
case opts.Init:
if len(args) > 1 {
fmt.Fprintln(cli.errStream, `WARNING: Arguments are not used in init mode and will error in a future version. Use --chdir instead.`)
}
return cli.init(opts)
case opts.Langserver:
if len(args) > 1 {
fmt.Fprintln(cli.errStream, `WARNING: Arguments are not used in language server mode and will error in a future version.`)
}
return cli.startLanguageServer(opts)
case opts.ActAsBundledPlugin:
return cli.actAsBundledPlugin()
default:
if len(args) > 1 {
fmt.Fprintln(cli.errStream, `WARNING: "tflint FILE/DIR" is deprecated and will error in a future version. Use --chdir or --filter instead.`)
}
return cli.inspect(opts, args)
return cli.inspect(opts)
}
}

Expand Down
64 changes: 3 additions & 61 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"google.golang.org/grpc/status"
)

func (cli *CLI) inspect(opts Options, args []string) int {
func (cli *CLI) inspect(opts Options) int {
// Respect the "--format" flag until a config is loaded
cli.formatter.Format = opts.Format

Expand All @@ -33,22 +33,7 @@ func (cli *CLI) inspect(opts Options, args []string) int {

for _, wd := range workingDirs {
err := cli.withinChangedDir(wd, func() error {
// Parse directory/file arguments after changing the working directory
targetDir, filterFiles, err := processArgs(args[1:])
if err != nil {
return fmt.Errorf("Failed to parse CLI arguments; %w", err)
}

if opts.Chdir != "" && (len(args) > 1 && (len(filterFiles) == 0 || targetDir != ".")) {
return fmt.Errorf("Cannot use --chdir and directory argument at the same time")
}
if opts.Recursive && len(args) > 1 {
return fmt.Errorf("Cannot use --recursive and arguments at the same time")
}
if len(opts.Filter) > 0 && len(args) > 1 {
return fmt.Errorf("Cannot use --filter and arguments at the same time")
}

filterFiles := []string{}
for _, pattern := range opts.Filter {
files, err := filepath.Glob(pattern)
if err != nil {
Expand All @@ -66,7 +51,7 @@ func (cli *CLI) inspect(opts Options, args []string) int {
filterFiles[i] = filepath.Join(wd, file)
}

moduleIssues, moduleChanges, err := cli.inspectModule(opts, targetDir, filterFiles)
moduleIssues, moduleChanges, err := cli.inspectModule(opts, ".", filterFiles)
if err != nil {
return err
}
Expand Down Expand Up @@ -116,49 +101,6 @@ func (cli *CLI) inspect(opts Options, args []string) int {
return ExitCodeOK
}

func processArgs(args []string) (string, []string, error) {
if len(args) == 0 {
return ".", []string{}, nil
}

var dir string
filterFiles := []string{}

for _, file := range args {
fileInfo, err := os.Stat(file)
if err != nil {
if os.IsNotExist(err) {
return dir, filterFiles, fmt.Errorf("Failed to load `%s`: File not found", file)
}
return dir, filterFiles, fmt.Errorf("Failed to load `%s`: %s", file, err)
}

if fileInfo.IsDir() {
dir = file
if len(args) != 1 {
return dir, filterFiles, fmt.Errorf("Failed to load `%s`: Multiple arguments are not allowed when passing a directory", file)
}
return dir, filterFiles, nil
}

if !strings.HasSuffix(file, ".tf") && !strings.HasSuffix(file, ".tf.json") {
return dir, filterFiles, fmt.Errorf("Failed to load `%s`: File is not a target of Terraform", file)
}

fileDir := filepath.Dir(file)
if dir == "" {
dir = fileDir
filterFiles = append(filterFiles, file)
} else if fileDir == dir {
filterFiles = append(filterFiles, file)
} else {
return dir, filterFiles, fmt.Errorf("Failed to load `%s`: Multiple files in different directories are not allowed", file)
}
}

return dir, filterFiles, nil
}

func (cli *CLI) inspectModule(opts Options, dir string, filterFiles []string) (tflint.Issues, map[string][]byte, error) {
issues := tflint.Issues{}
changes := map[string][]byte{}
Expand Down
48 changes: 21 additions & 27 deletions integrationtest/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,69 +242,64 @@ func TestIntegration(t *testing.T) {
name: "files arguments",
command: "./tflint empty.tf",
dir: "multiple_files",
status: cmd.ExitCodeOK,
stdout: "", // main.tf is ignored
stderr: `WARNING: "tflint FILE/DIR" is deprecated and will error in a future version. Use --chdir or --filter instead.`,
status: cmd.ExitCodeError,
stderr: `Command line arguments support was dropped in v0.47. Use --chdir or --filter instead.`,
},
{
name: "file not found",
command: "./tflint not_found.tf",
dir: "multiple_files",
status: cmd.ExitCodeError,
stderr: "Failed to load `not_found.tf`: File not found",
stderr: `Command line arguments support was dropped in v0.47. Use --chdir or --filter instead.`,
},
{
name: "not Terraform configuration",
command: "./tflint README.md",
dir: "multiple_files",
status: cmd.ExitCodeError,
stderr: "Failed to load `README.md`: File is not a target of Terraform",
stderr: `Command line arguments support was dropped in v0.47. Use --chdir or --filter instead.`,
},
{
name: "multiple files",
command: "./tflint empty.tf main.tf",
dir: "multiple_files",
status: cmd.ExitCodeIssuesFound,
// main.tf is not ignored
stdout: fmt.Sprintf("%s (aws_instance_example_type)", color.New(color.Bold).Sprint("instance type is t2.micro")),
stderr: `WARNING: "tflint FILE/DIR" is deprecated and will error in a future version. Use --chdir or --filter instead.`,
status: cmd.ExitCodeError,
stderr: `Command line arguments support was dropped in v0.47. Use --chdir or --filter instead.`,
},
{
name: "directory argument",
command: "./tflint subdir",
dir: "multiple_files",
status: cmd.ExitCodeIssuesFound,
stdout: fmt.Sprintf("%s (aws_instance_example_type)", color.New(color.Bold).Sprint("instance type is m5.2xlarge")),
stderr: `WARNING: "tflint FILE/DIR" is deprecated and will error in a future version. Use --chdir or --filter instead.`,
status: cmd.ExitCodeError,
stderr: `Command line arguments support was dropped in v0.47. Use --chdir or --filter instead.`,
},
{
name: "file under the directory",
command: fmt.Sprintf("./tflint %s", filepath.Join("subdir", "main.tf")),
dir: "multiple_files",
status: cmd.ExitCodeIssuesFound,
stdout: fmt.Sprintf("%s (aws_instance_example_type)", color.New(color.Bold).Sprint("instance type is m5.2xlarge")),
stderr: `WARNING: "tflint FILE/DIR" is deprecated and will error in a future version. Use --chdir or --filter instead.`,
status: cmd.ExitCodeError,
stderr: `Command line arguments support was dropped in v0.47. Use --chdir or --filter instead.`,
},
{
name: "multiple directories",
command: "./tflint subdir ./",
dir: "multiple_files",
status: cmd.ExitCodeError,
stderr: "Failed to load `subdir`: Multiple arguments are not allowed when passing a directory",
stderr: `Command line arguments support was dropped in v0.47. Use --chdir or --filter instead.`,
},
{
name: "file and directory",
command: "./tflint main.tf subdir",
dir: "multiple_files",
status: cmd.ExitCodeError,
stderr: "Failed to load `subdir`: Multiple arguments are not allowed when passing a directory",
stderr: `Command line arguments support was dropped in v0.47. Use --chdir or --filter instead.`,
},
{
name: "multiple files in different directories",
command: fmt.Sprintf("./tflint main.tf %s", filepath.Join("subdir", "main.tf")),
dir: "multiple_files",
status: cmd.ExitCodeError,
stderr: fmt.Sprintf("Failed to load `%s`: Multiple files in different directories are not allowed", filepath.Join("subdir", "main.tf")),
stderr: `Command line arguments support was dropped in v0.47. Use --chdir or --filter instead.`,
},
{
name: "--filter",
Expand Down Expand Up @@ -346,30 +341,29 @@ func TestIntegration(t *testing.T) {
name: "--chdir and file argument",
command: "./tflint --chdir=subdir main.tf",
dir: "chdir",
status: cmd.ExitCodeIssuesFound,
stdout: fmt.Sprintf("%s (aws_instance_example_type)", color.New(color.Bold).Sprint("instance type is m5.2xlarge")),
stderr: `WARNING: "tflint FILE/DIR" is deprecated and will error in a future version. Use --chdir or --filter instead.`,
status: cmd.ExitCodeError,
stderr: `Command line arguments support was dropped in v0.47. Use --chdir or --filter instead.`,
},
{
name: "--chdir and directory argument",
command: "./tflint --chdir=subdir ../",
dir: "chdir",
status: cmd.ExitCodeError,
stderr: "Cannot use --chdir and directory argument at the same time",
stderr: `Command line arguments support was dropped in v0.47. Use --chdir or --filter instead.`,
},
{
name: "--chdir and the current directory argument",
command: "./tflint --chdir=subdir .",
dir: "chdir",
status: cmd.ExitCodeError,
stderr: "Cannot use --chdir and directory argument at the same time",
stderr: `Command line arguments support was dropped in v0.47. Use --chdir or --filter instead.`,
},
{
name: "--chdir and file under the directory argument",
command: fmt.Sprintf("./tflint --chdir=subdir %s", filepath.Join("nested", "main.tf")),
dir: "chdir",
status: cmd.ExitCodeError,
stderr: "Cannot use --chdir and directory argument at the same time",
stderr: `Command line arguments support was dropped in v0.47. Use --chdir or --filter instead.`,
},
{
name: "--chdir and --filter",
Expand All @@ -383,21 +377,21 @@ func TestIntegration(t *testing.T) {
command: "./tflint --recursive main.tf",
dir: "chdir",
status: cmd.ExitCodeError,
stderr: "Cannot use --recursive and arguments at the same time",
stderr: `Command line arguments support was dropped in v0.47. Use --chdir or --filter instead.`,
},
{
name: "--recursive and directory argument",
command: "./tflint --recursive subdir",
dir: "chdir",
status: cmd.ExitCodeError,
stderr: "Cannot use --recursive and arguments at the same time",
stderr: `Command line arguments support was dropped in v0.47. Use --chdir or --filter instead.`,
},
{
name: "--recursive and the current directory argument",
command: "./tflint --recursive .",
dir: "chdir",
status: cmd.ExitCodeError,
stderr: "Cannot use --recursive and arguments at the same time",
stderr: `Command line arguments support was dropped in v0.47. Use --chdir or --filter instead.`,
},
{
name: "--recursive and --filter",
Expand Down

This file was deleted.

This file was deleted.

19 changes: 0 additions & 19 deletions integrationtest/inspection/arguments-with-values-file/dir/main.tf

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

25 changes: 0 additions & 25 deletions integrationtest/inspection/arguments-with-values-file/result.json

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions integrationtest/inspection/arguments/.tflint.hcl

This file was deleted.

This file was deleted.

3 changes: 0 additions & 3 deletions integrationtest/inspection/arguments/dir/example.tf

This file was deleted.

3 changes: 0 additions & 3 deletions integrationtest/inspection/arguments/dir/module/template.tf

This file was deleted.

8 changes: 0 additions & 8 deletions integrationtest/inspection/arguments/dir/template.tf

This file was deleted.

Loading