Files
dotfiles/bin/terraform-get-targets

25 lines
619 B
Plaintext
Raw Normal View History

#!/usr/bin/env bash
# Stolen from https://devops.stackexchange.com/a/8984 and adapted to my use case
2024-05-27 12:47:22 +02:00
if (( $# == 0 )) ; then
echo "Missing file input arguments"
exit 1
fi
for FILE in "$@"
do
2024-05-27 12:47:22 +02:00
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
2024-05-27 12:47:22 +02:00
if [[ -n "$RESOURCE" ]]; then
echo -e $"$RESOURCE"
fi
2024-05-27 12:47:22 +02:00
if [[ -n "$MODULE" ]]; then
echo -e $"$MODULE"
fi
done