Skip to content

Commit

Permalink
Implement Dry Run Feature (#257)
Browse files Browse the repository at this point in the history
* (feat) implement dry run feature

* test with no args provided
  • Loading branch information
hi-artem committed Dec 21, 2021
1 parent ac09880 commit a9cbb71
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
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
3 changes: 3 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 Down Expand Up @@ -89,6 +90,7 @@ func TestOutputCrasher(t *testing.T) {
Output: "junitxml",
OutputJSONFile: "",
TagGroups: []string{"git", "custom"},
DryRun: true,
}
options.Validate()
}
Expand All @@ -105,6 +107,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

0 comments on commit a9cbb71

Please sign in to comment.