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

feat: added ignore dir #1787

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Application Options:
--color Enable colorized output
--no-color Disable colorized output
--fix Fix issues automatically
--ignore-dir Ignore dir sources

Help Options:
-h, --help Show this help message
Expand Down
18 changes: 17 additions & 1 deletion cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/terraform-linters/tflint/formatter"
"github.com/terraform-linters/tflint/terraform"
"github.com/terraform-linters/tflint/tflint"
"golang.org/x/exp/slices"
)

// Exit codes are int values that represent an exit code for a particular error.
Expand Down Expand Up @@ -72,6 +73,15 @@ func (cli *CLI) Run(args []string) int {
color.NoColor = true
cli.formatter.NoColor = true
}
if len(opts.IgnoreDir) > 0 {
for idx, value := range opts.IgnoreDir {
if filepath.Separator == '/' {
opts.IgnoreDir[idx] = filepath.ToSlash(value)
} else {
opts.IgnoreDir[idx] = filepath.FromSlash(value)
}
}
}
level := os.Getenv("TFLINT_LOG")
log.SetOutput(&logutils.LevelFilter{
Levels: []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"},
Expand Down Expand Up @@ -144,13 +154,19 @@ func findWorkingDirs(opts Options) ([]string, error) {

if opts.Recursive {
// NOTE: The target directory is always the current directory in recursive mode
err := filepath.WalkDir(".", func(path string, d os.DirEntry, err error) error {
var root, _ = filepath.Abs(".")
err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
return nil
}
// ignored directories are skipped
var relDirPath, _ = filepath.Rel(root, path)
if d.IsDir() && slices.Contains(opts.IgnoreDir, relDirPath) {
return nil
}
// hidden directories are skipped
if path != "." && strings.HasPrefix(d.Name(), ".") {
return filepath.SkipDir
Expand Down
2 changes: 2 additions & 0 deletions cmd/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Options struct {
NoColor bool `long:"no-color" description:"Disable colorized output"`
Fix bool `long:"fix" description:"Fix issues automatically"`
ActAsBundledPlugin bool `long:"act-as-bundled-plugin" hidden:"true"`
IgnoreDir []string `long:"ignore-dir" description:"Disable dir sources" value-name:"DIR_PATH"`
}

func (opts *Options) toConfig() *tflint.Config {
Expand Down Expand Up @@ -131,5 +132,6 @@ func (opts *Options) toConfig() *tflint.Config {
IgnoreModules: ignoreModules,
Rules: rules,
Plugins: plugins,
IgnoreDir: opts.IgnoreDir,
}
}
30 changes: 30 additions & 0 deletions cmd/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,36 @@ func Test_toConfig(t *testing.T) {
Plugins: map[string]*tflint.PluginConfig{},
},
},
{
Name: "--ignore-dir",
Command: "./tflint --ignore-dir=modules/foo",
Expected: &tflint.Config{
Module: false,
Force: false,
IgnoreModules: map[string]bool{},
Varfiles: []string{},
Variables: []string{},
DisabledByDefault: false,
Rules: map[string]*tflint.RuleConfig{},
Plugins: map[string]*tflint.PluginConfig{},
IgnoreDir: []string{"modules/foo"},
},
},
{
Name: "--ignore-dir",
Command: "./tflint --ignore-dir=modules/foo --ignore-dir=modules/bar",
Expected: &tflint.Config{
Module: false,
Force: false,
IgnoreModules: map[string]bool{},
Varfiles: []string{},
Variables: []string{},
DisabledByDefault: false,
Rules: map[string]*tflint.RuleConfig{},
Plugins: map[string]*tflint.PluginConfig{},
IgnoreDir: []string{"modules/foo", "modules/bar"},
},
},
}

for _, tc := range cases {
Expand Down
13 changes: 11 additions & 2 deletions tflint/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"strings"

hcl "github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/hashicorp/hcl/v2/hclsyntax"
Expand Down Expand Up @@ -44,6 +44,7 @@ var innerConfigSchema = &hcl.BodySchema{
{Name: "disabled_by_default"},
{Name: "plugin_dir"},
{Name: "format"},
{Name: "ignore_dir"},
},
}

Expand Down Expand Up @@ -80,6 +81,8 @@ type Config struct {
Rules map[string]*RuleConfig
Plugins map[string]*PluginConfig

IgnoreDir []string

sources map[string][]byte
}

Expand Down Expand Up @@ -242,6 +245,10 @@ func loadConfig(file afero.File) (*Config, error) {
if !formatValid {
return config, fmt.Errorf("%s is invalid format. Allowed formats are: %s", config.Format, strings.Join(validFormats, ", "))
}
case "ignore_dir":
if err := gohcl.DecodeExpression(attr.Expr, nil, &config.IgnoreDir); err != nil {
return config, err
}
default:
panic("never happened")
}
Expand Down Expand Up @@ -292,7 +299,7 @@ func loadConfig(file afero.File) (*Config, error) {
for name, plugin := range config.Plugins {
log.Printf("[DEBUG] %s: enabled=%t, version=%s, source=%s", name, plugin.Enabled, plugin.Version, plugin.Source)
}

log.Printf("[DEBUG] Variables: %s", strings.Join(config.IgnoreDir, ", "))
return config, nil
}

Expand Down Expand Up @@ -398,6 +405,8 @@ func (c *Config) Merge(other *Config) {
c.Plugins[name] = plugin
}
}

c.IgnoreDir = append(c.IgnoreDir, other.IgnoreDir...)
}

// ToPluginConfig converts self into the plugin configuration format
Expand Down
9 changes: 8 additions & 1 deletion tflint/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ func TestMerge(t *testing.T) {
Body: file2.Body,
},
},
Plugins: map[string]*PluginConfig{},
Plugins: map[string]*PluginConfig{},
IgnoreDir: []string{"modules/foo", "modules/bar"},
}

tests := []struct {
Expand Down Expand Up @@ -452,6 +453,7 @@ func TestMerge(t *testing.T) {
Enabled: false,
},
},
IgnoreDir: []string{"modules/foo", "modules/bar"},
},
other: &Config{
Module: false,
Expand Down Expand Up @@ -491,6 +493,7 @@ func TestMerge(t *testing.T) {
Enabled: true,
},
},
IgnoreDir: []string{"modules/baz"},
},
want: &Config{
Module: true,
Expand Down Expand Up @@ -541,6 +544,7 @@ func TestMerge(t *testing.T) {
Enabled: true,
},
},
IgnoreDir: []string{"modules/foo", "modules/bar", "modules/baz"},
},
},
{
Expand Down Expand Up @@ -578,6 +582,7 @@ func TestMerge(t *testing.T) {
Enabled: false,
},
},
IgnoreDir: []string{"modules/foo", "modules/bar"},
},
other: &Config{
Module: false,
Expand Down Expand Up @@ -614,6 +619,7 @@ func TestMerge(t *testing.T) {
Enabled: true,
},
},
IgnoreDir: []string{"modules/baz"},
},
want: &Config{
Module: true,
Expand Down Expand Up @@ -661,6 +667,7 @@ func TestMerge(t *testing.T) {
Enabled: true,
},
},
IgnoreDir: []string{"modules/foo", "modules/bar", "modules/baz"},
},
},
{
Expand Down