Create a CLI with Cobra in Go
In this post, we talk about how we can create a simple CLI with Cobra in Go. Cobra is a library for creating powerful modern CLI interfaces similar to git & go tools.
Introduction
The term CLI stands for Command Line Interface, which is a method of interacting with a computer program through text-based commands entered in a terminal or console. It allows users to perform various tasks by typing specific commands and parameters instead of using a graphical user interface (GUI).
While CLI help improves efficiency, automation, and flexibility, they are typically lightweight and require fewer system resources as compared to GUI applications. However, it is the responsibility of the developer to ensure that the CLI is easy to use, provides help documentation, and also has autocomplete features.
In this article, we will be creating a simple, easy-to-use CLI in Go using the Cobra package. Let’s get started!
In case you prefer following along a video, you can checkout the following video on my YouTube channel.
Project Setup
I would assume you have a working Go environment on your system; if not, you can download and install it from the official page.
Now, let’s start by creating a directory called greeter. Then change into the directory and initialize the Go module using the go mod init command.
|
|
Open the directory in your favorite code editor because it’s time we write some code. I prefer using VIM or VS Code.
The Code
We will be creating a basic CLI named greeter using the Cobra library. The CLI has two commands: the root command, which simply prints a welcome message, and the greet command, which takes an argument (a name) and greets the person by their name.
Start by creating a file called main.go in the current directory and follow along. We start by importing the required packages.
|
|
We imported:
fmtpackage for printing messagesospackage for handling operating system-related functionalitycobrapackage, which is a popular library for building command-line interfaces in Go.
Note that the first two packages are available in the Go standard library, while Cobra is an external package that we need to get before we can use it.
In order to get the package, switch back to the terminal and execute the go-get command shown below.
|
|
Inside the main function, we create the root command using cobra.Command.
|
|
Use- sets the name of our CLI tool (greeter)Short- provides a brief descriptionLong- provides a more detailed descriptionRun- defines the function that will be executed when this command is invoked; in our case, it simply prints a welcome message.
Next, we define a subcommand named greet:
|
|
Similar to the root command, we provide a name (greet), a short description, and a long explanation for the subcommand. The Args field specifies that this command requires exactly one argument. The Run field contains a function that will be executed when this command is invoked. It retrieves the provided name argument and greets the person using fmt.Printf.
We add the greetCmd as a subcommand to the root command.
|
|
This line connects the greetCmd as a subcommand to the rootCmd.
Finally, we execute the root command and handle any errors:
|
|
The Execute method runs the CLI and processes the provided command-line arguments. If there’s an error during execution, it will be printed, and the program will exit with a non-zero status code.
Here is the complete code:
|
|
Build and Run
We can now build our CLI binary by executing the following command.
|
|
Now, if we run the CLI without any subcommand, we get the welcome message.
|
|
A good CLI always has good documentation. We can check the documentation generated for our CLI as well.
|
|
As a final test, let’s execute the greet subcommand as well.
|
|
Conclusion
In this article, we have created a simple yet well-documented CLI in Go using Cobra. In the posts to follow, we will restructure our project to be more organized and also add unit tests for the CLI.
Please share your questions and valuable feedback through comments.
Thank you!