Skip to content

Commit d34b229

Browse files
authored
fix(HCL): back out from formatting all HCL files (#143)
* fix(HCL): back out from formatting all HCL files The tool we are using (terraform fmt) doesn't support all HCL file extensions. We should probably switch tools. I don't have time today so reducing scope from an 'HCL' language to a (locally-invented) 'Terraform' language with fewer file types Fixes #141 * chore: fix golden test
1 parent ee4a30d commit d34b229

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

format/private/format.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ function ls-files {
3232
'C++') patterns=('*.cpp' '*.c++' '*.cc' '*.cp' '*.cppm' '*.cxx' '*.h' '*.h++' '*.hh' '*.hpp' '*.hxx' '*.inc' '*.inl' '*.ino' '*.ipp' '*.ixx' '*.re' '*.tcc' '*.tpp' '*.txx') ;;
3333
'CSS') patterns=('*.css') ;;
3434
'Go') patterns=('*.go') ;;
35-
'HCL') patterns=('*.hcl' '*.nomad' '*.tf' '*.tfvars' '*.workflow') ;;
3635
'HTML') patterns=('*.html' '*.hta' '*.htm' '*.html.hl' '*.inc' '*.xht' '*.xhtml') ;;
3736
'JSON') patterns=('.all-contributorsrc' '.arcconfig' '.auto-changelog' '.c8rc' '.htmlhintrc' '.imgbotconfig' '.nycrc' '.tern-config' '.tern-project' '.watchmanconfig' 'Pipfile.lock' 'composer.lock' 'deno.lock' 'flake.lock' 'mcmod.info' '*.json' '*.4DForm' '*.4DProject' '*.avsc' '*.geojson' '*.gltf' '*.har' '*.ice' '*.JSON-tmLanguage' '*.jsonl' '*.mcmeta' '*.tfstate' '*.tfstate.backup' '*.topojson' '*.webapp' '*.webmanifest' '*.yy' '*.yyp') ;;
3837
'Java') patterns=('*.java' '*.jav' '*.jsh') ;;
@@ -49,6 +48,14 @@ function ls-files {
4948
'Swift') patterns=('*.swift') ;;
5049
'TSX') patterns=('*.tsx') ;;
5150
'TypeScript') patterns=('*.ts' '*.cts' '*.mts') ;;
51+
52+
# Note: terraform fmt cannot handle all HCL files such as .terraform.lock
53+
# "Only .tf and .tfvars files can be processed with terraform fmt"
54+
# so we define a custom language here instead of 'HCL' from github-linguist definition for the language.
55+
# TODO: we should probably use https://terragrunt.gruntwork.io/docs/reference/cli-options/#hclfmt instead
56+
# which does support the entire HCL language FWICT
57+
'Terraform') patterns=('*.tf' '*.tfvars') ;;
58+
5259
*)
5360
echo >&2 "Internal error: unknown language $language"
5461
exit 1
@@ -196,10 +203,10 @@ if [ -n "$files" ] && [ -n "$bin" ]; then
196203
echo "$files" | tr \\n \\0 | xargs -0 $bin $ruffmode
197204
fi
198205

199-
files=$(ls-files HCL $@)
206+
files=$(ls-files Terraform $@)
200207
bin=$(rlocation {{terraform-fmt}})
201208
if [ -n "$files" ] && [ -n "$bin" ]; then
202-
echo "Formatting Hashicorp Config Language with terraform fmt..."
209+
echo "Formatting Terraform files with terraform fmt..."
203210
echo "$files" | tr \\n \\0 | xargs -0 $bin fmt $tfmode
204211
fi
205212

format/test/format_test.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ bats_load_library "bats-assert"
6969
run bazel run //format/test:format_hcl
7070
assert_success
7171

72-
assert_output --partial "Formatting Hashicorp Config Language with terraform fmt..."
72+
assert_output --partial "Formatting Terraform files with terraform fmt..."
7373
assert_output --partial "+ terraform-fmt fmt example/src/hello.tf"
7474
}
7575

0 commit comments

Comments
 (0)