Skip to content

Commit deea7bc

Browse files
committed
Add docs, remove useless logic
1 parent c3a1a0d commit deea7bc

File tree

3 files changed

+67
-22
lines changed

3 files changed

+67
-22
lines changed

README.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,46 @@ To replicate functionality in `terraform_docs` hook:
544544

545545
### terraform_providers_lock
546546

547-
1. The hook requires Terraform 0.14 or later.
548-
1. The hook invokes two operations that can be really slow:
549-
* `terraform init` (in case `.terraform` directory is not initialized)
550-
* `terraform providers lock`
547+
> **Note**: The hook requires Terraform 0.14 or later.
548+
> **Note**: The hook can invoke `terraform providers lock` that can be really slow and requires fetching metadata from remote Terraform registries - not all of that metadata is currently being cached by Terraform.
549+
550+
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.
551+
552+
* `only-check-is-current-lockfile-cross-platform` without terraform_validate - only checks that lockfile have all required SHAs for all already added to lockfile providers.
553+
554+
```yaml
555+
- id: terraform_providers_lock
556+
args:
557+
- --hook-config=--mode=only-check-is-current-lockfile-cross-platform
558+
```
559+
560+
* `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.
561+
562+
> **Note**: Next `terraform_validate` flag requires additional dependency to be installed: `jq`. Also, it could run another slow and time consuming command - `terraform init`
563+
564+
```yaml
565+
- id: terraform_validate
566+
args:
567+
- --hook-config=--retry-once-with-cleanup=true
568+
569+
- id: terraform_providers_lock
570+
args:
571+
- --hook-config=--mode=only-check-is-current-lockfile-cross-platform
572+
```
573+
574+
* `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
575+
576+
```yaml
577+
- id: terraform_validate
578+
args:
579+
- --hook-config=--retry-once-with-cleanup=true
580+
- --tf-init-args=-upgrade
581+
582+
- id: terraform_providers_lock
583+
args:
584+
- --hook-config=--mode=always-regenerate-lockfile
585+
```
551586

552-
Both operations require downloading data from remote Terraform registries, and not all of that downloaded data or meta-data is currently being cached by Terraform.
553587

554588
1. `terraform_providers_lock` supports custom arguments:
555589

@@ -576,6 +610,8 @@ To replicate functionality in `terraform_docs` hook:
576610

577611
1. `terraform_providers_lock` support passing custom arguments to its `terraform init`:
578612

613+
> **Warning** - DEPRECATION NOTICE: This available only in `no-mode` mode, which will be removed in v2.0. Please provide this keys to [`terraform_validate`](#terraform_validate) hook, which, to take effect, should be called before `terraform_providers_lock`
614+
579615
```yaml
580616
- id: terraform_providers_lock
581617
args:

hooks/_common.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ function common::colorify {
312312
# Outputs:
313313
# If failed - print out terraform init output
314314
#######################################################################
315+
# TODO: v2.0: Move it inside terraform_validate.sh
315316
function common::terraform_init {
316317
local -r command_name=$1
317318
local -r dir_path=$2

hooks/terraform_providers_lock.sh

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,32 @@ function per_dir_hook_unique_part {
126126

127127
# Available options:
128128
# only-check-is-current-lockfile-cross-platform (will be default)
129-
# check-is-there-new-providers-added---run-terraform-init
130129
# always-regenerate-lockfile
131130
# no-mode # TODO: Remove in 2.0
132131
[ ! "$mode" ] && mode="no-mode"
133132

134133
if [ "$mode" == "no-mode" ]; then
135134
common::colorify "yellow" "DEPRECATION NOTICE: We introduced '--mode' flag for this hook.
136-
Please specify '--hook-config=--mode=always-regenerate-lockfile' in 'args:' if you'd like to continue using this hook as before.
137-
When v2.x will be introduced - the default mode will be changed.
135+
If you'd like to continue using this hook as before, please:
136+
* Specify '--hook-config=--mode=always-regenerate-lockfile' in 'args:'
137+
* Before 'terraform_providers_lock', add 'terraform_validate' hook with '--hook-config=--retry-once-with-cleanup=true'
138+
* Move '--tf-init-args=' to 'terraform_validate' hook
138139
139-
You can check available hook modes at https://github.com/antonbabenko/pre-commit-terraform#terraform_providers_lock
140+
At the end, you should get config like this:
141+
142+
- id: terraform_validate
143+
args:
144+
- --hook-config=--retry-once-with-cleanup=true
145+
# - --tf-init-args=-upgrade
146+
147+
- id: terraform_providers_lock
148+
args:
149+
- --hook-config=--mode=always-regenerate-lockfile
150+
151+
152+
Why? When v2.x will be introduced - the default mode will be changed.
153+
154+
You can check available modes for hook at https://github.com/antonbabenko/pre-commit-terraform#terraform_providers_lock
140155
"
141156
fi
142157

@@ -146,22 +161,15 @@ You can check available hook modes at https://github.com/antonbabenko/pre-commit
146161
exit 0
147162
fi
148163

149-
common::terraform_init 'terraform providers lock' "$dir_path" || {
150-
exit_code=$?
151-
return $exit_code
152-
}
153-
154-
if [ "$mode" == "check-is-there-new-providers-added---run-terraform-init" ] &&
155-
lockfile_contains_all_needed_sha "$platforms_count" &&
156-
# Check that lockfile contains all providers
157-
#? Don't require `tf init`` for providers, but required `tf init` for modules
158-
terraform providers schema -json > /dev/null 2>&1; then
159-
160-
exit 0
164+
if [ "$mode" == "no-mode" ]; then
165+
common::terraform_init 'terraform providers lock' "$dir_path" || {
166+
exit_code=$?
167+
return $exit_code
168+
}
161169
fi
162170

163171
#? Don't require `tf init`` for providers, but required `tf init` for modules
164-
#? Could be mitigated by `function match_validate_errors {` from tf_validate hook
172+
#? Mitigated by `function match_validate_errors` from terraform_validate hook
165173
# pass the arguments to hook
166174
terraform providers lock "${args[@]}"
167175

0 commit comments

Comments
 (0)