Supercharge Your Tart: Create Your Own Zsh Function!

Previously, I showed how to manually create a Jamf Pro test VM with Tart. Now, let's take it further—automate the process of setting up that new VM with Zsh functions to save time, reduce errors, and make VM creation effortless.

Supercharge Your Tart: Create Your Own Zsh Function!

Why Automate VM Creation?

If you followed the previous guide, you know that creating a fresh Jamf Pro test VM involves several Tart commands and configuration steps to start it up:

  • tart clone
  • tart set --random-serial --random-mac --display-refit

Doing this repeatedly can be tedious and error-prone, because we all suck at typing on the command line with that so many - dashes.
By creating a Zsh function like starttart, you can bundle all those steps into a single command. This not only speeds up your workflow, but also ensures consistency every time you spin up a new VM for testing or development.

Why Create Custom Zsh Functions?

If you spend a lot of time in the terminal, you've likely found yourself typing the same commands over and over. This is where Zsh functions come to the rescue! Creating your own functions in Zsh offers several fantastic benefits:

  1. Save time & keystrokes: Instead of typing out long or complex sequences of commands (like those needed to initialize a new Jamf Pro test VM), you can encapsulate them into a short, easy-to-remember function name.
  2. Reduce Errors: Complex commands are easy to mistype. Once you've perfected a command sequence and saved it as a function, you can execute it flawlessly every time.
  3. Streamline Your Workflow: Functions can automate multi-step processes. For example, a function could navigate to a project directory, pull the latest changes from git, and start a development server – or, as we'll see, clone and configure a virtual machine with a single command!
  4. Learn Zsh More Deeply: The process of writing functions is an excellent way to get more familiar with Zsh syntax, shell scripting concepts, and the power hidden within your terminal.
  5. Personalize Your Environment: Tailor your shell to your specific needs and habits. Your custom functions make your terminal truly yours.

What Exactly is a Zsh Function?

In simple terms, a Zsh function is a named block of Zsh code that performs a specific task. You define it once, usually in your Zsh configuration file (like .zshrc) or a separate script file that you "source", and then you can call it by its name from anywhere in your terminal, just like any other built-in command. You can even add that source to your .zshrc so you can use when your shell loads.

A perfect example is the starttart function that I created, located in the zsh_function.sh file. This function is designed to dramatically speed up the process of creating and configuring new virtual machines using Tart.
Instead of manually running several tart commands like tart clone ..., then tart set ... --random-serial, tart set ... --random-mac, etc., the starttart function bundles all these steps. You just provide it with a base VM name and a name for your new VM, and it handles the cloning and initial setup (like randomizing the serial number and MAC address) for you.

💻
This is the power of a Zsh function: taking a common, multi-step, and potentially error-prone operation and making it accessible via a single, reliable command.

Let's dive deeper into how our starttart function from zsh_function.sh acts as a real-world time saver, especially when you need to spin up fresh Tart VMs frequently.

The Script

Read script on Github

Regularly creating new Tart VMs involves several command-line steps:

  1. Cloning an existing base image (tart clone <base_image> <new_vm_name>).
  2. Configuring the new VM, often to ensure it's unique (e.g., tart set <new_vm_name> --random-serial --random-mac).
  3. Potentially other setup steps depending on your needs. You can add a tart run
    Doing this manually each time is repetitive and prone to typos or forgotten steps.

The starttart Solution:


Our starttart function, defined in zsh_function.sh, automates this entire sequence.

By simply running:
starttart "your-base-vm-image" "your-new-vm-name"

The function takes care of:

  • Checking if the tart command is available.
  • Validating that you've provided the necessary base and new VM names.
  • Executing tart clone.
  • Executing tart set with options like --random-serial, --random-mac, and --display-refit.
  • Providing feedback throughout the process.

Why is this so useful?

This kind of automation is invaluable when you need to quickly create clean environments. For instance, if you're following a process like the one for setting up specialized test VMs, such as a Jamf Pro test environment (as detailed in a previous post on The Cookbook: Baking Up Your Perfect Jamf Pro Test VM, the starttart function becomes a massive time-saver. It allows you to get a fresh, uniquely configured VM instance ready for your specific testing or development tasks in seconds, with just one command.

How to Get Started with Your Own Functions


Typically, you'd add your functions to your ~/.zshrc file, or for more complex functions like starttart, you might keep them in a separate script file (e.g., zsh_function.sh) and then "source" that file in your ~/.zshrc.
Here's a very basic structure for a function in your .zshrc:

my_simple_function() {  
  # Your commands go here
  echo "Hello from my simple function!"  
  ls -l
  }

After adding this (or sourcing your script file like source /path/to/your/zsh_function.sh), you'd need to either reload your Zsh configuration (e.g., source ~/.zshrc) or open a new terminal window. Then, you can just type my_simple_function or starttart "base" "new" to run your functions.

The GOAL

The goal is to identify those repetitive command patterns in your daily work – like setting up Tart VMs – and turn them into handy functions. It's a small investment of time upfront that pays off significantly in the long run by making your command-line experience faster, more reliable, and more enjoyable.

😂 Happy scripting!

Subscribe to motionbug

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe