Skip to content

Commit

Permalink
add --terragrunt-hclfmt-exclude-dir parameter hclfmt command. fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
wakeful authored Nov 29, 2024
1 parent d8313f9 commit 13801e0
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 13 deletions.
12 changes: 10 additions & 2 deletions cli/commands/hclfmt/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,22 @@ func Run(opts *options.TerragruntOptions) error {
for _, fname := range tgHclFiles {
skipFile := false
// Ignore any files that are in the cache or scaffold dir
if util.ListContainsElement(strings.Split(fname, "/"), util.TerragruntCacheDir) {
pathList := strings.Split(fname, "/")
if util.ListContainsElement(pathList, util.TerragruntCacheDir) {
skipFile = true
}

if util.ListContainsElement(strings.Split(fname, "/"), util.DefaultBoilerplateDir) {
if util.ListContainsElement(pathList, util.DefaultBoilerplateDir) {
skipFile = true
}

for _, excludeDir := range opts.HclExclude {
if util.ListContainsElement(pathList, excludeDir) {
skipFile = true
break
}
}

if skipFile {
opts.Logger.Debugf("%s was ignored", fname)
} else {
Expand Down
30 changes: 23 additions & 7 deletions cli/commands/hclfmt/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@ import (
func TestHCLFmt(t *testing.T) {
t.Parallel()

tmpPath, err := files.CopyFolderToTemp("../../../test/fixtures/hclfmt", t.Name(), func(path string) bool { return true })
tmpPath, err := files.CopyFolderToTemp("./testdata/fixtures", t.Name(), func(path string) bool { return true })

t.Cleanup(func() {
os.RemoveAll(tmpPath)
})

require.NoError(t, err)

expected, err := util.ReadFileAsString("../../../test/fixtures/hclfmt/expected.hcl")
expected, err := util.ReadFileAsString("./testdata/fixtures/expected.hcl")
require.NoError(t, err)

tgOptions, err := options.NewTerragruntOptionsForTest("")
require.NoError(t, err)

tgOptions.WorkingDir = tmpPath
tgOptions.HclExclude = []string{".history"}

err = hclfmt.Run(tgOptions)
require.NoError(t, err)
Expand Down Expand Up @@ -60,11 +61,11 @@ func TestHCLFmt(t *testing.T) {
})
}

// Finally, check to make sure the file in the `.terragrunt-cache` folder was ignored and untouched
// check to make sure the file in the `.terragrunt-cache` folder was ignored and untouched
t.Run("terragrunt-cache", func(t *testing.T) {
t.Parallel()

originalTgHclPath := "../../../test/fixtures/hclfmt/ignored/.terragrunt-cache/terragrunt.hcl"
originalTgHclPath := "./testdata/fixtures/ignored/.terragrunt-cache/terragrunt.hcl"
original, err := util.ReadFileAsString(originalTgHclPath)
require.NoError(t, err)

Expand All @@ -74,6 +75,21 @@ func TestHCLFmt(t *testing.T) {

assert.Equal(t, original, actual)
})

// Finally, check to make sure the file in the `.history` folder was ignored and untouched
t.Run("history", func(t *testing.T) {
t.Parallel()

originalTgHclPath := "./testdata/fixtures/ignored/.history/terragrunt.hcl"
original, err := util.ReadFileAsString(originalTgHclPath)
require.NoError(t, err)

tgHclPath := filepath.Join(tmpPath, "ignored/.history/terragrunt.hcl")
actual, err := util.ReadFileAsString(tgHclPath)
require.NoError(t, err)

assert.Equal(t, original, actual)
})
})

}
Expand Down Expand Up @@ -209,15 +225,15 @@ func TestHCLFmtCheckErrors(t *testing.T) {
func TestHCLFmtFile(t *testing.T) {
t.Parallel()

tmpPath, err := files.CopyFolderToTemp("../../../test/fixtures/hclfmt", t.Name(), func(path string) bool { return true })
tmpPath, err := files.CopyFolderToTemp("./testdata/fixtures", t.Name(), func(path string) bool { return true })

t.Cleanup(func() {
os.RemoveAll(tmpPath)
})

require.NoError(t, err)

expected, err := os.ReadFile("../../../test/fixtures/hclfmt/expected.hcl")
expected, err := os.ReadFile("./testdata/fixtures/expected.hcl")
require.NoError(t, err)

tgOptions, err := options.NewTerragruntOptionsForTest("")
Expand Down Expand Up @@ -245,7 +261,7 @@ func TestHCLFmtFile(t *testing.T) {
"a/b/c/terragrunt.hcl",
}

original, err := os.ReadFile("../../../test/fixtures/hclfmt/terragrunt.hcl")
original, err := os.ReadFile("./testdata/fixtures/terragrunt.hcl")
require.NoError(t, err)

// test that none of the other files were formatted
Expand Down
15 changes: 11 additions & 4 deletions cli/commands/hclfmt/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
const (
CommandName = "hclfmt"

FlagNameTerragruntHCLFmt = "terragrunt-hclfmt-file"
FlagNameTerragruntCheck = "terragrunt-check"
FlagNameTerragruntDiff = "terragrunt-diff"
FlagNameTerragruntHCLFmtStdin = "terragrunt-hclfmt-stdin"
FlagNameTerragruntHCLFmt = "terragrunt-hclfmt-file"
FlagNameTerragruntHCLFmtExcludeDir = "terragrunt-hclfmt-exclude-dir"
FlagNameTerragruntCheck = "terragrunt-check"
FlagNameTerragruntDiff = "terragrunt-diff"
FlagNameTerragruntHCLFmtStdin = "terragrunt-hclfmt-stdin"
)

func NewFlags(opts *options.TerragruntOptions) cli.Flags {
Expand All @@ -22,6 +23,12 @@ func NewFlags(opts *options.TerragruntOptions) cli.Flags {
Destination: &opts.HclFile,
Usage: "The path to a single hcl file that the hclfmt command should run on.",
},
&cli.SliceFlag[string]{
Name: FlagNameTerragruntHCLFmtExcludeDir,
Destination: &opts.HclExclude,
EnvVar: "TERRAGRUNT_HCLFMT_EXCLUDE_DIR",
Usage: "Skip HCL formatting in given directories.",
},
&cli.BoolFlag{
Name: FlagNameTerragruntCheck,
Destination: &opts.Check,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions cli/commands/hclfmt/testdata/fixtures/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
inputs = {
# comments
foo = "bar"
bar="baz"

inputs = "disjoint"
disjoint = true

listInput = [
"foo",
"bar",
]
}
11 changes: 11 additions & 0 deletions docs/_docs/04_reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,17 @@ When passed in, running `hclfmt` will print diff between original and modified f

When passed in, run `hclfmt` only on the specified file.

### terragrunt-hclfmt-exclude-dir

**CLI Arg**: `--terragrunt-hclfmt-exclude-dir .dir-name`<br/>
**Environment Variable**: `TERRAGRUNT_HCLFMT_EXCLUDE_DIR`<br/>
**Commands**:

- [hclfmt](#hclfmt)

Can be supplied multiple times: `--terragrunt-hclfmt-exclude-dir .back --terragrunt-hclfmt-exclude-dir .archive`<br/>
When passed in, `hclfmt` will ignore files in the specified directories.

### terragrunt-hclfmt-stdin

**CLI Arg**: `--terragrunt-hclfmt-stdin`<br/>
Expand Down
4 changes: 4 additions & 0 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ type TerragruntOptions struct {
// The file which hclfmt should be specifically run on
HclFile string

// If set hclfmt will skip files in given directories.
HclExclude []string

// If True then HCL from StdIn must should be formatted.
HclFromStdin bool

Expand Down Expand Up @@ -602,6 +605,7 @@ func (opts *TerragruntOptions) Clone(terragruntConfigPath string) (*TerragruntOp
RunTerragrunt: opts.RunTerragrunt,
AwsProviderPatchOverrides: opts.AwsProviderPatchOverrides,
HclFile: opts.HclFile,
HclExclude: opts.HclExclude,
HclFromStdin: opts.HclFromStdin,
JSONOut: opts.JSONOut,
JSONLogFormat: opts.JSONLogFormat,
Expand Down

0 comments on commit 13801e0

Please sign in to comment.