-
-
Notifications
You must be signed in to change notification settings - Fork 547
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
feat: Add terraform_graph
hook
#589
Open
jrdnbradford
wants to merge
7
commits into
antonbabenko:master
Choose a base branch
from
jrdnbradford:add-graph-hook
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a472b2
Add `terraform_graph` hook
jrdnbradford 3527e5e
Update default file name
jrdnbradford 820cac9
Add docs
jrdnbradford 60eb114
Remove whitespace
jrdnbradford 5cf8772
Remove `run_hook_on_whole_repo`
jrdnbradford b273235
Re-add `run_hook_on_whole_repo`
jrdnbradford a4999f9
Troubleshooting blank svgs
jrdnbradford File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,74 @@ | ||||||
#!/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 | ||||||
|
||||||
# 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 | ||||||
terraform graph "${args[@]}" | dot -Tsvg > "$temp_file" | ||||||
|
||||||
# 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 | ||||||
} | ||||||
|
||||||
[ "${BASH_SOURCE[0]}" != "$0" ] || main "$@" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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