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

Rewrite package terratag/cli #95

Merged
merged 3 commits into from
Oct 5, 2021
Merged

Rewrite package terratag/cli #95

merged 3 commits into from
Oct 5, 2021

Conversation

arybolovlev
Copy link

@arybolovlev arybolovlev commented Sep 20, 2021

Hi there,

With this PR I would love to propose a small improvement for the terratag/cli package.
Instead of writing your own functions to manage CLI arguments, you can use the package 'flag' from the standard Golang library.

One of the benefits of using the package 'flag' is that it is coming with the build-in "help", it is done transparently by the package. Here is how it looks:

# terratag -help
Usage of terratag:
  -dir string
    	Directory to recursively search for .tf files and terratag them (default ".")
  -filter string
    	Only apply tags to the selected resource types (regex) (default ".*")
  -rename
    	Keep the original filename or replace it with <basename>.terratag.tf (default true)
  -skipTerratagFiles
    	Skips any previously tagged files (default true)
  -tags string
    	Tags as a valid JSON document
  -verbose
    	Enable verbose logging

Another benefit is the build-in check for the parameters of the argument:

# terratag -verbose=hello                                                                                                                               6s
invalid boolean value "hello" for -verbose: parse error

I am not sure if the descriptions for the options are right, please feel free to update them.

Thank you.

Copy link
Contributor

@yaronya yaronya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arybolovlev great suggestion 💯
It looks like you got some tests failing there

@yaronya yaronya added enhancement New feature or request await author labels Sep 22, 2021
@arybolovlev
Copy link
Author

Hi @yaronya,

I've fixed this. Tests were failing due to the nature of the flag package. When you use a default FlagSet, as I did in my first PR, all flags are set on the global level and cannot be re-set. Since tests were running in parallel each test starting from the second one was trying to set flags again and because of that, it was failing.

I also removed the unused field SkipTerratagFiles from the Args structure. I couldn't find anywhere in the code if it is used. I guess it was replaced with the IsSkipTerratagFiles.

Also, as you can see I've updated the terratag_test.go file to cut off the extra arguments that are added by the package testing when executing tests. It doesn't influence tests. For instance, I ran tests with the flag -time.timeout=30s to intentionally fail them due to timeout and it was working well. So cutting off extra arguments seems to look fine.

Locally all tests are green now.

Thank you!

@arybolovlev arybolovlev marked this pull request as ready for review September 30, 2021 08:54
Copy link
Contributor

@yaronya yaronya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nicely done! Thank you for your contribution!
Left one last small comment 🙏

cli/cli.go Outdated
args.Filter = setFlag("filter", ".*")
args.Verbose = booleanFlag("verbose", false)
args.Rename = booleanFlag("rename", true)
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please extract os.Args[0] and os.Args[1:] into meaningful variables and use those instead?
i.e

programName := os.Args[0]
programArgs := os.Args[1:]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thank you! Updated also the test file just to have consistency.

@yaronya yaronya merged commit 968ecfc into env0:master Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants