Skip to content
Myr

Update documentation to reflect the module aware generator

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Steve Francia · committed by Steve Francia · parent 9388e79 · Browse files · 2 files+63 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+63 added−3 removed

  • other:+63 added−3 removed

2 files+63 added−3 removed

modified:README.md+2 added-2 removed
modified:user_guide.md+61 added-1 removed
30 unmodified lines
3132Cobra provides its own program that will create your application and add any33commands you want. It's the easiest way to incorporate Cobra into your application.3435[Here](https://github.com/spf13/cobra/blob/master/cobra/README.md) you can find more information about it.35Install the cobra generator with the command `go install github.com/spf13/cobra/cobra`. 36Go will automatically install it in your $GOPATH/bin directory which should be in your $PATH. 3738Once installed you should have the `cobra` command available. Confirm by typing `cobra` at a 39command line. 4041There are only two operations currently supported by Cobra generator. 4243### 1. Initializing a new project4445The Cobra generator works from within a Go module. 4647If you haven't yet setup your project as a Go module: 4849 1. Create a new directory 50 2. `cd` into that directory51 3. run `go mod init <MODNAME>`5253From within a Go module run `cobra init`. This will create a new barebones project54for you to edit. 5556You should be able to run you new application immediately. Try it with 57`go run main.go`. 5859You will want to open up and edit 'cmd/root.go' and provide your own description and logic. 6061#### Optional flags:62You can provide it your author name with the `--author` flag. 63e.g. `cobra init --author "Steve Francia spf@spf13.com"`6465You can provide a license to use with `--license` 66e.g. `cobra init --license apache`6768Use the `--viper` flag to automatically setup [viper](https://github.com/spf13/viper)6970Viper is a companion to Cobra intended to provide easy handling of environment variables and config files71and seamlessly connecting them to the application flags.7273### 2. Add a command to a project7475Once a cobra application is initialized you can continue to use cobra generator to 76add additional commands to your application. The command to do this is `cobra add`. 7778As an example, if I was designing a todo application I would want to have my base `todo` command list the items. 79I would then add additional commands to display, create, mark complete and delete items.8081To add a command to an existing application, make sure you are in the directory with the main.go file and run:82`cobra add <cmdname>`. 8384#### Optional flags: 8586`cobra add` supports all the same optional flags as `cobra init` does.8788Additionally you can provide a parent command for your new command. This defaults to rootCmd if not provided.89If you want to place your command under a different command, just provide the name of the command.9091A todo is a bit too simple to really need a sub sub command. So let's use git as an example. 9293If I wanted to create a new git stash command I would do the following:94`cobra add stash`95`cobra add pop --parent=stash`9697## Using the Cobra Library9899To manually implement Cobra you need to create a bare main.go file and a rootCmd file.100You will optionally provide additional commands as you see fit.
597 unmodified lines