The cobra Package I
Let’s set up our basic cobra package.
We'll cover the following
cobra
is a very handy and popular Go package that allows us to develop command-line utilities with commands, subcommands, and aliases. If you have ever used hugo
, docker
, or kubectl
, you are going to realize immediately what the cobra
package does, as all these tools are developed using cobra
. Commands can have one or more aliases, which is very handy when we want to please both amateur and experienced users. cobra
also supports persistent flags and local flags, which are flags that are available to all commands and flags that are available to given commands only, respectively. Also, by default, cobra
uses viper
for parsing its command-line arguments.
All cobra
projects follow the same development pattern. We use the cobra
utility, then we create commands, and then we make the desired changes to the generated Go source code files in order to implement the desired functionality. Depending on the complexity of our utility, we might need to make lots of changes to the created files. Although cobra
saves us lots of time, we still have to write the code that implements the desired functionality for each command.
Installation
We need to take some extra steps in order to download the cobra
binary the right way:
Get hands-on with 1400+ tech skills courses.