Refactoring Multi-git to use Cobra - The Plan

In this lesson. you'll apply what you learned about Cobra and how to adapt programs to use Cobra as a library.

We will go back to the multi-git program and refactor it to use Cobra. We already did a lot of the groundwork by organizing the directory structure of multi-git and breaking its functionality between a package and a command. Now, we will complete the process by incorporating Cobra, starting with a plan for the refactoring process.

Refactoring plan

The first step is to understand what is the current state of multi-git is and what the desired state is. Let’s review the current state quickly, describe clearly what the desired state is, and then come up with a concrete plan on how to go about it.

The current state of multi-git

The current version of multi-git is 0.4: https://github.com/the-gigi/multi-git/tree/v0.4

It has the following attributes:

  • Multi-git is not a Cobra application.
  • Multi-git can run git commands against multiple repositories.
  • Multi-git has a cumbersome command-line interface.
  • Multi-git has no usage message and crashes if arguments are incorrect.
  • Multi-git has tests.
  • Multi-git is not easy to extend with additional commands.

Here is the current directory structure

$ tree
.
├── LICENSE
├── README.md
├── cmd
│   └── mg
│       └── main.go
├── e2e_tests
│   ├── e2e_suite_test.go
│   └── multi_git_test.go
├── go.mod
├── go.sum
└── pkg
    ├── helpers
    │   └── helpers.go
    └── repo_manager
        ├── repo_manager.go
        ├── repo_manager_suite_test.go
        └── repo_manager_test.go

The desired state of multi-git

The refactoring process should produce the following result:

  • Multi-git is a Cobra application.
  • Multi-git retains its functionality.
  • Multi-git is more user-friendly.
  • Multi-git has usage message and handles incorrect arguments gracefully.
  • Multi-git has at least the same tests coverage as the current version.
  • Multi-git is easier to extend with additional commands.

Step-by-step plan

To address these concerns and refactor multi-git to be a Cobra application we need to do the following:

  • Design the command structure.
  • Add a root command.
  • Implement the root command.
  • Adapt the helpers package.
  • Refactor the main.go file.
  • Test the refactored multi-git.

Let’s start with the command structure.

Designing the command structure

Multi-git really has a single command only, which takes whatever command-line arguments and passes them to git across all the target repositories. That lands itself nicely to a Cobra application with only a root command.

Multi-git 0.4 can be invoked as follows:

export MG_ROOT=<root dir for all repos>
export MG_REPOS=<repo names>
multi-git --command <git command>

The --command flag is an awkward way to specify the git command. Flags shouldn’t be mandatory and should be used to modify the way the application behaves. It is more appropriate to have the following syntax:

multi-git <git command>

To differentiate between flags to the git command and flag to multi-git itself, the git command will be enclosed in quotes if it has more than one word, including its own flags.i

Try out multi-git in the application shown below.

Get hands-on with 1400+ tech skills courses.