You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Cobra provides its own program that will create your application and add any
99
+
commands you want. It's the easiest way to incorporate Cobra into your application.
98
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).
Cobra init can also be run from a subdirectory such as how the [cobra generator itself is organized](https://github.com/spf13/cobra).
60
+
This is useful if you want to keep your application code separate from your library code.
61
+
62
+
#### Optional flags:
63
+
You can provide it your author name with the `--author` flag.
64
+
e.g. `cobra init --author "Steve Francia spf@spf13.com"`
65
+
66
+
You can provide a license to use with `--license`
67
+
e.g. `cobra init --license apache`
68
+
69
+
Use the `--viper` flag to automatically setup [viper](https://github.com/spf13/viper)
70
+
71
+
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.
72
+
73
+
### Add commands to a project
74
+
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`.
39
77
40
-
Once an application is initialized, Cobra can create additional commands for you.
41
78
Let's say you created an app and you wanted the following commands for it:
42
79
43
80
* app serve
@@ -52,6 +89,14 @@ cobra add config
52
89
cobra add create -p 'configCmd'
53
90
```
54
91
92
+
`cobra add` supports all the same optional flags as `cobra init` does mentioned above.
93
+
94
+
You'll notice that this final command has a `-p` flag. This is used to assign a
95
+
parent command to the newly added command. In this case, we want to assign the
96
+
"create" command to the "config" command. All commands have a default parent of rootCmd if not specified.
97
+
98
+
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.
99
+
55
100
*Note: Use camelCase (not snake_case/kebab-case) for command names.
56
101
Otherwise, you will encounter errors.
57
102
For example, `cobra add add-user` is incorrect, but `cobra add addUser` is valid.*
@@ -62,18 +107,22 @@ the following:
62
107
```
63
108
▾ app/
64
109
▾ cmd/
65
-
serve.go
66
110
config.go
67
111
create.go
112
+
serve.go
113
+
root.go
68
114
main.go
69
115
```
70
116
71
117
At this point you can run `go run main.go` and it would run your app. `go run
72
118
main.go serve`, `go run main.go config`, `go run main.go config create` along
73
119
with `go run main.go help serve`, etc. would all work.
74
120
75
-
Obviously you haven't added your own code to these yet. The commands are ready
76
-
for you to give them their tasks. Have fun!
121
+
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.
122
+
123
+
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).
124
+
125
+
Have fun!
77
126
78
127
### Configuring the cobra generator
79
128
@@ -86,6 +135,7 @@ An example ~/.cobra.yaml file:
86
135
```yaml
87
136
author: Steve Francia <spf@spf13.com>
88
137
license: MIT
138
+
viper: true
89
139
```
90
140
91
141
You can also use built-in licenses. For example, **GPLv2**, **GPLv3**, **LGPL**,
Copy file name to clipboardExpand all lines: user_guide.md
+1-61Lines changed: 1 addition & 61 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,67 +32,7 @@ func main() {
32
32
Cobra provides its own program that will create your application and add any
33
33
commands you want. It's the easiest way to incorporate Cobra into your application.
34
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.
37
-
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 project
44
-
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 directory
51
-
3. run `go mod init <MODNAME>`
52
-
53
-
From within a Go module run `cobra init`. This will create a new barebones project
54
-
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 files
71
-
and seamlessly connecting them to the application flags.
72
-
73
-
### 2. Add a command to a project
74
-
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`
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)
0 commit comments