From e5fc45116c27152f24b6f544720e5696cd860918 Mon Sep 17 00:00:00 2001 From: umarcor Date: Tue, 29 Jun 2021 06:02:42 +0200 Subject: [PATCH] site: move documentation about CLI --- cobra/README.md => site/content/CLI.md | 46 +++++++++++++------------- 1 file changed, 23 insertions(+), 23 deletions(-) rename cobra/README.md => site/content/CLI.md (89%) diff --git a/cobra/README.md b/site/content/CLI.md similarity index 89% rename from cobra/README.md rename to site/content/CLI.md index f1a60565bc..68537915e5 100644 --- a/cobra/README.md +++ b/site/content/CLI.md @@ -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 +### `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 ` -e.g. +e.g. ``` -cd $HOME/code +cd $HOME/code mkdir myapp cd myapp go mod init github.com/spf13/myapp @@ -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. ``` @@ -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) @@ -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: @@ -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.