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

Implement Dry Run Feature #257

Merged
merged 2 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func tagCommand() *cli.Command {
externalConfPath := "config-file"
skipResourceTypesArg := "skip-resource-types"
parsersArgs := "parsers"
dryRunArgs := "dry-run"
return &cli.Command{
Name: "tag",
Usage: "apply tagging across your directory",
Expand All @@ -107,6 +108,7 @@ func tagCommand() *cli.Command {
ConfigFile: c.String(externalConfPath),
SkipResourceTypes: c.StringSlice(skipResourceTypesArg),
Parsers: c.StringSlice(parsersArgs),
DryRun: c.Bool(dryRunArgs),
}

options.Validate()
Expand Down Expand Up @@ -185,6 +187,12 @@ func tagCommand() *cli.Command {
Value: cli.NewStringSlice("Terraform", "CloudFormation", "Serverless"),
DefaultText: "Terraform,CloudFormation,Serverless",
},
&cli.BoolFlag{
Name: dryRunArgs,
Usage: "skip resource 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 @@ -26,6 +26,7 @@ type TagOptions struct {
ConfigFile string `validate:"config-file"`
SkipResourceTypes []string
Parsers []string
DryRun bool
}

type ListTagsOptions struct {
Expand Down
4 changes: 4 additions & 0 deletions src/common/clioptions/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestCliArgParsing(t *testing.T) {
Output: "cli",
OutputJSONFile: "",
ConfigFile: "",
DryRun: true,
}
// Expect the validation to pass without throwing errors
options.Validate()
Expand All @@ -44,6 +45,7 @@ func TestCliArgParsing(t *testing.T) {
Output: "cli",
OutputJSONFile: "",
TagGroups: []string{"git", "code2cloud"},
DryRun: true,
nimrodkor marked this conversation as resolved.
Show resolved Hide resolved
}
// Expect the validation to pass without throwing errors
options.Validate()
Expand Down Expand Up @@ -89,6 +91,7 @@ func TestOutputCrasher(t *testing.T) {
Output: "junitxml",
OutputJSONFile: "",
TagGroups: []string{"git", "custom"},
DryRun: true,
}
options.Validate()
}
Expand All @@ -105,6 +108,7 @@ func TestTagGroupCrasher(t *testing.T) {
Output: "cli",
OutputJSONFile: "",
TagGroups: []string{"git", "custom"},
DryRun: true,
}
options.Validate()
}
Expand Down
4 changes: 3 additions & 1 deletion src/common/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Runner struct {
configFilePath string
skippedResourceTypes []string
workersNum int
dryRun bool
}

const WorkersNumEnvKey = "YOR_WORKER_NUM"
Expand Down Expand Up @@ -90,6 +91,7 @@ func (r *Runner) Init(commands *clioptions.TagOptions) error {
r.skippedTags = commands.SkipTags
r.skipDirs = append(commands.SkipDirs, ".git")
r.configFilePath = commands.ConfigFile
r.dryRun = commands.DryRun
if utils.InSlice(r.skipDirs, r.dir) {
logger.Warning(fmt.Sprintf("Selected dir, %s, is skipped - expect an empty result", r.dir))
}
Expand Down Expand Up @@ -186,7 +188,7 @@ func (r *Runner) TagFile(file string) {
}
r.ChangeAccumulator.AccumulateChanges(block)
}
if isFileTaggable {
if isFileTaggable && !r.dryRun {
err = parser.WriteFile(file, blocks, file)
if err != nil {
logger.Warning(fmt.Sprintf("Failed writing tags to file %s, because %v", file, err))
Expand Down