Skip to content

Commit

Permalink
site: move documentation about CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
umarcor committed Nov 4, 2021
1 parent cf07a9b commit 465bf39
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions cobra/README.md → site/content/CLI.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
# Cobra Generator
# Cobra Generator CLI tool

Cobra provides its own program that will create your application and add any
commands you want. It's the easiest way to incorporate Cobra into your application.

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.
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.

Once installed you should have the `cobra` command available. Confirm by typing `cobra` at a
command line.
Once installed you should have the `cobra` command available. Confirm by typing `cobra` at a
command line.

There are only two operations currently supported by the Cobra generator:
There are only two operations currently supported by the Cobra generator:

### cobra init

The `cobra init [app]` command will create your initial application code
for you. It is a very powerful application that will populate your program with
the right structure so you can immediately enjoy all the benefits of Cobra.
the right structure so you can immediately enjoy all the benefits of Cobra.
It can also apply the license you specify to your application.

With the introduction of Go modules, the Cobra generator has been simplified to
take advantage of modules. The Cobra generator works from within a Go module.
take advantage of modules. The Cobra generator works from within a Go module.

#### Initalizing a module

__If you already have a module, skip this step.__

If you want to initialize a new Go module:
If you want to initialize a new Go module:

1. Create a new directory
1. Create a new directory
2. `cd` into that directory
3. run `go mod init <MODNAME>`

e.g.
e.g.
```
cd $HOME/code
cd $HOME/code
mkdir myapp
cd myapp
go mod init github.com/spf13/myapp
Expand All @@ -42,12 +42,12 @@ go mod init github.com/spf13/myapp
#### Initalizing an Cobra CLI application

From within a Go module run `cobra init`. This will create a new barebones project
for you to edit.
for you to edit.

You should be able to run your new application immediately. Try it with
`go run main.go`.
You should be able to run your 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.
You will want to open up and edit 'cmd/root.go' and provide your own description and logic.

e.g.
```
Expand All @@ -60,10 +60,10 @@ Cobra init can also be run from a subdirectory such as how the [cobra generator
This is useful if you want to keep your application code separate from your library code.

#### Optional flags:
You can provide it your author name with the `--author` flag.
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`
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)
Expand All @@ -72,8 +72,8 @@ Viper is a companion to Cobra intended to provide easy handling of environment v

### Add commands to a project

Once a cobra application is initialized you can continue to use the Cobra generator to
add additional commands to your application. The command to do this is `cobra add`.
Once a cobra application is initialized you can continue to use the 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:

Expand All @@ -93,9 +93,9 @@ cobra add create -p 'configCmd'

You'll notice that this final command has a `-p` flag. This is used to assign a
parent 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.
"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 name for the internal variable name. When specifying a parent, be sure to match the variable name used in the code.
By default `cobra` will append `Cmd` to the name provided and uses this 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.
Expand Down

0 comments on commit 465bf39

Please sign in to comment.