Using Shell Functions to Automate Dev Tasks

Using Shell Functions Has Helped Me Become a More Consistant Developer

At work recently we have picked a saying to represent our goals for this year: “counting the cost.” The idea behind this is remembering we only have so much time on this earth and we should use it with purpose. This applies heavily to our personal lives but it also can apply to our work lives. In all things we should work hard and give our all. I’ve been trying to challenge myself to find real ways to implement new systems and changes to my processes at work to be more efficient and remove roadblocks to doing my best work.

Why Did Nobody Tell Me About Shell Functions and Aliases

My coworker sent me an alias he made in his .zshrc file to clone trellis from the roots/trellis repo. I thought it was so cool that with a little setup he can now use the command “gittrel” instead of having to type out the whole git clone command or look up the repo name for the thousandth time.

alias gittrel="git clone [email protected]:roots/trellis.git"

Then the wheels started turning. Cloning trellis is part of setting up our local dev environment for a new site, Wouldn’t it be so cool if we could chain a bunch of commands together? After a bit of googling, I figured out you can use ”&&” to chain commands.

gittrel && getrekt

Then I kept thinking about all the steps we take to set up a dev environment. One of my least favorite tasks is setting up the .env for the new project and copying over fresh salts from WordPress. Unfortunately there isn’t a command for this that can be run directly in the terminal and chained to my other commands. More Googling revealed what I am convinced is the holy grail of developer automation: Custom Shell Functions.

Shell functions are the GOAT

Shell functions are similar to running multiple commands one after the other with the ”&&” delimiter but take it the next step by allowing all the fun things we get with functions: conditionals, variables, errors, echoing etc. Let’s look at a more advanced function.

clone-site() {
  if [ -z "$1" ] || [ -z "$2" ]; then
      echo "Error: Both site name and git repository URL are required"
      echo "Usage: create-site <site_name> <git_repo_url>"
      return 1
  fi

  site_name=$1
  git_repo=$2

  # Your commands here
  echo "Usage: create-site <site_name ex:testing123> <git_repo_url ex:astroblog>"
  
  # Make sure this is run in /Code/sites
  echo "Navigating to /Code/sites"
  cd ~/Code/Sites

  # Create the site directory
  echo "Creating site: $site_name"
  mkdir -p "$site_name"
  echo "cd $site_name"
  cd "$site_name"

  # Clone the repository
  echo "Cloning repository: [email protected]:benjaminsmallwood/$git_repo.git"
  git clone "[email protected]:benjaminsmallwood/$git_repo.git" .
  # Check if git clone was successful
  if [ $? -ne 0 ]; then
      echo "Error: Failed to clone repository. Please check if the repository exists and you have the correct   permissions."
      return 1
  fi

This function “clone-site” has two parameters that must be used in the command: site_name and git_repo. The usage of this command would look like this.

clone-site namethesitethis astroblog

After running the command the code runs from top to bottom creating a directory in my ~/Code/Sites folder and then clones the repo into that new directory. In this example, the repo would be “[email protected]:benjaminsmallwood/astroblog.git”. I also have some error checking at the end to let me know if the repo existed or not.

So Why Is This So Cool?

Being able to chain together multiple commands and run them as a single command probably only saves about twenty minutes of work at most if I run everything correctly. That’s a huge “if.” Half the time setting up a site, especially if I haven’t set one up for a few weeks, I make many mistakes. I either run a command in the wrong order, type in a version number incorrectly, or forget a niche command that we have for our specific environment that I keep forgetting to add to my dev checklist. So for me, the value in these functions isn’t from the time-saving of actually typing in the commands, it’s the time-saving up having all the commands run correctly, without typos, in the right order, and if one fails it can tell me why.

This is just the beginning of how I want to use these functions. I’m looking deeper into using the functions to edit files for me or even see if I can have it interact with a webpage. If I get that ironed out then the goal will be to run the command, wait a few minutes, and then be welcomed by a fresh WordPress install and a dev environment setup with a database and all the launch variables for a production and staging server.

Get Notified When I Post

* indicates required

Intuit Mailchimp