Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suzuki-shunsuke/b18753a0fcf3a1005d9713a0f72d2fff to your computer and use it in GitHub Desktop.
Save suzuki-shunsuke/b18753a0fcf3a1005d9713a0f72d2fff to your computer and use it in GitHub Desktop.
Shell script to get the list of Terraform local module paths which the specified path depends on.
#!/usr/bin/env bash
#
# Get the list of Terraform local module paths which the specified path depends on.
# This command get dependencies recursively.
#
# Usage:
# $ bash scripts/get_dependencies.sh "<path>"
set -eu
set -o pipefail
get_dependency() {
local target=$1
# shellcheck disable=SC2046,SC2002
cat $(find "$target" -maxdepth 1 -name "*.tf") | grep -e "^ *source *=" | sed 's/.*"\(.*\)".*/\1/' | sort | uniq
}
get_dependencies() {
local target=$1
local t
for t in $(get_dependency "$target"); do
if [[ ! $t =~ ^\./ ]] && [[ ! $t =~ ^\.\./ ]]; then
# https://www.terraform.io/docs/modules/sources.html#local-paths
# > A local path must begin with either ./ or ../
continue
fi
local p
# DEPENDENCY: realpath is required
# alpine: apk add coreutils
p=$(realpath --relative-to="$PWD" "$target/$t")
echo "$p"
get_dependencies "$p"
done
}
get_dependencies "$1" | sort | uniq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment