Recently I have been looking at Istio (a Service Mesh framework). When I saw its source code, I found a new thing called Cobra. After checking it, I found that it is a good thing and can be used in many ways, such as Docker, Kubernetes, etc. In order to better understand these open source frameworks (such as Istio, Kubernetes, etc.), it is necessary to have a detailed understanding of Cobra, which may be used in many places in the future. I’m going to give you a general introduction to Cobra today, so that we can get a sense of it and maybe you’ll use it in your future projects.

1. Overview of Cobra

Cobra is a Golang package that provides a simple interface to create command-line programs. Cobra is also an application that generates an application framework for developing CoBRA-based applications.

1.1 Main Functions

Cobra’s main functions are as follows:

  • Simple sub-command line patterns such asapp server.app fetchAnd so on.
  • Fully compatible withposixCommand line mode.
  • Nested subcommandssubcommand.
  • Support global, local, seriesflags.
  • usecobraEasy to generate applications and commands (cobra init appnamecobra add cmdname).
  • Provide intelligent hints (e.g., output)app srverCommand will prompt you if you want to enterapp server?). .
  • Automatically generatecommandsflagsHelp information.
  • Automatic generation of detailedhelpInformation, such asapp -help.
  • Automatic identification helpflag,-h.--help.
  • Automatically generate applications inbashRun the command to complete automatically.
  • Automatically generate the applicationmanThe manual.
  • Command line alias.
  • The customhelpusageInformation.
  • Optional tight integration with Viper.

These features are tailor-made for a command line program.

1.2 Application Examples

Cobra is used in many Go projects such as Kubernetes, Hugo and Github CLI. More widely used projects include:

(Source: github.com/spf13/cobra…)

  • Arduino CLI
  • Bleve
  • CockroachDB
  • Cosmos SDK
  • Delve
  • Docker (distribution)
  • Etcd
  • Gardener
  • Giant Swarm’s gsctl
  • Git Bump
  • Github CLI
  • GitHub Labeler
  • Golangci-lint
  • GopherJS
  • Helm
  • Hugo
  • Istio
  • Kool
  • Kubernetes
  • Linkerd
  • Mattermost-server
  • Metal Stack CLI
  • Moby (former Docker)
  • Nanobox/Nanopack
  • OpenShift
  • Ory Hydra
  • Ory Kratos
  • Pouch
  • ProjectAtomic (enterprise)
  • Prototool
  • Random
  • Rclone
  • Skaffold
  • Tendermint
  • Twitch CLI
  • Werf

Read these, a word “praise”, two words “excellent”!

Once I know Cobra, WHEN I look at the code of Kubernetes, ETCD, Registry and other open source projects, I have a general idea of how to look. This is why I learned Cobra.

2, concepts,

Cobra is created based on commands, arguments, and flags.

There are a few concepts to be aware of before you start using Cobra: Commands, arguments, and flags.

  • Commands: Commands represent behavior.
  • Arguments: Arguments represent command line arguments.
  • Flags: options represent changes to command behavior, that is, command-line options.

The best command line programs have a more intuitive understanding of how to interact with the user when they are actually used, like reading a beautiful sentence. A COMMAND line program should follow the usual format: APPNAME VERB NOUN –ADJECTIVE or APPNAME COMMAND ARG –FLAG

For example:

# clone = commands, URL = arguments Flag Git clone URL --bareCopy the code