mirror of
https://github.com/tofuutils/pre-commit-opentofu.git
synced 2025-10-15 17:38:54 +02:00
feat: Add support for set env vars inside hook runtime (#408)
This commit is contained in:
parent
c4f8251d30
commit
d4902313ce
15 changed files with 65 additions and 79 deletions
|
|
@ -26,6 +26,8 @@ function common::initialize {
|
|||
# ARGS (array) arguments that configure wrapped tool behavior
|
||||
# HOOK_CONFIG (array) arguments that configure hook behavior
|
||||
# TF_INIT_ARGS (array) arguments for `terraform init` command
|
||||
# ENVS (array) environment variables will be available
|
||||
# for all 3rd-party tools executed by a hook.
|
||||
# FILES (array) filenames to check
|
||||
# Arguments:
|
||||
# $@ (array) all specified in `hooks.[].args` in
|
||||
|
|
@ -37,9 +39,11 @@ function common::parse_cmdline {
|
|||
ARGS=() HOOK_CONFIG=() FILES=()
|
||||
# Used inside `common::terraform_init` function
|
||||
TF_INIT_ARGS=()
|
||||
# Used inside `common::export_provided_env_vars` function
|
||||
ENVS=()
|
||||
|
||||
local argv
|
||||
argv=$(getopt -o a:,h:,i: --long args:,hook-config:,init-args:,tf-init-args: -- "$@") || return
|
||||
argv=$(getopt -o a:,h:,i:,e: --long args:,hook-config:,init-args:,tf-init-args:,envs: -- "$@") || return
|
||||
eval "set -- $argv"
|
||||
|
||||
for argv; do
|
||||
|
|
@ -60,6 +64,11 @@ function common::parse_cmdline {
|
|||
TF_INIT_ARGS+=("$1")
|
||||
shift
|
||||
;;
|
||||
-e | --envs)
|
||||
shift
|
||||
ENVS+=("$1")
|
||||
shift
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
# shellcheck disable=SC2034 # Variable is used
|
||||
|
|
@ -270,3 +279,24 @@ function common::terraform_init {
|
|||
|
||||
return $exit_code
|
||||
}
|
||||
|
||||
#######################################################################
|
||||
# Export provided K/V as environment variables.
|
||||
# Arguments:
|
||||
# env_vars (array) environment variables will be available
|
||||
# for all 3rd-party tools executed by a hook.
|
||||
#######################################################################
|
||||
function common::export_provided_env_vars {
|
||||
local -a -r env_vars=("$@")
|
||||
|
||||
local var
|
||||
local var_name
|
||||
local var_value
|
||||
|
||||
for var in "${env_vars[@]}"; do
|
||||
var_name="${var%%=*}"
|
||||
var_value="${var#*=}"
|
||||
# shellcheck disable=SC2086
|
||||
export $var_name="$var_value"
|
||||
done
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|||
function main {
|
||||
common::initialize "$SCRIPT_DIR"
|
||||
common::parse_cmdline "$@"
|
||||
common::export_provided_env_vars "${ENVS[@]}"
|
||||
common::parse_and_export_env_vars
|
||||
# shellcheck disable=SC2153 # False positive
|
||||
infracost_breakdown_ "${HOOK_CONFIG[*]}" "${ARGS[*]}"
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|||
function main {
|
||||
common::initialize "$SCRIPT_DIR"
|
||||
common::parse_cmdline "$@"
|
||||
common::export_provided_env_vars "${ENVS[@]}"
|
||||
common::parse_and_export_env_vars
|
||||
# Support for setting PATH to repo root.
|
||||
# shellcheck disable=SC2178 # It's the simplest syntax for that case
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|||
function main {
|
||||
common::initialize "$SCRIPT_DIR"
|
||||
common::parse_cmdline "$@"
|
||||
common::export_provided_env_vars "${ENVS[@]}"
|
||||
common::parse_and_export_env_vars
|
||||
# Support for setting relative PATH to .terraform-docs.yml config.
|
||||
# shellcheck disable=SC2178 # It's the simplest syntax for that case
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|||
function main {
|
||||
common::initialize "$SCRIPT_DIR"
|
||||
common::parse_cmdline "$@"
|
||||
common::export_provided_env_vars "${ENVS[@]}"
|
||||
common::parse_and_export_env_vars
|
||||
# shellcheck disable=SC2153 # False positive
|
||||
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|||
function main {
|
||||
common::initialize "$SCRIPT_DIR"
|
||||
common::parse_cmdline "$@"
|
||||
common::export_provided_env_vars "${ENVS[@]}"
|
||||
common::parse_and_export_env_vars
|
||||
# shellcheck disable=SC2153 # False positive
|
||||
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|||
function main {
|
||||
common::initialize "$SCRIPT_DIR"
|
||||
common::parse_cmdline "$@"
|
||||
common::export_provided_env_vars "${ENVS[@]}"
|
||||
common::parse_and_export_env_vars
|
||||
# Support for setting PATH to repo root.
|
||||
# shellcheck disable=SC2178 # It's the simplest syntax for that case
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|||
function main {
|
||||
common::initialize "$SCRIPT_DIR"
|
||||
common::parse_cmdline "$@"
|
||||
common::export_provided_env_vars "${ENVS[@]}"
|
||||
common::parse_and_export_env_vars
|
||||
# Support for setting PATH to repo root.
|
||||
# shellcheck disable=SC2178 # It's the simplest syntax for that case
|
||||
|
|
|
|||
|
|
@ -12,73 +12,13 @@ export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-us-east-1}
|
|||
|
||||
function main {
|
||||
common::initialize "$SCRIPT_DIR"
|
||||
parse_cmdline_ "$@"
|
||||
common::parse_cmdline "$@"
|
||||
common::export_provided_env_vars "${ENVS[@]}"
|
||||
common::parse_and_export_env_vars
|
||||
|
||||
# Export provided env var K/V pairs to environment
|
||||
local var var_name var_value
|
||||
for var in "${ENVS[@]}"; do
|
||||
var_name="${var%%=*}"
|
||||
var_value="${var#*=}"
|
||||
# shellcheck disable=SC2086
|
||||
export $var_name="$var_value"
|
||||
done
|
||||
|
||||
# shellcheck disable=SC2153 # False positive
|
||||
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
||||
}
|
||||
|
||||
#######################################################################
|
||||
# Parse args and filenames passed to script and populate respective
|
||||
# global variables with appropriate values
|
||||
# Globals (init and populate):
|
||||
# ARGS (array) arguments that configure wrapped tool behavior
|
||||
# HOOK_CONFIG (array) arguments that configure hook behavior
|
||||
# TF_INIT_ARGS (array) arguments to `terraform init` command
|
||||
# ENVS (array) environment variables that will be used with
|
||||
# `terraform` commands
|
||||
# FILES (array) filenames to check
|
||||
# Arguments:
|
||||
# $@ (array) all specified in `hooks.[].args` in
|
||||
# `.pre-commit-config.yaml` and filenames.
|
||||
#######################################################################
|
||||
function parse_cmdline_ {
|
||||
declare argv
|
||||
argv=$(getopt -o e:i:a:h: --long envs:,tf-init-args:,init-args:,args: -- "$@") || return
|
||||
eval "set -- $argv"
|
||||
|
||||
for argv; do
|
||||
case $argv in
|
||||
-a | --args)
|
||||
shift
|
||||
ARGS+=("$1")
|
||||
shift
|
||||
;;
|
||||
-h | --hook-config)
|
||||
shift
|
||||
HOOK_CONFIG+=("$1;")
|
||||
shift
|
||||
;;
|
||||
# TODO: Planned breaking change: remove `--init-args` as not self-descriptive
|
||||
-i | --init-args | --tf-init-args)
|
||||
shift
|
||||
TF_INIT_ARGS+=("$1")
|
||||
shift
|
||||
;;
|
||||
-e | --envs)
|
||||
shift
|
||||
ENVS+=("$1")
|
||||
shift
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
FILES=("$@")
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
#######################################################################
|
||||
# Unique part of `common::per_dir_hook`. The function is executed in loop
|
||||
# on each provided dir path. Run wrapped tool with specified arguments
|
||||
|
|
@ -120,7 +60,4 @@ function per_dir_hook_unique_part {
|
|||
return $exit_code
|
||||
}
|
||||
|
||||
# global arrays
|
||||
declare -a ENVS
|
||||
|
||||
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@"
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|||
function main {
|
||||
common::initialize "$SCRIPT_DIR"
|
||||
common::parse_cmdline "$@"
|
||||
common::export_provided_env_vars "${ENVS[@]}"
|
||||
common::parse_and_export_env_vars
|
||||
|
||||
check_dependencies
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|||
function main {
|
||||
common::initialize "$SCRIPT_DIR"
|
||||
common::parse_cmdline "$@"
|
||||
common::export_provided_env_vars "${ENVS[@]}"
|
||||
common::parse_and_export_env_vars
|
||||
# shellcheck disable=SC2153 # False positive
|
||||
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|||
function main {
|
||||
common::initialize "$SCRIPT_DIR"
|
||||
common::parse_cmdline "$@"
|
||||
common::export_provided_env_vars "${ENVS[@]}"
|
||||
common::parse_and_export_env_vars
|
||||
# shellcheck disable=SC2153 # False positive
|
||||
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|||
function main {
|
||||
common::initialize "$SCRIPT_DIR"
|
||||
common::parse_cmdline "$@"
|
||||
common::export_provided_env_vars "${ENVS[@]}"
|
||||
common::parse_and_export_env_vars
|
||||
# shellcheck disable=SC2153 # False positive
|
||||
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
|
|||
function main {
|
||||
common::initialize "$SCRIPT_DIR"
|
||||
common::parse_cmdline "$@"
|
||||
common::export_provided_env_vars "${ENVS[@]}"
|
||||
common::parse_and_export_env_vars
|
||||
# shellcheck disable=SC2153 # False positive
|
||||
common::per_dir_hook "${ARGS[*]}" "$HOOK_ID" "${FILES[@]}"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue