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

..03-May-2022-

cmd/H10-Feb-2021-3,5572,733

tpl/H10-Feb-2021-14688

MakefileH A D10-Feb-2021392 2418

README.mdH A D10-Feb-20213.7 KiB13095

main.goH A D10-Feb-2021744 2710

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 either run it in your current application directory
20or you can specify a relative path to an existing project. If the directory does not exist, it will be created for you.
21
22Updates to the Cobra generator have now decoupled it from the GOPATH.
23As such `--pkg-name` is required.
24
25**Note:** init will no longer fail on non-empty directories.
26
27```
28mkdir -p newApp && cd newApp
29cobra init --pkg-name github.com/spf13/newApp
30```
31
32or
33
34```
35cobra init --pkg-name github.com/spf13/newApp path/to/newApp
36```
37
38### cobra add
39
40Once an application is initialized, Cobra can create additional commands for you.
41Let's say you created an app and you wanted the following commands for it:
42
43* app serve
44* app config
45* app config create
46
47In your project directory (where your main.go file is) you would run the following:
48
49```
50cobra add serve
51cobra add config
52cobra add create -p 'configCmd'
53```
54
55*Note: Use camelCase (not snake_case/kebab-case) for command names.
56Otherwise, you will encounter errors.
57For example, `cobra add add-user` is incorrect, but `cobra add addUser` is valid.*
58
59Once you have run these three commands you would have an app structure similar to
60the following:
61
62```
63  ▾ app/
64    ▾ cmd/
65        serve.go
66        config.go
67        create.go
68      main.go
69```
70
71At this point you can run `go run main.go` and it would run your app. `go run
72main.go serve`, `go run main.go config`, `go run main.go config create` along
73with `go run main.go help serve`, etc. would all work.
74
75Obviously you haven't added your own code to these yet. The commands are ready
76for you to give them their tasks. Have fun!
77
78### Configuring the cobra generator
79
80The Cobra generator will be easier to use if you provide a simple configuration
81file which will help you eliminate providing a bunch of repeated information in
82flags over and over.
83
84An example ~/.cobra.yaml file:
85
86```yaml
87author: Steve Francia <spf@spf13.com>
88license: MIT
89```
90
91You can also use built-in licenses. For example, **GPLv2**, **GPLv3**, **LGPL**,
92**AGPL**, **MIT**, **2-Clause BSD** or **3-Clause BSD**.
93
94You can specify no license by setting `license` to `none` or you can specify
95a custom license:
96
97```yaml
98author: Steve Francia <spf@spf13.com>
99year: 2020
100license:
101  header: This file is part of CLI application foo.
102  text: |
103    {{ .copyright }}
104
105    This is my license. There are many like it, but this one is mine.
106    My license is my best friend. It is my life. I must master it as I must
107    master my life.
108```
109
110In the above custom license configuration the `copyright` line in the License
111text is generated from the `author` and `year` properties. The content of the
112`LICENSE` file is
113
114```
115Copyright © 2020 Steve Francia <spf@spf13.com>
116
117This is my license. There are many like it, but this one is mine.
118My license is my best friend. It is my life. I must master it as I must
119master my life.
120```
121
122The `header` property is used as the license header files. No interpolation is
123done. This is the example of the go file header.
124```
125/*
126Copyright © 2020 Steve Francia <spf@spf13.com>
127This file is part of CLI application foo.
128*/
129```
130