bin: Add helper to get terraform target list from files

This commit is contained in:
2020-10-05 22:06:58 +02:00
parent 2163292379
commit 54f135e316

24
bin/terraform-get-targets Executable file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/env bash
# Stolen from https://devops.stackexchange.com/a/8984 and adapted to my use case
if [[ -z "$@" ]]; then
echo "Missing file input arguments"
exit 1
fi
for FILE in "$@"
do
RESOURCE=$(sed -n 's/^resource "\([^"]*\)" "\([^"]*\)".*/-target=\1.\2 /gp' $FILE)
MODULE=$(sed -n 's/^module "\([^"]*\)".*/-target=module.\1 /gp' $FILE)
if [[ -z "$RESOURCE" ]] && [[ -z "$MODULE" ]]; then
echo "Cannot detect terraform resource and module in $FILE"
exit 1
fi
if [[ ! -z "$RESOURCE" ]]; then
echo -e $"$RESOURCE"
fi
if [[ ! -z "$MODULE" ]]; then
echo -e $"$MODULE"
fi
done