Getting Started with Cobra
Welcome to Cobra! This tutorial will guide you through the essentials of installing Cobra and creating your first command-line application. By the end of this tutorial, you’ll have a working CLI application with your own custom command.
Prerequisites
Before we begin, make sure you have:
- Go 1.21 or later installed on your system
- Basic familiarity with Go development
- A terminal or command prompt
Step 1: Install Cobra and cobra-cli
First, let’s install the Cobra library and the cobra-cli tool that will help us generate our application structure:
go get -u github.com/spf13/cobra@latestgo install github.com/spf13/cobra-cli@latestThe first command adds Cobra as a dependency, while the second installs the cobra-cli generator tool that makes creating new applications and commands much easier.
Step 2: Create Your Application
Now let’s create your first CLI application using the cobra-cli tool:
cobra-cli init my-cliYour Cobra application is ready at my-cliAdd commands to your application by using the `add` commandThis creates a new directory called my-cli with a complete Go module and basic Cobra application structure. Let’s explore what was created:
cd my-clils -latotal 16drwxr-xr-x 6 user staff 192 Aug 10 12:00 .drwxr-xr-x 3 user staff 96 Aug 10 12:00 ..drwxr-xr-x 3 user staff 96 Aug 10 12:00 cmd-rw-r--r-- 1 user staff 87 Aug 10 12:00 go.mod-rw-r--r-- 1 user staff 155 Aug 10 12:00 go.sum-rw-r--r-- 1 user staff 615 Aug 10 12:00 main.goStep 3: Test Your Base Application
Let’s test the basic application that was generated:
go run main.goA longer description that spans multiple lines and likely containsexamples and usage of using your application. For example:Cobra is a CLI library for Go that empowers applications.This application is a tool to generate the needed filesto quickly create a Cobra application.Great! Your base application is working. You can also test the built-in help:
go run main.go --helpA longer description that spans multiple lines and likely containsexamples and usage of using your application. For example:Cobra is a CLI library for Go that empowers applications.This application is a tool to generate the needed filesto quickly create a Cobra application.Usage:my-cli [flags]Flags:-h, --help help for my-cliStep 4: Add Your First Command
Now let’s add a custom command to make your CLI more interesting:
cobra-cli add hellohello created at cmd/hello.goThis creates a new file cmd/hello.go with a hello command. Let’s test it:
go run main.go hellohello calledPerfect! You now have a working command. Let’s see how it appears in the help:
go run main.go --helpA longer description that spans multiple lines and likely containsexamples and usage of using your application. For example:Cobra is a CLI library for Go that empowers applications.This application is a tool to generate the needed filesto quickly create a Cobra application.Usage:my-cli [command]Available Commands:completion Generate the autocompletion script for the specified shellhello A brief description of your commandhelp Help about any commandFlags:-h, --help help for my-cliUse "my-cli [command] --help" for more information about a command.Step 5: Customize Your Command
Let’s make the hello command more interesting by editing the generated file. Open cmd/hello.go and you’ll see the basic structure that cobra-cli created.
The key part is the Run function where you can add your custom logic:
go run main.go helloHello from your new Cobra CLI application!You can also add flags to your command. Try the built-in help for your hello command:
go run main.go hello --helpA brief description of your commandUsage:my-cli hello [flags]Flags:-h, --help help for helloStep 6: Build Your Application
Finally, let’s build your CLI application into an executable:
go build -o my-cli./my-cli helloHello from your new Cobra CLI application!Next Steps
Now that you have a working Cobra application, you can:
- Add more commands with
cobra-cli add [command-name] - Learn about flags and arguments in the My First CLI tutorial
- Explore command customization in the Customizing Your CLI tutorial
- Read the User Guide for comprehensive documentation
Happy coding with Cobra!