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

improve formatting of generated tf code #474

Merged
merged 12 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions .reviewdog.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
runner:
golangci:
cmd: ./bin/golangci-lint run --out-format=line-number
cmd: ./bin/golangci-lint run --out-format=line-number -v --timeout 5m
errorformat:
- '%E%f:%l:%c: %m'
- '%E%f:%l: %m'
- '%C%.%#'
level: warning
level: warning
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ install:
- mkdir bin # this is in theory supposed to exist already
- curl -L -o $HOME/bin/buildevents https://github.com/honeycombio/buildevents/releases/latest/download/buildevents-linux-amd64
- chmod 755 $HOME/bin/buildevents
- buildevents cmd $TRAVIS_BUILD_ID $STEP_SPAN_ID npm-install -- npm install -g snyk
# - buildevents cmd $TRAVIS_BUILD_ID $STEP_SPAN_ID npm-install -- npm install -g snyk
- buildevents cmd $TRAVIS_BUILD_ID $STEP_SPAN_ID make-setup -- make setup
- buildevents step $TRAVIS_BUILD_ID $STEP_SPAN_ID $STEP_START install
- curl -sfL https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh| sh -s -- -b ~/bin
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ lint: ## run the fast go linters
.PHONY: lint

lint-ci: ## run the fast go linters
./bin/reviewdog -conf .reviewdog.yml -reporter=github-pr-review -tee
./bin/reviewdog -conf .reviewdog.yml -reporter=github-pr-review -tee -level=info
.PHONY: lint-ci

lint-all: ## run the fast go linters
Expand Down
31 changes: 22 additions & 9 deletions apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ func applyTree(dest afero.Fs, source *packr.Box, common *packr.Box, targetBasePa
if e != nil {
return errs.WrapUser(e, "unable to apply template")
}

if targetExtension == ".tf" {
e = fmtHcl(dest, target, true)
if e != nil {
return errs.WrapUser(e, "unable to format HCL")
}
}

} else if extension == ".touch" {
e = touchFile(dest, target)
if e != nil {
Expand Down Expand Up @@ -251,21 +259,26 @@ func applyTree(dest afero.Fs, source *packr.Box, common *packr.Box, targetBasePa
logrus.Infof("%s copied", target)
}

if targetExtension == ".tf" {
e = fmtHcl(dest, target)
if e != nil {
return errs.WrapUser(e, "unable to format HCL")
}
}
return nil
})
}

func fmtHcl(fs afero.Fs, path string) error {
// collapseLines will convert \n+ to \n to reduce spurious diffs in generated output
// post 0.12 terraform fmt will not manage vertical whitespace
// https://github.com/hashicorp/terraform/issues/23223#issuecomment-547519852
func collapseLines(in []byte) []byte {
fmtRegex := regexp.MustCompile(`\n+`)
return fmtRegex.ReplaceAll(in, []byte("\n"))
}

func fmtHcl(fs afero.Fs, path string, collapse bool) error {
in, e := afero.ReadFile(fs, path)
if e != nil {
return errs.WrapUserf(e, "unable to read file %s", path)
}
if collapse {
in = collapseLines(in)
}
out := hclwrite.Format(in)
return afero.WriteReader(fs, path, bytes.NewReader(out))
}
Expand Down Expand Up @@ -377,7 +390,7 @@ func applyModuleInvocation(fs afero.Fs, path, moduleAddress string, box packr.Bo
if e != nil {
return errs.WrapUser(e, "unable to apply template for main.tf")
}
e = fmtHcl(fs, filepath.Join(path, "main.tf"))
e = fmtHcl(fs, filepath.Join(path, "main.tf"), false)
if e != nil {
return errs.WrapUser(e, "unable to format main.tf")
}
Expand All @@ -393,7 +406,7 @@ func applyModuleInvocation(fs afero.Fs, path, moduleAddress string, box packr.Bo
return errs.WrapUser(e, "unable to apply template for outputs.tf")
}

e = fmtHcl(fs, filepath.Join(path, "outputs.tf"))
e = fmtHcl(fs, filepath.Join(path, "outputs.tf"), false)
if e != nil {
return errs.WrapUser(e, "unable to format outputs.tf")
}
Expand Down
2 changes: 1 addition & 1 deletion apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func TestFmtHcl(t *testing.T) {
in := strings.NewReader(before)
e := afero.WriteReader(fs, "foo.tf", in)
r.Nil(e)
e = fmtHcl(fs, "foo.tf")
e = fmtHcl(fs, "foo.tf", false)
r.Nil(e)
out, e := afero.ReadFile(fs, "foo.tf")
r.Nil(e)
Expand Down
1 change: 0 additions & 1 deletion apply/golden_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func TestIntegration(t *testing.T) {
{"v2_no_aws_provider_yaml"},
{"github_actions"},
{"circleci"},
{"component_kind"},
{"tfe_provider_yaml"},
}

Expand Down
50 changes: 0 additions & 50 deletions testdata/bless_provider_yaml/terraform/accounts/foo/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 0 additions & 58 deletions testdata/bless_provider_yaml/terraform/envs/bar/bam/fogg.tf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading