DEV Community

Ryan Tiffany
Ryan Tiffany

Posted on

Quick Project Templates in shell

As someone that has to jump between a host of Terraform deployment environments having a standard template of files for each environment is a real time saver. Today I want to highlight a shell utility I have been using to help streamline this process: prm

prm allows you to use CRUD methodology for projects within your shell. Upon activation, each projects runs its associated start up script and upon deactivation, it can run scripts to clean up the environment.

These start and stop scripts can be used for changing directories, setting environment variables, cleanup, etc.

Example

One of the IBM Cloud Services I interact with the most is Schematics. Schematics is a hosted Terraform environment for defining and deploying Infrastructure as Code both within and outside of IBM Cloud.

Everytime I start a new Schematics project I need to do the following:

  • Create a Directory structure in a specified location
  • Copy a set of example Terraform files
  • Initialize a new Git repository
  • Open Visual Studio Code in the newly created project directory

prm allows me to do that with the following start up script:

# Command line arguments can be used, $3 would be the first argument after your project name.
PROJECT_NAME="$3"
TEMPLATE_DIR="${HOME}/Sync/Code/Terraform/templates/prm/schematics"
TF_DIR="${HOME}/Sync/Code/Terraform"

dt=$(date "+%Y.%m.%d")

if [[ ! -d "${TF_DIR}/${PROJECT_NAME}" ]]; then
    mkdir "${TF_DIR}/${PROJECT_NAME}" && cd $_
    export TF_PROJECT_DIR="${TF_DIR}/${PROJECT_NAME}"
else
    echo "Directory already exists, creating new one with date appended"
    mkdir "${TF_DIR}/${PROJECT_NAME}-${dt}" && cd $_
    export TF_PROJECT_DIR="${TF_DIR}/${PROJECT_NAME}-${dt}"
fi

cp "${TEMPLATE_DIR}"/Terraform.gitignore "${TF_PROJECT_DIR}"/.gitignore
cp "${TEMPLATE_DIR}/main.tf" "${TF_PROJECT_DIR}/main.tf"
cp "${TEMPLATE_DIR}/variables.tf" "${TF_PROJECT_DIR}/variables.tf"
cp "${TEMPLATE_DIR}/providers.tf" "${TF_PROJECT_DIR}/providers.tf"
cp "${TEMPLATE_DIR}/install.yml" "${TF_PROJECT_DIR}/install.yml"
cp "${TEMPLATE_DIR}/installer.sh" "${TF_PROJECT_DIR}/installer.sh"
cp "${TEMPLATE_DIR}/data.tf" "${TF_PROJECT_DIR}/data.tf"

git init 
code . 
Enter fullscreen mode Exit fullscreen mode

Invoking PRM

To start a prm Schematics project I simply run prm start schematics <name of project>.

tycho ◲ prm start schematics testfordevto
Starting project schematics
Initialized empty Git repository in /Users/ryan/Sync/Code/Terraform/testfordevto/.git/

~/Sync/Code/Terraform/testfordevto master*
tycho ◲ ls-l 
-rw-r--r--  1 ryan  staff    68 Jul 14 10:40 data.tf
-rw-r--r--  1 ryan  staff  2440 Jul 14 10:40 install.yml
-rw-r--r--  1 ryan  staff  2259 Jul 14 10:40 installer.sh
-rw-r--r--  1 ryan  staff   599 Jul 14 10:40 main.tf
-rw-r--r--  1 ryan  staff   121 Jul 14 10:40 providers.tf
-rw-r--r--  1 ryan  staff  1136 Jul 14 10:40 variables.tf
Enter fullscreen mode Exit fullscreen mode

When running prm stop schematics prm invokes the following actions:

  • Checks if latest code changes have been pushed to Source control and if not prompts the user to do so.
  • Clears out any local *.tfplan or *.tfvars files.
  • Prompts if any new TODO items need to be added to the README file.
  • Changes directory back to $HOME

Top comments (0)