...

/

Refactoring Multi-git to use Cobra - The Root Command

Refactoring Multi-git to use Cobra - The Root Command

In this lesson, we will do the heavy lifting. First, we'll create a Cobra root command and then port the logic in the current main.go of multi-git into the root command.

Adding the root command

Since multi-git is an existing application, we will not use the Cobra generator and just add the root command ourselves. Let’s place it according to Cobra conventions in cmd/root.go:

package cmd

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any sub-commands
var rootCmd = &cobra.Command{
	Use:   "multi-git",
	Short: "Runs git commands over multiple repos",
	Long: `Runs git commands over multiple repos.

Requires the following environment variables defined:	
MG_ROOT: root directory of target git repositories
MG_REPOS: list of repository names to operate on`,
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) { 
		
	},
}

The root command expects exactly one argument, which is the git command. If flags are necessary the user will surround them in ...