Skip to content

Commit feea5f1

Browse files
committed
Add check-lockfile-is-cross-platform mode, rename mode only-check-is-current-lockfile-cross-platform to regenerate-lockfile-if-some-platform-missed
1 parent bf77b74 commit feea5f1

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

README.md

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -759,26 +759,44 @@ To replicate functionality in `terraform_docs` hook:
759759
> - --hook-config=--mode=always-regenerate-lockfile
760760
> ```
761761
>
762-
> Why? When v2.x will be introduced - the default mode will be changed, probably, to `only-check-is-current-lockfile-cross-platform`.
762+
> Why? When v2.x will be introduced - the default mode will be changed, probably, to `check-lockfile-is-cross-platform`.
763763
>
764764
> You can check available modes for hook below.
765765
> </details>
766766

767767

768-
1. The hook can work in a few different modes: `only-check-is-current-lockfile-cross-platform` with and without [terraform_validate hook](#terraform_validate) and `always-regenerate-lockfile` - only with terraform_validate hook.
768+
1. The hook can work in a few different modes:
769769

770-
* `only-check-is-current-lockfile-cross-platform` without terraform_validate - only checks that lockfile has all required SHAs for all providers already added to lockfile.
770+
1. <details><summary><code>--mode=check-lockfile-is-cross-platform</code> (standalone)</summary>
771+
Checks that lockfile has the same amount of platform (`h1:`) checksums as specified in hook configuration. It **does not** check are these checksums are valid or that they are belongs to needed platforms.
771772

772773
```yaml
773774
- id: terraform_providers_lock
774775
args:
775-
- --hook-config=--mode=only-check-is-current-lockfile-cross-platform
776+
- --hook-config=--mode=check-lockfile-is-cross-platform
776777
```
777778

778-
* `only-check-is-current-lockfile-cross-platform` with [terraform_validate hook](#terraform_validate) - make up-to-date lockfile by adding/removing providers and only then check that lockfile has all required SHAs.
779+
</details>
780+
781+
2. <details><summary><code>--mode=regenerate-lockfile-if-some-platform-missed</code> (standalone)</summary>
782+
783+
Checks that lockfile has all required SHAs for all providers already added to lockfile, and if any missed - try to add them (but could fail if `terraform init` wasn't run previously)
784+
785+
786+
```yaml
787+
- id: terraform_providers_lock
788+
args:
789+
- --hook-config=--mode=regenerate-lockfile-if-some-platform-missed
790+
```
791+
792+
</details>
793+
794+
3. <details><summary><code>--mode=regenerate-lockfile-if-some-platform-missed</code> with <code>terraform_validate</code> hook</summary>
795+
796+
Make up-to-date lockfile by adding/removing providers and only then check that lockfile has all required SHAs. If any missed - adds them.
779797

780798
> **Important**
781-
> Next `terraform_validate` flag requires additional dependency to be installed: `jq`. Also, it could run another slow and time consuming command - `terraform init`
799+
> Next [`terraform_validate`](#terraform_validate) hook flag requires additional dependency to be installed: `jq`. Also, it could run another slow and time consuming command - `terraform init`
782800

783801
```yaml
784802
- id: terraform_validate
@@ -787,10 +805,14 @@ To replicate functionality in `terraform_docs` hook:
787805
788806
- id: terraform_providers_lock
789807
args:
790-
- --hook-config=--mode=only-check-is-current-lockfile-cross-platform
808+
- --hook-config=--mode=regenerate-lockfile-if-some-platform-missed
791809
```
792810

793-
* `always-regenerate-lockfile` only with [terraform_validate hook](#terraform_validate) - regenerate lockfile from scratch. Can be useful for upgrading providers in lockfile to latest versions
811+
</details>
812+
813+
4. <details><summary><code>always-regenerate-lockfile</code> - only with terraform_validate hook.</summary>
814+
815+
Regenerate lockfile from scratch. Can be useful for upgrading providers in lockfile to latest versions
794816

795817
```yaml
796818
- id: terraform_validate
@@ -803,6 +825,8 @@ To replicate functionality in `terraform_docs` hook:
803825
- --hook-config=--mode=always-regenerate-lockfile
804826
```
805827

828+
</details>
829+
806830
2. `terraform_providers_lock` supports custom arguments:
807831

808832
```yaml

hooks/terraform_providers_lock.sh

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ function per_dir_hook_unique_part {
135135
done
136136

137137
# Available options:
138-
# only-check-is-current-lockfile-cross-platform (will be default)
138+
# check-lockfile-is-cross-platform (will be default)
139+
# regenerate-lockfile-if-some-platform-missed
139140
# always-regenerate-lockfile
140141
# TODO: Remove in 2.0
141142
if [ ! "$mode" ]; then
@@ -146,9 +147,25 @@ Check migration instructions at https://github.com/antonbabenko/pre-commit-terra
146147
exit_code=$?
147148
return $exit_code
148149
}
150+
elif [ "$mode" == "only-check-is-current-lockfile-cross-platform" ]; then
151+
common::colorify "yellow" "DEPRECATION NOTICE: Flag '--mode=only-check-is-current-lockfile-cross-platform' was renamed
152+
to '--mode=regenerate-lockfile-if-some-platform-missed' to better reflect its behavior. Please update your configuration.
153+
"
154+
mode="regenerate-lockfile-if-some-platform-missed"
155+
fi
156+
157+
if [ "$mode" == "check-lockfile-is-cross-platform" ]; then
158+
159+
if lockfile_contains_all_needed_sha "$platforms_count"; then
160+
exit 0
161+
fi
162+
163+
common::colorify "red" "\n$dir_path/.terraform.lock.hcl missing some of required platforms.
164+
All required platforms: ${platforms_names[*]}."
165+
exit 1
149166
fi
150167

151-
if [ "$mode" == "only-check-is-current-lockfile-cross-platform" ]; then
168+
if [ "$mode" == "regenerate-lockfile-if-some-platform-missed" ]; then
152169

153170
if lockfile_contains_all_needed_sha "$platforms_count"; then
154171
exit 0

0 commit comments

Comments
 (0)