-
-
Notifications
You must be signed in to change notification settings - Fork 566
feat: Add terraform_graph
hook
#589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
9a472b2
3527e5e
820cac9
60eb114
5cf8772
b273235
a4999f9
3d54d6c
190de55
2e6e74f
c1dfd0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,91 @@ | ||||||||||||||||||||||||||||||
#!/usr/bin/env bash | ||||||||||||||||||||||||||||||
set -eo pipefail | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# globals variables | ||||||||||||||||||||||||||||||
# shellcheck disable=SC2155 # No way to assign to readonly variable in separate lines | ||||||||||||||||||||||||||||||
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" | ||||||||||||||||||||||||||||||
# shellcheck source=_common.sh | ||||||||||||||||||||||||||||||
. "$SCRIPT_DIR/_common.sh" | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
function main { | ||||||||||||||||||||||||||||||
common::initialize "$SCRIPT_DIR" | ||||||||||||||||||||||||||||||
common::parse_cmdline "$@" | ||||||||||||||||||||||||||||||
common::export_provided_env_vars "${ENV_VARS[@]}" | ||||||||||||||||||||||||||||||
common::parse_and_export_env_vars | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# shellcheck disable=SC2153 # False positive | ||||||||||||||||||||||||||||||
common::per_dir_hook "$HOOK_ID" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}" | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
####################################################################### | ||||||||||||||||||||||||||||||
# Unique part of `common::per_dir_hook`. The function is executed in loop | ||||||||||||||||||||||||||||||
# on each provided dir path. Run wrapped tool with specified arguments | ||||||||||||||||||||||||||||||
# Arguments: | ||||||||||||||||||||||||||||||
# dir_path (string) PATH to dir relative to git repo root. | ||||||||||||||||||||||||||||||
# Can be used in error logging | ||||||||||||||||||||||||||||||
# change_dir_in_unique_part (string/false) Modifier which creates | ||||||||||||||||||||||||||||||
# possibilities to use non-common chdir strategies. | ||||||||||||||||||||||||||||||
# Availability depends on hook. | ||||||||||||||||||||||||||||||
# args (array) arguments that configure wrapped tool behavior | ||||||||||||||||||||||||||||||
# Outputs: | ||||||||||||||||||||||||||||||
# If failed - print out hook checks status | ||||||||||||||||||||||||||||||
####################################################################### | ||||||||||||||||||||||||||||||
function per_dir_hook_unique_part { | ||||||||||||||||||||||||||||||
# shellcheck disable=SC2034 # Unused var. | ||||||||||||||||||||||||||||||
local -r dir_path="$1" | ||||||||||||||||||||||||||||||
# shellcheck disable=SC2034 # Unused var. | ||||||||||||||||||||||||||||||
local -r change_dir_in_unique_part="$2" | ||||||||||||||||||||||||||||||
shift 2 | ||||||||||||||||||||||||||||||
local -a -r args=("$@") | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if [[ ! $(command -v dot) ]]; then | ||||||||||||||||||||||||||||||
echo "ERROR: dot is required by terraform_graph pre-commit hook but is not installed or in the system's PATH." | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add some colors
Suggested change
|
||||||||||||||||||||||||||||||
exit 1 | ||||||||||||||||||||||||||||||
fi | ||||||||||||||||||||||||||||||
Comment on lines
+41
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a robust check for GraphViz's The current test if ! command -v dot >/dev/null 2>&1; then
common::colorify "red" "ERROR: \`dot\` is required but not found in PATH."
exit 1
fi This reliably detects |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# set file name passed from --hook-config | ||||||||||||||||||||||||||||||
local text_file="tf-graph.svg" | ||||||||||||||||||||||||||||||
IFS=";" read -r -a configs <<< "${HOOK_CONFIG[*]}" | ||||||||||||||||||||||||||||||
for c in "${configs[@]}"; do | ||||||||||||||||||||||||||||||
IFS="=" read -r -a config <<< "$c" | ||||||||||||||||||||||||||||||
key=${config[0]} | ||||||||||||||||||||||||||||||
value=${config[1]} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
case $key in | ||||||||||||||||||||||||||||||
--path-to-file) | ||||||||||||||||||||||||||||||
text_file=$value | ||||||||||||||||||||||||||||||
;; | ||||||||||||||||||||||||||||||
esac | ||||||||||||||||||||||||||||||
done | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
temp_file=$(mktemp) | ||||||||||||||||||||||||||||||
# pass the arguments to hook | ||||||||||||||||||||||||||||||
echo "${args[@]}" >> per_dir_hook_unique_part | ||||||||||||||||||||||||||||||
Comment on lines
+61
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unintended debug output Left-over debug lines append |
||||||||||||||||||||||||||||||
terraform graph "${args[@]}" | dot -Tsvg > "$temp_file" | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Respect the detected Terraform binary path Rather than hardcoding - terraform graph "${args[@]}" | dot -Tsvg > "$temp_file"
+ "$tf_path" graph "${args[@]}" | dot -Tsvg > "$temp_file" This ensures compatibility with custom Terraform/OpenTofu installations. 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# check if files are the same | ||||||||||||||||||||||||||||||
cmp -s "$temp_file" "$text_file" | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# return exit code to common::per_dir_hook | ||||||||||||||||||||||||||||||
local exit_code=$? | ||||||||||||||||||||||||||||||
mv "$temp_file" "$text_file" | ||||||||||||||||||||||||||||||
return $exit_code | ||||||||||||||||||||||||||||||
Comment on lines
+67
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Only update SVG on content changes The current logic always moves the temp file, updating the SVG even when identical. This can trigger false diffs. Instead: - cmp -s "$temp_file" "$text_file"
- local exit_code=$?
- mv "$temp_file" "$text_file"
- return $exit_code
+ if cmp -s "$temp_file" "$text_file"; then
+ rm "$temp_file"
+ return 0
+ else
+ mv "$temp_file" "$text_file"
+ return 1
+ fi This preserves modification timestamps when no content change has occurred. 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# Arguments: | ||||||||||||||||||||||||||||||
# args (array) arguments that configure wrapped tool behavior | ||||||||||||||||||||||||||||||
####################################################################### | ||||||||||||||||||||||||||||||
function run_hook_on_whole_repo { | ||||||||||||||||||||||||||||||
local -a -r args=("$@") | ||||||||||||||||||||||||||||||
local text_file="graph.svg" | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# pass the arguments to hook | ||||||||||||||||||||||||||||||
echo "${args[@]}" >> run_hook_on_whole_repo | ||||||||||||||||||||||||||||||
terraform graph "$(pwd)" "${args[@]}" | dot -Tsvg > "$text_file" | ||||||||||||||||||||||||||||||
Comment on lines
+83
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Clean up debug echo and use custom Terraform path In - echo "${args[@]}" >> run_hook_on_whole_repo
- terraform graph "$(pwd)" "${args[@]}" | dot -Tsvg > "$text_file"
+ "$tf_path" graph "$(pwd)" "${args[@]}" | dot -Tsvg > "$text_file" This prevents stray files and honors custom binaries. |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# return exit code to common::per_dir_hook | ||||||||||||||||||||||||||||||
local exit_code=$? | ||||||||||||||||||||||||||||||
return $exit_code | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@" |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to have an example image of resulting graph here
You can populate it inside
assets/
dir