Cobra - Create CLI in Go

Golang for CLI

Go is the perfect language to develop command line applications. Go has a few advantages that really set it apart from other languages:

  1. Single binary
  2. Very fast execution time, no interpreter needed
  3. Go is awesome!
  4. Cross platform support

Command line based applications are nearly as old as computing itself but this doesn’t mean that they haven’t evolved. Traditional cli applications used flags to manage the different behaviors an application could perform. Modern cli applications have evolved to use a combination of commands, subcommands and flags to control behavior.

Using Cobra

This guide is going to implement four scenarios:

  • A command line utility with first level commands only
  • A command line utility with first and second level commands
  • A command line utility with support for command line flags
  • A command line utility with command aliases

Getting cobra

You must install Cobra before beginning – you can install it by executing the following command:

go get github.com/spf13/cobra/cobra

More details can be found here - https://github.com/spf13/cobra.

Folder structure

While you are welcome to provide your own organization, typically a Cobra-based application will follow the following organizational structure:

  ▾ appName/
    ▾ cmd/
        add.go
        your.go
        commands.go
        here.go
      main.go

In a Cobra app, typically the main.go file is very bare. It serves one purpose: initializing Cobra.

package main

import (
  "{pathToYourApp}/cmd"
)

func main() {
  cmd.Execute()
}

Generate CLI

You can generate the CLI code in 2 ways:

  1. Using cobra generator. More details can be found here - https://github.com/spf13/cobra/blob/master/cobra/README.md.
  2. Manually adding using cobra library

Using Cobra generator

1. Create a package - cobra init

mkdir -p newApp && cd newApp
cobra init --pkg-name github.com/spf13/newApp

or

cobra init --pkg-name github.com/spf13/newApp path/to/newApp

2. Add the sub commands - cobra add

Suppose you want to add 3 sub commands - serve, config and create, you can do following:

cobra add serve
cobra add config
cobra add create -p 'configCmd'

Note: Use camelCase (not snake_case/snake-case) for command names. Otherwise, you will encounter errors. For example, cobra add add-user is incorrect, but cobra add addUser is valid.

Once you have run these three commands you would have an app structure similar to the following:

  ▾ app/
    ▾ cmd/
        serve.go
        config.go
        create.go
      main.go

At this point you can run go run main.go and it would run your app. go run main.go serve, go run main.go config, go run main.go config create along with go run main.go help serve, etc. would all work.

Configuring the cobra generator

The Cobra generator will be easier to use if you provide a simple configuration file which will help you eliminate providing a bunch of repeated information in flags over and over.

An example ~/.cobra.yaml file:

author: Steve Francia <[email protected]>
license: MIT

You can also use built-in licenses or custom licenses or none for no license.


See also