diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index ad5da271bdab7993d92de180b888a247b8020183..d8e8866380809a61cd122bb06ccc8de7400f2e2f 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -42,7 +42,7 @@ description: Fix known backwards incompatibilities in Packer templates language: python entry: packer-fix - types_or: [json, hcl] + types_or: [json] - id: packer-fmt name: Format Packer templates diff --git a/README.md b/README.md index 143df4f5e8bbb29c123ce25cd0830480c2982c43..78ef6c1f6147e896d710deae2d8475397f56dbb8 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,8 @@ Requires an installed `terraform`. ### `packer-fix` -Fix known backwards incompatibilities in Packer templates. +Fix known backwards incompatibilities in Packer templates (just JSON files, +until support for HCL files is added in Packer). ### `packer-fmt` diff --git a/hooks/packer_validate.py b/hooks/packer_validate.py index 7df19eb0fc6950ca736d0d0bb1a7f1feeba18db2..4db70d953db6e89b3fb1d8e737a8dc4e4bb8cdb2 100644 --- a/hooks/packer_validate.py +++ b/hooks/packer_validate.py @@ -6,6 +6,17 @@ import sys import hooks.utils +def packer_validate(file): + """Validate a Packer template. + + Run init when needed. + """ + if str(file).endswith(".pkr.hcl") or str(file).endswith(".pkr.json"): + if hooks.utils.check_file(["packer", "init", file]) > 0: + return 1 + return hooks.utils.check_file(["packer", "validate", file]) + + def main(): """Main entrypoint.""" parser = argparse.ArgumentParser(description=__doc__) @@ -13,7 +24,7 @@ def main(): args = parser.parse_args() hooks.utils.check_executable("packer") return hooks.utils.bulk_check( - lambda x: hooks.utils.check_file(["packer", "validate", x]), + packer_validate, args.file, )