Starting Your Cobra Application
Powerful Command-Line Applications in Go — by Ricardo Gerardi (73 / 127)
👈 Chapter 7 Using the Cobra CLI Framework | TOC | Navigating Your New Cobra Applicati on 👉
Start this project by creating a directory structure for your pScan application under the book’s root directory, and switch to the new directory:
$ mkdir -p $HOME/pragprog.com/rggo/cobra/pScan
$ cd $HOME/pragprog.com/rggo/cobra/pScan
Next, initialize the Go module for this application:
$ go mod init pragprog.com/rggo/cobra/pScan
go: creating new go.mod: module pragprog.com/rggo/cobra/pScan
The Cobra CLI framework works as a library to write CLI applications, and as a code generator to generate boilerplate code for a new CLI tool. You need to install the cobra executable command to generate code. Use go get to download and install Cobra:
$ go get -u github.com/spf13/cobra/cobra@v1.1.3
This command downloads Cobra v1.1.3, which is what the book uses, including all its dependencies. If you want, you can use a later version, but you need to make a few minor adjustments to the code. This command also installs the cobra tool in the $GOBIN or $GOPATH/bin directories. Ensure the correct directory is included in the $PATH so you can execute cobra directly:
$ export PATH=$(go…