• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

cmd/H07-Jun-2019-3,6642,813

tpl/H07-Jun-2019-15495

README.mdH A D07-Jun-20192.7 KiB9568

main.goH A D07-Jun-2019692 215

README.md

1# Cobra Generator
2
3Cobra provides its own program that will create your application and add any
4commands you want. It's the easiest way to incorporate Cobra into your application.
5
6In order to use the cobra command, compile it using the following command:
7
8    go get github.com/spf13/cobra/cobra
9
10This will create the cobra executable under your `$GOPATH/bin` directory.
11
12### cobra init
13
14The `cobra init [app]` command will create your initial application code
15for you. It is a very powerful application that will populate your program with
16the right structure so you can immediately enjoy all the benefits of Cobra. It
17will also automatically apply the license you specify to your application.
18
19Cobra init is pretty smart. You can provide it a full path, or simply a path
20similar to what is expected in the import.
21
22```
23cobra init github.com/spf13/newApp
24```
25
26### cobra add
27
28Once an application is initialized, Cobra can create additional commands for you.
29Let's say you created an app and you wanted the following commands for it:
30
31* app serve
32* app config
33* app config create
34
35In your project directory (where your main.go file is) you would run the following:
36
37```
38cobra add serve
39cobra add config
40cobra add create -p 'configCmd'
41```
42
43*Note: Use camelCase (not snake_case/snake-case) for command names.
44Otherwise, you will encounter errors.
45For example, `cobra add add-user` is incorrect, but `cobra add addUser` is valid.*
46
47Once you have run these three commands you would have an app structure similar to
48the following:
49
50```
51  ▾ app/
52    ▾ cmd/
53        serve.go
54        config.go
55        create.go
56      main.go
57```
58
59At this point you can run `go run main.go` and it would run your app. `go run
60main.go serve`, `go run main.go config`, `go run main.go config create` along
61with `go run main.go help serve`, etc. would all work.
62
63Obviously you haven't added your own code to these yet. The commands are ready
64for you to give them their tasks. Have fun!
65
66### Configuring the cobra generator
67
68The Cobra generator will be easier to use if you provide a simple configuration
69file which will help you eliminate providing a bunch of repeated information in
70flags over and over.
71
72An example ~/.cobra.yaml file:
73
74```yaml
75author: Steve Francia <spf@spf13.com>
76license: MIT
77```
78
79You can specify no license by setting `license` to `none` or you can specify
80a custom license:
81
82```yaml
83license:
84  header: This file is part of {{ .appName }}.
85  text: |
86    {{ .copyright }}
87
88    This is my license. There are many like it, but this one is mine.
89    My license is my best friend. It is my life. I must master it as I must
90    master my life.
91```
92
93You can also use built-in licenses. For example, **GPLv2**, **GPLv3**, **LGPL**,
94**AGPL**, **MIT**, **2-Clause BSD** or **3-Clause BSD**.
95