tflint
tflint copied to clipboard
Introduce a concept of subcommands in CLI
Introduction
Currently, TFLint has 4 modes of operation.
tflint --init- Installing plugins
tflint- Inspecting a Terraform configuration
tflint --langserver- Starting a language server
tflint --version- Printing version
In v0.44, we plan to introduce a --chdir option to move the working directory, similar to Terraform. See also https://github.com/terraform-linters/tflint/pull/1612.
In Terraform, the --chdir has a special meaning to other flags. Below is a quote from the PR where --chdir was introduced:
The new option is only accepted at the start of the command line (before the subcommand) as a way to reflect that it is a global command (not specific to a particular subcommand) and that it takes effect before executing the subcommand. This also means it'll be forced to appear before any other command-specific arguments that take file paths, which hopefully communicates that those other arguments are interpreted relative to the overridden path.
https://github.com/hashicorp/terraform/pull/26087
If we adopt a concept similar to this in TFLint, we cannot force the user to assume that the path will be relative to the changed directory, since TFLint has no concept of subcommands.
Proposal
Introduce a subcommand as below and allow --chdir only before the subcommand:
tflint --chdir=dir inittflint --chdir=dir inspecttflint --chdir=dir langservertflint --chdir=dir version
For my personal taste, it feels a bit redundant to always require a subcommand for inspection. To improve the user's first experience, I would like the inspection to work with just running tflint without subcommands.
In terms of compatibility with Terraform, https://github.com/mitchellh/cli would be a good option. Another popular one would be https://github.com/spf13/cobra. What to use should be decided after more detailed research.
References
- https://github.com/terraform-linters/tflint/pull/1612
- https://github.com/hashicorp/terraform/pull/26087
- https://github.com/mitchellh/cli
- https://github.com/spf13/cobra
To implement the behavior where -chdir is handled differently than sub-command flags, Terraform manually processes (and strips) it before passing off the args to be parsed as flags:
https://github.com/hashicorp/terraform/blob/a9230c9e7582c353c224cf0f4832d472ce042c0d/main.go#L428
Since there are no sub-commands now, users may just stick chdir on the end:
tflint --init --chdir foo
So it will be somewhat disruptive but probably the right move to require this:
tflint --chdir=foo init
Terraform also requires an equals in -chdir to disambiguate the value from a subcommand. Otherwise you can pass something like this:
tflint -chdir init
This should be rejected as invalid (because chdir requires an argument) rather than treated as tflint -chdir=init inspect.