Building a CLI Tool in Go: Part 1 - Project Setup

SERIES · BUILDING A CLI TOOL IN GO
1 / 3 View series

Every side project starts with optimism. This one started with go mod init and a vague sense of dread.

In this series, we’ll build a real CLI tool in Go — something that actually solves a problem rather than just proving we can parse flags. We’ll use Cobra for the CLI framework and ship it with proper tests and CI.

Project Structure

// main.go
package main

import "github.com/fparch/ddd-cli/cmd"

func main() {
	cmd.Execute()
}
// cmd/root.go
package cmd

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use:   "ddd",
	Short: "A CLI that does things you probably don't need",
	Long:  `But you'll build it anyway because that's what we do.`,
}

func Execute() {
	if err := rootCmd.Execute(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

Why Cobra?

Three reasons:

  1. Subcommand support out of the box
  2. Auto-generated help and completions
  3. It’s what kubectl uses, and if it’s good enough for Kubernetes…

What’s Next

In Part 2, we’ll add actual commands and wire up configuration with Viper.

fparch/ddd-cli View the complete source on GitHub

Need help building developer tools or CLI applications? Let’s talk.

I help teams modernize legacy systems with AI-assisted development guardrails that keep the agents productive and the production safe. fitzpatricksoftware.com