Build a CLI in Golang with Cobra

Build a CLI in Golang with Cobra

Introduction

I do not know about you but I wouldn't say I like snakes. The slither and the fact that this belly-crawling reptile can pack venom to kill you just terrifies me.

However, in this article, we are not talking about Cobra the Snake but a Go package that can be used to create powerful and fully functional CLI applications without unnecessary headaches. Shall we?

Why the command Line?

You can be fast with command line commands. That’s not the only reason though.

Interacting with your computer can also be done without clicking buttons or moving your mouse around.

CLIs can help developers like you and me to automate tasks, manipulate files, and run programs quickly.

Cobra is a tool that helps you build CLI apps in Golang. You can build very powerful and sophisticated apps with less effort and I guess that is the reason why Cobra is so powerful for creating CLI applications in Golang.

Setting up your Go environment

It is very important to get the right tools for the job. If you do not have Go installed on your computer you can find it here go docs

You will need an IDE or any text editor of your choice. I am using VsCode at the time of writing this article. It is just convenient for me.

Create a folder where you can keep all your projects and navigate into that folder.

mkdir Go projects && cd Go projects

Now you should be in the Go Projects directory. In this directory, you would create your CLI project.

mkdir goCli

In this directory, you need to do two things. Create a main.go file and initialize a go project.

You can initialize a go project by running go mod init <package name> This will create a mod file to keep track of your dependencies.

go mod init <package name>

Next, you need to create a main.go file that will be your entry point to the CLI app you are about to build. Create this file in your goCli project.

touch main.go

For this CLI you will be using the jokeapi wrapper in Golang. It makes it easy to send a request and get a response back really easy. It also helps you to have some configuration neatly as you will see later.

To build your CLI app using Cobra you will create two folders cmd and jokeapi. These folders are important and you will see why in a moment.

The cmd folder will hold all the commands of your CLI. Whenever you want to add a new command it goes here.

jokeapi holds the function that runs whenever the command you added to the cmd is triggered.

In the cmd folder create two files root.go and getJoke.go. Also in the jokeapi folder create one file jokeapi.go.

NB: The main.go file is where your CLI starts it runs the root command.

Adding commands

Now that you are all set up it is time to add commands to your CLI application. It is time to start adding commands. Commands are what users type to use your CLI application.

For example, typing myCli time could print out the current time to your users.

First things first. You have to set up a root command. The root command is the main one that runs when the user types in your CLI name. Every other command will be added to this command.

Here is how to go about it

  1. Go to cmd/root.go.

  2. In this file add the below code block.


package cmd

import (
    "fmt"
    "os"

    "github.com/spf13/cobra"
)

var rootCMD = &cobra.Command{
    Use: "Joke",
    Short: "A simple CLI to tell you a joke when you are bored",
    Long:  "Not super fancy at all",
    Run: func(cmd *cobra.Command, args []string) {
        fmt.Println("Welcome! Get jokes delivered to your terminal whenever you are bored")
    },
}


func Execute() {
    if err := rootCMD.Execute(); err != nil {
        fmt.Fprintf(os.Stderr, "Error executing your command %s", err)
        os.Exit(1)
    }
}

The use field is the name of your CLI(Joke in your case). The short gives a short description of what the command is all about. If no other command is given the Run function will fire.

Finally in your main.go , import your cmd, and call the Execute function to fire the root command.

package main

import "github.com/Shoetan/joke-cli/cmd"

func main()  {
    cmd.Execute()
}

Add command functions

Your CLI app will fire off some functions when a user types in your CLI app. Remember we created a folder called jokeapi which houses a file called getJoke.go.

In this file, we will write the function our subcommand will run when called.

Add the below code block to your getJoke.go.

This will fetch you a fresh joke from the joke API every time.

package jokeapi

import (
    "fmt"

    joke "github.com/icelain/jokeapi"
)

func GetJoke()  {

    jokeType := "single"
    blacklist := []string{"nsfw", "religious", "political", "racist", "sexist"}


    api := joke.New()

    api.Set(joke.Params{Blacklist: blacklist, JokeType: jokeType})

    response, err := api.Fetch()

    if err != nil {
        fmt.Printf("Error getting joke %s", err)
    }

    fmt.Println(response.Joke)

}

Adding Subcommands

Once the root command is up and running, you can add subcommands. A subcommand comes after the root command. For example Joke Tell me a joke , the subcommand here is Tell me a joke.

Add your subcommand to the cmd folder in the file you created earlier getJoke.go Inside that file add the below code block.

package cmd

import (
    "github.com/Shoetan/joke-cli/jokeapi"
    "github.com/spf13/cobra"
)



var jokeCMD = &cobra.Command{
    Use: "Tell me a joke",
    Short: "Getting joke from jokeapi",
    Aliases: []string{"joke"},
    Long:  "Something to make you giggle",
    Run: func(cmd *cobra.Command, args []string) {
        jokeapi.GetJoke()
    },
}

func init(){
    rootCMD.AddCommand(jokeCMD)
}

Building and Running Your CLI Application

Once you've added your commands, it’s time to turn your CLI into an executable program and test it out.

To build your CLI application into a file you can run, follow these steps: Open your terminal and navigate to your project folder run the following command go build.

This will create an executable file (usually named after your project, like goCli ). If you're on Windows, it will be goCli.exe.

Running and Testing Your CLI Application

Now that you’ve built your CLI, you can run and test it: In the terminal, type ./goCli (or goCli.exe on Windows) to run the program.

Congratulations on building your CLI application. You can take this even further. Your only limit is your imagination.