Skip to content

Instantly share code, notes, and snippets.

@mniak
Last active February 11, 2022 18:45
Show Gist options
  • Save mniak/1fd10046799c612bcdf1bf807450f08b to your computer and use it in GitHub Desktop.
Save mniak/1fd10046799c612bcdf1bf807450f08b to your computer and use it in GitHub Desktop.
Script to create files in all subfolders in terraform
#!/bin/bash
## Install as script in local directory
# wget https://gist.githubusercontent.com/mniak/1fd10046799c612bcdf1bf807450f08b/raw/mktf.sh; chmod +x mktf.sh
## Install as system-wide tool in a development box
# su
# curl -o /bin/mktf https://gist.githubusercontent.com/mniak/1fd10046799c612bcdf1bf807450f08b/raw/mktf.sh; chmod +x /bin/mktf
LEVELNAME_ROOT="root"
LEVELNAME_REGION="region"
LEVELNAME_ENV="env"
level=$1
name=$2
if [[ "$name" == "" ]]; then
echo "The 1st argument (level) is missing"
exit 1
fi
if [[ "$name" == "" ]]; then
echo "The 2nd argument (name) is missing"
exit 2
fi
create_file () {
if [[ "$level" == "$1" ]]; then
[[ -f "$name.tf" ]] || touch "$name.tf"
fi
}
echo "Creating files $name.tf on level $level"
create_file $LEVELNAME_ROOT
for region in */; do
[[ "$region" == "*/" ]] && continue
cd $region
create_file $LEVELNAME_REGION
for environment in */; do
[[ "$environment" == "*/" ]] && continue
cd "$environment"
create_file $LEVELNAME_ENV
if [[ ! -f "$name-$level.tf" ]]; then
[[ "$level" == "$LEVELNAME_ROOT" ]] && ln -s "../../$name.tf" "$name.tf"
[[ "$level" == "$LEVELNAME_REGION" ]] && ln -s "../$name.tf" "$name.tf"
fi
cd ..
done
cd ..
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment