Implementing Git Hook-Style functionality in Your Terminal

Dr. GosDr. Gos
3 min read

Introduction

The power of the terminal lies in its flexibility and the ability to customize it to fit our unique workflows. Drawing inspiration from Git hooks, a powerful feature in Git that triggers custom scripts at certain points in its execution flow, we can similarly extend the functionality of our terminal. This article delves into how I used this concept to streamline my workflow, not just limited to Go development, but applicable to a range of tasks in the terminal.

Understanding Git Hooks

Git hooks are scripts that Git executes before or after events such as commit, push, and pull. They are a vital part of automating tasks in the software development process. This concept can be extrapolated to the terminal, where we often perform repetitive tasks that could benefit from automation. For example, I automated the prefixing of each of my commit messages with the ticket id and checks that I don't commit any secrets in my repos (not that well optimized).

Identifying Repetitive Tasks

My journey began with recognizing a repetitive task: running asdf reshim golang every time after using go install or go get. This was necessary due to using asdf as a version manager for Go, but it was easy to forget. This scenario is just one example; think about your daily terminal use and identify similar repetitive commands.

Implementing the hook

To automate this, I created a bash script named .go_install_hook.sh:

#!/bin/bash
# Display a custom pre-run message.
echo "Running a Go command..."

# Run the original 'go run' command.
go "$@"

# Check if the command is 'go run' or 'go install'
if [[ "$1" == "run" ]] || [[ "$1" == "install" ]]; then
  # If it's 'go run' or 'go install', run 'asdf reshim golang'
  echo "it's 'go run' or 'go install', running 'asdf reshim golang'"
  asdf reshim golang
fi

This script acts as a wrapper around your intended command, in my case go whatever allowing you to execute additional logic before or after the main command.

In the example above I print a statement, this is more for debugging purposes, after that I run the intended go command and at the end I check if the command was go run or go install and run the reshim command.

Integrating with the Terminal

The script was then integrated into my daily workflow by aliasing the original command in my bash configuration (.zshrc in my case):

alias go="~/.go_install_hook.sh"

In my case, it was the go command, but this can be adapted to any command where you see a pattern of repetitive post-command actions.

Expanding Beyond Go

While my initial use case was specific to Go and asdf, the principle applies universally. For instance, you could create a script that cleans up temporary files after compiling a program, or one that automatically pushes changes to a remote repository after a successful commit.

Conclusion: A World of Possibilities

This approach opens up a world of possibilities in terminal automation. By emulating the functionality of Git hooks, we can create a more efficient, error-resistant, and customized command-line experience. This method's adaptability across various commands and workflows demonstrates the power of custom scripting in enhancing our daily interactions with the terminal.

0
Subscribe to my newsletter

Read articles from Dr. Gos directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dr. Gos
Dr. Gos

๐Ÿ‘‹ Hi there! I'm a seasoned developer with over a decade of hands-on experience, primarily in Java. My journey in the tech world has been fueled by a keen interest in exploring diverse programming languages and frameworks. While Java remains my stronghold, I've also ventured into the realms of JavaScript and Golang, finding each to offer unique perspectives and challenges. ๐Ÿš€ As a developer, I thrive in the backend, where I love to delve deep into the intricacies of server-side programming. However, my curiosity doesn't stop there! I have a passion for experimenting with various frontend frameworks. Svelte, Angular, and Vue have been my playgrounds of choice, allowing me to craft engaging and dynamic user experiences. It's worth noting, though, that React and I have never crossed paths โ€” and that's a conscious choice. I believe in focusing on the tools and technologies that resonate with my style and goals, and React just doesn't fit into that picture. ๐Ÿ—๏ธ Currently, I'm embracing the role of an architect at a small-to-medium-sized company. Here, my focus is on enhancing our delivery speed and efficiency. I'm constantly seeking ways to optimize our processes, ensuring that we stay agile and responsive in our project execution. ๐Ÿ’ก Beyond the code, I'm an avid learner and a collaborator at heart. I enjoy sharing insights, engaging in tech discussions, and contributing to a vibrant, knowledge-rich community. ๐ŸŒฑ Looking forward, I'm excited to continue my exploration of the tech landscape, always seeking new challenges and opportunities to grow. Let's connect and share our journeys in this ever-evolving world of technology!