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:
If you type tree . in terminal, the output should looks like:
Let's see the go.mod content:
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:
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
}
- main GoLang function
- Factory function to create the root
*cobra.Command
Testing the CLI
Run the application:
The output should looks something like this:
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
}
- Add
[application]command as subcommand - Create
applicationcommand - Add
createcommand asapplicationsubcommand - Create
createcommand
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.
Testing the application
Add permission to execute:
Execute the following commands and see the result:
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.