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

Feat: add Support of Terratag parameters through Environment Variable #176

Merged
merged 2 commits into from
May 9, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ locals {
- `-filter=<regular expression>` - defaults to `.*`. Only apply tags to the resource types matched by the regular expression
- `-type=<terraform or terragrunt>` - defaults to `terraform`. If `terragrunt` is used, tags the files under `.terragrunt-cache` folder. Note: if Terragrunt does not create a `.terragrunt-cache` folder, use the default or omit.

Setting options via enviroment variables is also supported. CLI flags have a precedence over envrionment variable.

```
TERRATAG_TAGS
TERRATAG_DIR
TERRATAG_SKIPTERRATAGFILES
TERRATAG_FILTER
TERRATAG_SKIP
TERRATAG_VERBOSE
TERRATAG_RENAME
TERRATAG_TYPE
```

##### See more samples [here](https://github.com/env0/terratag/tree/master/test/fixture)

## Notes
Expand Down
16 changes: 16 additions & 0 deletions cli/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"os"
"strings"

"github.com/env0/terratag/internal/common"
)
Expand Down Expand Up @@ -48,6 +49,21 @@ func InitArgs() (Args, error) {
fs.StringVar(&args.Type, "type", string(common.Terraform), "The IAC type. Valid values: terraform or terragrunt")
fs.BoolVar(&args.Version, "version", false, "Prints the version")

// Set cli args based on environment variables.
//The command line flags have precedence over environment variables.
fs.VisitAll(func(f *flag.Flag) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is really all the code...
The rest is testing etc...

if f.Name == "version" {
return
}

name := "TERRATAG_" + strings.ToUpper(strings.ReplaceAll(f.Name, "-", "_"))
if value, ok := os.LookupEnv(name); ok {
if err := fs.Set(f.Name, value); err != nil {
fmt.Printf("[WARN] failed to set command arg flag '%s' from environment variable '%s': %v\n", f.Name, name, err)
}
}
})

if err := fs.Parse(programArgs); err != nil {
return args, err
}
Expand Down
48 changes: 48 additions & 0 deletions terratag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import (

"github.com/bmatcuk/doublestar"
"github.com/env0/terratag/cli"
"github.com/env0/terratag/internal/common"
. "github.com/onsi/gomega"
"github.com/otiai10/copy"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var cleanArgs = append([]string(nil), os.Args...)
Expand Down Expand Up @@ -397,3 +399,49 @@ func TestToHclMap(t *testing.T) {
})
}
}

func TestEnvVariables(t *testing.T) {
os.Setenv("TERRATAG_TAGS", `{"a":"b"}`)
os.Setenv("TERRATAG_DIR", "./dir")
os.Setenv("TERRATAG_SKIPTERRATAGFILES", "true")
os.Setenv("TERRATAG_FILTER", "filter")
os.Setenv("TERRATAG_SKIP", "skip")
os.Setenv("TERRATAG_VERBOSE", "true")
os.Setenv("TERRATAG_RENAME", "false")
os.Setenv("TERRATAG_TYPE", string(common.Terragrunt))

defer func() {
os.Unsetenv("TERRATAG_TAGS")
os.Unsetenv("TERRATAG_DIR")
os.Unsetenv("TERRATAG_SKIPTERRATAGFILES")
os.Unsetenv("TERRATAG_FILTER")
os.Unsetenv("TERRATAG_SKIP")
os.Unsetenv("TERRATAG_VERBOSE")
os.Unsetenv("TERRATAG_RENAME")
os.Unsetenv("TERRATAG_TYPE")
}()

osArgsLock.Lock()
defer osArgsLock.Unlock()

os.Args = []string{programName}
args, err := cli.InitArgs()
os.Args = cleanArgs
require.Nil(t, err)

assert.Equal(t, `{"a":"b"}`, args.Tags)
assert.Equal(t, "./dir", args.Dir)
assert.Equal(t, true, args.IsSkipTerratagFiles)
assert.Equal(t, "filter", args.Filter)
assert.Equal(t, "skip", args.Skip)
assert.Equal(t, true, args.Verbose)
assert.Equal(t, false, args.Rename)
assert.Equal(t, string(common.Terragrunt), args.Type)

// The command line flags have precedence over environment variables.
os.Args = []string{programName, `-tags={"c":"d"}`}
args, err = cli.InitArgs()
os.Args = cleanArgs
require.Nil(t, err)
assert.Equal(t, `{"c":"d"}`, args.Tags)
}