Skip to content

Commit

Permalink
Non recursive tagging (#555)
Browse files Browse the repository at this point in the history
* fix tags with tag-prefix

* new UT

* support non recursive tagging

* fix tags with tag-prefix

* new UT

* support non recursive tagging

* fix runner

* fix UT

* fix UT
  • Loading branch information
MIRIAM-SLOV authored Aug 4, 2024
1 parent f86cd7e commit 5590238
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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()
Expand Down Expand Up @@ -240,6 +242,12 @@ func tagCommand() *cli.Command {
Value: false,
DefaultText: "false",
},
&cli.BoolFlag{
Name: nonRecursiveArgs,
Usage: "non recursive tagging",
Value: false,
DefaultText: "false",
},
},
}
}
Expand Down
1 change: 1 addition & 0 deletions src/common/clioptions/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type TagOptions struct {
TagPrefix string
NoColor bool
UseCodeOwners bool
NonRecursive bool
}

type ListTagsOptions struct {
Expand Down
5 changes: 5 additions & 0 deletions src/common/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Runner struct {
skippedResources []string
workersNum int
dryRun bool
nonRecursive bool
}

const WorkersNumEnvKey = "YOR_WORKER_NUM"
Expand Down Expand Up @@ -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))
}
Expand All @@ -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)
}
Expand Down
25 changes: 25 additions & 0 deletions src/common/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}

0 comments on commit 5590238

Please sign in to comment.