Skip to content

Creating a GoLang with Cobra: Hands-On

In this tutorial, we will create a command-line application in GoLang.

Prerequisites

Creating the GoLang application

Starting a GoLang application is quite simple; you just need to create a folder and run the command go mod init [module-path].

Let's create:

mkdir platformoon-cli && cd platformoon-cli
go mod init github.com/mcruzdev/platformoon-cli

If you type tree . in terminal, the output should looks like:

.
└── go.mod

Let's see the go.mod content:

module github.com/mcruzdev/platformoon-cli

go 1.20

Installing Cobra dependency

Cobra overview:

Cobra is a library providing a simple interface to create powerful modern CLI interfaces similar to git & go tools.

Getting the dependency with go:

go get -u github.com/spf13/cobra@latest

Creating the root command (platformoon)

Let's see the initial code:

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/spf13/cobra"
)

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

func NewPlatformoonRoot() *cobra.Command { // (2)
    var root = &cobra.Command{
        Use:   "platformoon",
        Short: "Platformoon CLI facilitates programmers lifes",
        Run: func(cmd *cobra.Command, args []string) {
            log.Println("platformoon works")
        },
    }

    return root
}
  1. main GoLang function
  2. Factory function to create the root *cobra.Command

Testing the CLI

Run the application:

go run cmd/platformoon/platformoon.go

The output should looks something like this:

2023/10/27 20:47:56 platformoon works

Great, let's create the complete command platformoon application create --name [application-name].

Adding subcommands to the platformoon command

func NewPlatformoonRoot() *cobra.Command {
    var root = &cobra.Command{
        Use:   "platformoon",
        Short: "Platformoon CLI facilitates programmers lifes",
        Run: func(cmd *cobra.Command, args []string) {
            log.Println("platformoon works")
        },
    }

    root.AddCommand(NewApplicationCmd()) // (1)

    return root
}

func NewApplicationCmd() *cobra.Command { // (2)
    var cmd = &cobra.Command{
        Use:   "application",
        Short: "This command consists of multiple subcommands to interact with applications",
        Run: func(cmd *cobra.Command, args []string) {
            log.Println("platformoon application works")
        },
    }

    cmd.AddCommand(NewApplicationCreateCmd()) // (3)
    return cmd
}

func NewApplicationCreateCmd() *cobra.Command { // (4)
    var cmd = &cobra.Command{
        Use:   "create",
        Short: "This command is used to create a new application",
        Run: func(cmd *cobra.Command, args []string) {
            log.Println("platformoon application create works")
        },
    }

    return cmd
}
  1. Add [application] command as subcommand
  2. Create application command
  3. Add create command as application subcommand
  4. Create create command

Note

The Cobra documentation recommends organizing the subcommands in a slightly different way, for didactic purposes, and to fit within this tutorial, we will place them in the same file. See more details here.

Building the application

To test how the end user experience will be, let's build our CLI.

go build -o platformoon cmd/platformoon/platformoon.go 

Testing the application

Add permission to execute:

chmod +x platformoon

Execute the following commands and see the result:

./platformoon application
./platformoon application create

Adding the logic to send HTTP request to REST API

So far, the API doesn't exist yet, so let's create it now. Access this tutorial to create a Quarkus application that will serve as our REST API.

Note

Don't worry, we'll return to working with our CLI as soon as we create our REST API.

Commit and push your changes to access later.