Improving Multi-git

In this lesson, we will identify various areas of improvement. Later in the course, we will address these areas to make multi-git a professional Go CLI program.

Overview

Okay, multi-git is a small and simple program. It seems like a single function in a single file is not a bad decision, but, we will improve it to serve as the foundation for much larger and more complicated command-line programs. Keep this in mind as we re-organize the code and add powerful capabilities. There are several meta-topics for improvement such as:

  • Refactoring the directory structure
  • Testing
  • Command-line parsing
  • Error handling
  • Configuration
  • Packaging

For long-running programs there are additional concerns such logging and metrics. Let’s explore some of the areas of improvement for Multi-git.

Refactor towards idiomatic Golang

Well-factored Go programs, command-line or otherwise have a directory structure that separates different roles into their own directories, like pkg, cmd, etc.

Add testing

Proper programs in any language should have automated tests that verify that the program works as expected. In fact, each package of the program should also be tested.

Better command-line parsing

Multi-git uses the flag package from the standard library. This is not as bad as reinventing the wheel and directly working with arguments through os.Args, but there are better libraries that are designed for building sophisticated command-line programs. Let’s stand on the shoulders of giants and benefit from their work.

Better error handling

Error handling in multi-git is pretty basic. Not all error conditions are checked and the error reporting is not fantastic either. We can do better.

Add configuration

The current configuration through the MG_ROOT and MG_REPOS environment variables is okay for certain situations, but command-line programs are often used in multiple scenarios, including interactive, batch, and CI/CD pipelines. A more powerful and flexible configuration can be very useful.

Add packaging and upgrade options

Most successful programs evolve over time and need to be packaged, installed, and upgraded regularly. Multi-git doesn’t provide any facilities to assist in managing this aspect, so we can make it much better.

Directory overview

Take a look at the directory structure of multi-git in the following coding playground.

Get hands-on with 1200+ tech skills courses.