Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
40 changes: 32 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -759,26 +759,44 @@ To replicate functionality in `terraform_docs` hook:
> - --hook-config=--mode=always-regenerate-lockfile
> ```
>
> Why? When v2.x will be introduced - the default mode will be changed, probably, to `only-check-is-current-lockfile-cross-platform`.
> Why? When v2.x will be introduced - the default mode will be changed, probably, to `check-lockfile-is-cross-platform`.
>
> You can check available modes for hook below.
> </details>


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.
1. The hook can work in a few different modes:

* `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.
1. <details><summary><code>--mode=check-lockfile-is-cross-platform</code> (standalone)</summary>
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.
Comment thread
MaxymVlasov marked this conversation as resolved.
Outdated

Comment thread
coderabbitai[bot] marked this conversation as resolved.
```yaml
- id: terraform_providers_lock
args:
- --hook-config=--mode=only-check-is-current-lockfile-cross-platform
- --hook-config=--mode=check-lockfile-is-cross-platform
```

* `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.
</details>

2. <details><summary><code>--mode=regenerate-lockfile-if-some-platform-missed</code> (standalone)</summary>

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)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
Comment thread
MaxymVlasov marked this conversation as resolved.
Outdated


```yaml
- id: terraform_providers_lock
args:
- --hook-config=--mode=regenerate-lockfile-if-some-platform-missed
```

</details>

3. <details><summary><code>--mode=regenerate-lockfile-if-some-platform-missed</code> with <code>terraform_validate</code> hook</summary>

Make up-to-date lockfile by adding/removing providers and only then check that lockfile has all required SHAs. If any missed - adds them.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
Comment thread
MaxymVlasov marked this conversation as resolved.
Outdated

> **Important**
> Next `terraform_validate` flag requires additional dependency to be installed: `jq`. Also, it could run another slow and time consuming command - `terraform init`
> 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`
Comment thread
MaxymVlasov marked this conversation as resolved.
Outdated
Comment thread
MaxymVlasov marked this conversation as resolved.
Outdated

```yaml
- id: terraform_validate
Expand All @@ -787,10 +805,14 @@ To replicate functionality in `terraform_docs` hook:

- id: terraform_providers_lock
args:
- --hook-config=--mode=only-check-is-current-lockfile-cross-platform
- --hook-config=--mode=regenerate-lockfile-if-some-platform-missed
```

* `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
</details>

4. <details><summary><code>always-regenerate-lockfile</code> - only with terraform_validate hook.</summary>
Comment thread
MaxymVlasov marked this conversation as resolved.
Outdated

Regenerate lockfile from scratch. Can be useful for upgrading providers in lockfile to latest versions
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
Comment thread
MaxymVlasov marked this conversation as resolved.
Outdated

```yaml
- id: terraform_validate
Expand All @@ -803,6 +825,8 @@ To replicate functionality in `terraform_docs` hook:
- --hook-config=--mode=always-regenerate-lockfile
```

</details>

2. `terraform_providers_lock` supports custom arguments:

```yaml
Expand Down
54 changes: 49 additions & 5 deletions hooks/terraform_providers_lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ function per_dir_hook_unique_part {
local -a -r args=("$@")

local platforms_count=0
local platforms_names=()
for arg in "${args[@]}"; do
if grep -Eq '^-platform=' <<< "$arg"; then
platforms_count=$((platforms_count + 1))
platforms_names+=("${arg#*=}")
fi
done

Expand All @@ -128,12 +130,33 @@ function per_dir_hook_unique_part {
exit 1
fi
mode=$value

case $mode in
check-lockfile-is-cross-platform) ;;
regenerate-lockfile-if-some-platform-missed) ;;
always-regenerate-lockfile) ;;

only-check-is-current-lockfile-cross-platform)
common::colorify "yellow" "DEPRECATION NOTICE: Flag '--mode=only-check-is-current-lockfile-cross-platform' was renamed
to '--mode=regenerate-lockfile-if-some-platform-missed' to better reflect its behavior. Please update your configuration.
"
mode="regenerate-lockfile-if-some-platform-missed"
;;
*)
common::colorify "red" "Invalid hook config. Supported --mode values are:
- check-lockfile-is-cross-platform
- regenerate-lockfile-if-some-platform-missed
- always-regenerate-lockfile"
exit 1
;;
esac
;;
esac
done

# Available options:
# only-check-is-current-lockfile-cross-platform (will be default)
# check-lockfile-is-cross-platform (will be default)
Comment thread
MaxymVlasov marked this conversation as resolved.
Outdated
# regenerate-lockfile-if-some-platform-missed
# always-regenerate-lockfile
# TODO: Remove in 2.0
if [ ! "$mode" ]; then
Expand All @@ -146,19 +169,40 @@ Check migration instructions at https://github.com/antonbabenko/pre-commit-terra
}
fi

if [ "$mode" == "only-check-is-current-lockfile-cross-platform" ] &&
lockfile_contains_all_needed_sha "$platforms_count"; then
if [ "$mode" == "check-lockfile-is-cross-platform" ]; then
Comment thread
yermulnik marked this conversation as resolved.
Outdated

if lockfile_contains_all_needed_sha "$platforms_count"; then
exit 0
fi
Comment thread
yermulnik marked this conversation as resolved.
Outdated

exit 0
common::colorify "red" "\n$dir_path/.terraform.lock.hcl missing some of required platforms.
All required platforms: ${platforms_names[*]}."
exit 1
fi

if [ "$mode" == "regenerate-lockfile-if-some-platform-missed" ]; then

if lockfile_contains_all_needed_sha "$platforms_count"; then
exit 0
fi

common::colorify "yellow" "\n$dir_path/.terraform.lock.hcl missing some of required platforms.
Comment thread
MaxymVlasov marked this conversation as resolved.
Outdated
All required platforms: ${platforms_names[*]}."
Comment thread
yermulnik marked this conversation as resolved.
Outdated
Comment thread
MaxymVlasov marked this conversation as resolved.
Outdated
fi

#? Don't require `tf init` for providers, but required `tf init` for modules
#? Mitigated by `function match_validate_errors` from terraform_validate hook
# pass the arguments to hook
"$tf_path" providers lock "${args[@]}"

# return exit code to common::per_dir_hook
exit_code=$?
if [[ $exit_code -ne 0 ]]; then
common::colorify "red" "$dir_path run failed. Detailed error above.
Most common issue is that required 'terraform init' command was likely not run before running this hook. It might be run for you automatically by 'terraform_validate' hook - see https://github.com/antonbabenko/pre-commit-terraform#terraform_validate for more details
"
fi

# return exit code to common::per_dir_hook
return $exit_code
}

Expand Down
Loading