Updating generator documentation and links
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Merging the updated documentation from the user_guide into the cobra/README.md. Adding links as appropriate to both guides.
authored by Steve Francia · committed by Steve Francia · parent c97b7ec · Browse files · 3 files·+76 added−82 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+76 added−82 removed
- other:+76 added−82 removed
Provenance
- Commit
cf87fc4- Tree
ce59ace- Author
Steve Francia <steve.francia@gmail.com>- Committer
Steve Francia <steve.francia@gmail.com>- Parent
c97b7ec
3 files changed+76 added−82 removed
modified:cobra/README.md+69 added-19 removed
1234566788910111012131415161716171819201920212223222324252526272829303132333435282936373839404132424344454647484950515253355455565758385960614062636465666768697071727374757677787980816 unmodified lines8889909192939495969798991001011021032 unmodified lines1061071081096511011111211311411511611711811912075761211221231241251261271281291303 unmodified lines13413513613713813914014114237 unmodified lines# Cobra Generator
Cobra provides its own program that will create your application and add anycommands you want. It's the easiest way to incorporate Cobra into your application.
In order to use the cobra command, compile it using the following command:Install the cobra generator with the command `go install github.com/spf13/cobra/cobra`. Go will automatically install it in your `$GOPATH/bin` directory which should be in your $PATH.
go get github.com/spf13/cobra/cobraOnce installed you should have the `cobra` command available. Confirm by typing `cobra` at a command line.
This will create the cobra executable under your `$GOPATH/bin` directory.There are only two operations currently supported by Cobra generator:
### cobra init
The `cobra init [app]` command will create your initial application codefor you. It is a very powerful application that will populate your program withthe right structure so you can immediately enjoy all the benefits of Cobra. Itwill also automatically apply the license you specify to your application.the right structure so you can immediately enjoy all the benefits of Cobra. It can also apply the license you specify to your application.
Cobra init is pretty smart. You can either run it in your current application directoryor you can specify a relative path to an existing project. If the directory does not exist, it will be created for you.With the introduction of Go modules, the Cobra generator has been simplified totake advantage of modules. The Cobra generator works from within a Go module.
Updates to the Cobra generator have now decoupled it from the GOPATH.As such `--pkg-name` is required.#### Initalizing a module
**Note:** init will no longer fail on non-empty directories.__If you already have a module, skip this step.__
If you want to initialize a new Go module:
1. Create a new directory 2. `cd` into that directory 3. run `go mod init <MODNAME>`
e.g. ```mkdir -p newApp && cd newAppcobra init --pkg-name github.com/spf13/newAppcd $HOME/code mkdir myappcd myappgo mod init github.com/spf13/myapp```
or#### Initalizing an Cobra CLI application
From within a Go module run `cobra init`. This will create a new barebones projectfor you to edit.
You should be able to run you new application immediately. Try it with `go run main.go`.
You will want to open up and edit 'cmd/root.go' and provide your own description and logic.
e.g.```cobra init --pkg-name github.com/spf13/newApp path/to/newAppcd $HOME/code/myappcobra initgo run main.go```
### cobra addCobra init can also be run from a subdirectory such as how the [cobra generator itself is organized](https://github.com/spf13/cobra).This is useful if you want to keep your application code separate from your library code.
Once an application is initialized, Cobra can create additional commands for you.#### Optional flags:You can provide it your author name with the `--author` flag. e.g. `cobra init --author "Steve Francia spf@spf13.com"`
You can provide a license to use with `--license` e.g. `cobra init --license apache`
Use the `--viper` flag to automatically setup [viper](https://github.com/spf13/viper)
Viper is a companion to Cobra intended to provide easy handling of environment variables and config files and seamlessly connecting them to the application flags.
### Add commands to a project
Once a cobra application is initialized you can continue to use cobra generator to add additional commands to your application. The command to do this is `cobra add`.
Let's say you created an app and you wanted the following commands for it:
* app serve* app config6 unmodified linescobra add configcobra add create -p 'configCmd'```
`cobra add` supports all the same optional flags as `cobra init` does mentioned above.
You'll notice that this final command has a `-p` flag. This is used to assign aparent command to the newly added command. In this case, we want to assign the"create" command to the "config" command. All commands have a default parent of rootCmd if not specified.
By default `cobra` will append `Cmd` to the name provided and uses this to name for the internal variable name. When specifying a parent, be sure to match the variable name used in the code.
*Note: Use camelCase (not snake_case/kebab-case) for command names.Otherwise, you will encounter errors.For example, `cobra add add-user` is incorrect, but `cobra add addUser` is valid.*
2 unmodified lines
``` ▾ app/ ▾ cmd/ serve.go config.go create.go serve.go root.go main.go```
At this point you can run `go run main.go` and it would run your app. `go runmain.go serve`, `go run main.go config`, `go run main.go config create` alongwith `go run main.go help serve`, etc. would all work.
Obviously you haven't added your own code to these yet. The commands are readyfor you to give them their tasks. Have fun!You now have a basic Cobra-based application up and running. Next step is to edit the files in cmd and customize them for your application.
For complete details on using the Cobra library, please read the [The Cobra User Guide](https://github.com/spf13/cobra/blob/master/user_guide.md#using-the-cobra-library).
Have fun!
### Configuring the cobra generator
The Cobra generator will be easier to use if you provide a simple configurationfile which will help you eliminate providing a bunch of repeated information in3 unmodified lines
```yamlauthor: Steve Francia <spf@spf13.com>license: MITviper: true```
You can also use built-in licenses. For example, **GPLv2**, **GPLv3**, **LGPL**,**AGPL**, **MIT**, **2-Clause BSD** or **3-Clause BSD**.37 unmodified linesmodified:README.md+6 added-2 removed
64 unmodified lines65 have children commands and optionally run an action.66 67 In the example above, 'server' is the command.68 69-[More about cobra.Command](https://godoc.org/github.com/spf13/cobra#Command)69+[More about cobra.Command](https://pkg.go.dev/github.com/spf13/cobra#Command)70 71 ## Flags72 73 A flag is a way to modify the behavior of a command. Cobra supports20 unmodified lines94 import "github.com/spf13/cobra"95 ```96 97 # Usage98+Cobra provides its own program that will create your application and add any99+commands you want. It's the easiest way to incorporate Cobra into your application.100 99-See [User Guide](user_guide.md).101+For complete details on using the Cobra generator, please read [The Cobra Generator README](https://github.com/spf13/cobra/blob/master/cobra/README.md)102 103+For complete details on using the Cobra library, please read the [The Cobra User Guide](user_guide.md).104+105 # License106 107 Cobra is released under the Apache 2.0 license. See [LICENSE.txt](https://github.com/spf13/cobra/blob/master/LICENSE.txt)modified:user_guide.md+1 added-61 removed
30 unmodified lines31 32 Cobra provides its own program that will create your application and add any33 commands you want. It's the easiest way to incorporate Cobra into your application.34 35-Install the cobra generator with the command `go install github.com/spf13/cobra/cobra`. 36-Go will automatically install it in your $GOPATH/bin directory which should be in your $PATH. 35+For complete details on using the Cobra generator, please read [The Cobra Generator README](https://github.com/spf13/cobra/blob/master/cobra/README.md)36 38-Once installed you should have the `cobra` command available. Confirm by typing `cobra` at a 39-command line. 40-41-There are only two operations currently supported by Cobra generator. 42-43-### 1. Initializing a new project44-45-The Cobra generator works from within a Go module. 46-47-If you haven't yet setup your project as a Go module: 48-49- 1. Create a new directory 50- 2. `cd` into that directory51- 3. run `go mod init <MODNAME>`52-53-From within a Go module run `cobra init`. This will create a new barebones project54-for you to edit. 55-56-You should be able to run you new application immediately. Try it with 57-`go run main.go`. 58-59-You will want to open up and edit 'cmd/root.go' and provide your own description and logic. 60-61-#### Optional flags:62-You can provide it your author name with the `--author` flag. 63-e.g. `cobra init --author "Steve Francia spf@spf13.com"`64-65-You can provide a license to use with `--license` 66-e.g. `cobra init --license apache`67-68-Use the `--viper` flag to automatically setup [viper](https://github.com/spf13/viper)69-70-Viper is a companion to Cobra intended to provide easy handling of environment variables and config files71-and seamlessly connecting them to the application flags.72-73-### 2. Add a command to a project74-75-Once a cobra application is initialized you can continue to use cobra generator to 76-add additional commands to your application. The command to do this is `cobra add`. 77-78-As an example, if I was designing a todo application I would want to have my base `todo` command list the items. 79-I would then add additional commands to display, create, mark complete and delete items.80-81-To 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>`. 83-84-#### Optional flags: 85-86-`cobra add` supports all the same optional flags as `cobra init` does.87-88-Additionally you can provide a parent command for your new command. This defaults to rootCmd if not provided.89-If you want to place your command under a different command, just provide the name of the command.90-91-A todo is a bit too simple to really need a sub sub command. So let's use git as an example. 92-93-If I wanted to create a new git stash command I would do the following:94-`cobra add stash`95-`cobra add pop --parent=stash`96-37 ## Using the Cobra Library38 39 To manually implement Cobra you need to create a bare main.go file and a rootCmd file.40 You will optionally provide additional commands as you see fit.597 unmodified lines