diff --git a/README.md b/README.md index fb4f5d86..fb9aee97 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,9 @@ Pre-commit # Apply all the tags in yor on the directory tree terraform. yor tag --directory terraform/ +# Apply all the tags in yor on the directory terraform, without tagging subdirectories. +yor tag --directory terraform/ --non-recursive + # Apply only the specified tags git_file and git_org yor tag --directory terraform/ --tags git_file,git_org diff --git a/main.go b/main.go index 8e633bb3..6f91fa48 100644 --- a/main.go +++ b/main.go @@ -95,6 +95,7 @@ func tagCommand() *cli.Command { tagPrefix := "tag-prefix" noColor := "no-color" useCodeowners := "use-code-owners" + nonRecursiveArgs := "non-recursive" return &cli.Command{ Name: "tag", Usage: "apply tagging across your directory", @@ -120,6 +121,7 @@ func tagCommand() *cli.Command { TagPrefix: c.String(tagPrefix), NoColor: c.Bool(noColor), UseCodeOwners: c.Bool(useCodeowners), + NonRecursive: c.Bool(nonRecursiveArgs), } options.Validate() @@ -240,6 +242,12 @@ func tagCommand() *cli.Command { Value: false, DefaultText: "false", }, + &cli.BoolFlag{ + Name: nonRecursiveArgs, + Usage: "non recursive tagging", + Value: false, + DefaultText: "false", + }, }, } } diff --git a/src/common/clioptions/cli.go b/src/common/clioptions/cli.go index 2107fcf1..3d539b8d 100644 --- a/src/common/clioptions/cli.go +++ b/src/common/clioptions/cli.go @@ -33,6 +33,7 @@ type TagOptions struct { TagPrefix string NoColor bool UseCodeOwners bool + NonRecursive bool } type ListTagsOptions struct { diff --git a/src/common/runner/runner.go b/src/common/runner/runner.go index 5b008832..1a64626e 100644 --- a/src/common/runner/runner.go +++ b/src/common/runner/runner.go @@ -38,6 +38,7 @@ type Runner struct { skippedResources []string workersNum int dryRun bool + nonRecursive bool } const WorkersNumEnvKey = "YOR_WORKER_NUM" @@ -95,6 +96,7 @@ func (r *Runner) Init(commands *clioptions.TagOptions) error { r.skipDirs = commands.SkipDirs r.configFilePath = commands.ConfigFile r.dryRun = commands.DryRun + r.nonRecursive = commands.NonRecursive if utils.InSlice(r.skipDirs, r.dir) { logger.Warning(fmt.Sprintf("Selected dir, %s, is skipped - expect an empty result", r.dir)) } @@ -121,6 +123,9 @@ func (r *Runner) TagDirectory() (*reports.ReportService, error) { if err != nil { logger.Warning(fmt.Sprintf("Failed to scan dir %s", path)) } + if r.nonRecursive && info.IsDir() && path != r.dir { + return filepath.SkipDir + } if !info.IsDir() { files = append(files, path) } diff --git a/src/common/runner/runner_test.go b/src/common/runner/runner_test.go index 8818db82..f6f477a3 100644 --- a/src/common/runner/runner_test.go +++ b/src/common/runner/runner_test.go @@ -311,3 +311,28 @@ func Test_YorNameTag(t *testing.T) { assert.True(t, yorNameCounter > 0) }) } + +func TestNonRecursiveTagging(t *testing.T) { + t.Run("tag directory non recursive", func(t *testing.T) { + rootDir := "../../../tests/terraform/resources/taggedkms" + runner := new(Runner) + err := runner.Init(&clioptions.TagOptions{ + Directory: rootDir, + TagGroups: taggingUtils.GetAllTagGroupsNames(), + NonRecursive: true, + Parsers: []string{"Terraform"}, + }) + if err != nil { + t.Error(err) + } + reportService, err := runner.TagDirectory() + if err != nil { + t.Error(err) + } + reportService.CreateReport() + report := reportService.GetReport() + for _, newTag := range report.NewResourceTags { + assert.NotEqual(t, "../../../tests/terraform/resources/taggedkms/modified/modified_kms.tf", newTag.File) + } + }) +}