[Awesome Go] Use corbar package to create powerful modern CLI interfaces similar to git & go tools
Cobra
Cobra is both a library for creating powerful modern CLI applications as well as a program to generate applications and command files.
Cobra is used in many Go projects such as [Kubernetes - https://kubernetes.io/], Hugo - https://gohugo.io/, and Github CLI - https://github.com/cli/cli to name a few. This list - https://github.com/spf13/cobra/blob/master/projects_using_cobra.md contains a more extensive list of projects using Cobra.
Cobra provides:
-
Easy subcommand-based CLIs: app server, app fetch, etc.
-
Fully POSIX-compliant flags (including short & long versions)
-
Nested subcommands
-
Global, local and cascading flags
-
Easy generation of applications & commands with cobra init appname & cobra add cmdname
-
Intelligent suggestions (app srver… did you mean app server?)
-
Automatic help generation for commands and flags
-
Automatic help flag recognition of -h, --help, etc.
-
Automatically generated shell autocomplete for your application (bash, zsh, fish, powershell)
-
Automatically generated man pages for your application
-
Command aliases so you can change things without breaking them
-
The flexibility to define your own help, usage, etc.
-
Optional tight integration with viper for 12-factor apps
Concepts
Cobra is built on a structure of commands, arguments & flags.
Commands represent actions, Args are things and Flags are modifiers for those actions.
The best applications read like sentences when used, and as a result, users intuitively know how to interact with them.
The pattern to follow is APPNAME VERB NOUN --ADJECTIVE
. or APPNAME COMMAND ARG --FLAG
A few good real world examples may better illustrate this point.
In the following example, ‘server’ is a command, and ‘port’ is a flag:
1 | hugo server --port=1313 |
In this command we are telling Git to clone the url bare.
1 | git clone URL --bare |
Commands
Command is the central point of the application. Each interaction that the application supports will be contained in a Command. A command can have children commands and optionally run an action.
In the example above, ‘server’ is the command.
More about cobra.Command - https://godoc.org/github.com/spf13/cobra#Command
Flags
A flag is a way to modify the behavior of a command. Cobra supports fully POSIX-compliant flags as well as the Go flag package - https://golang.org/pkg/flag/. A Cobra command can define flags that persist through to children commands and flags that are only available to that command.
In the example above, ‘port’ is the flag.
Flag functionality is provided by the pflag library - https://github.com/spf13/pflag, a fork of the flag standard library which maintains the same interface while adding POSIX compliance.
Installation
Standard go get:
1 | go get -u github.com/spf13/cobra |
This command will install the cobra
generator executable along with the library and its dependencies:
Next, include it in your application:
1 | import "github.com/spf13/cobra" |
Usage
Getting Started
While you are welcome to provide your own organization, typically a Cobra-based application will follow the following organizational structure:
1 | ▾ appName/ |
In a Cobra app, typically the main.go
file is very bare. It serves one purpose: initializing Cobra.
1 | package main |
Using the Cobra Library
To manually implement Cobra you need to create a bare main.go file and a rootCmd file. You will optionally provide additional commands as you see fit.
Create rootCmd
Cobra doesn’t require any special constructors. Simply create your commands.
Ideally you place this in app/cmd/root.go:
1 | var rootCmd = &cobra.Command{ |
You will additionally define flags and handle configuration in your init() function.
For example cmd/root.go:
1 | package cmd |
Create your main.go
With the root command you need to have your main function execute it. Execute should be run on the root for clarity, though it can be called on any command.
In a Cobra app, typically the main.go file is very bare. It serves one purpose: to initialize Cobra.
1 | package main |
Create additional commands
Additional commands can be defined and typically are each given their own file inside of the cmd/ directory.
If you wanted to create a version command you would create cmd/version.go and populate it with the following:
1 | package cmd |
Returning and handling errors
If you wish to return an error to the caller of a command, RunE can be used.
1 | package cmd |
The error can then be caught at the execute function call.
References
[1] GitHub - spf13/cobra: A Commander for modern Go CLI interactions - https://github.com/spf13/cobra