• Stars
    star
    579
  • Rank 74,153 (Top 2 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 2 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cobra CLI tool to generate applications and commands

Cobra Generator

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-cli@latest. Go will automatically install it in your $GOPATH/bin directory which should be in your $PATH.

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

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

cobra-cli init

The cobra-cli 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. 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.

Initalizing a module

If you already have a module, skip this step.

If you want to initialize a new Go module:

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

e.g.

cd $HOME/code 
mkdir myapp
cd myapp
go mod init github.com/spf13/myapp

Initalizing a Cobra CLI application

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

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.

e.g.

cd $HOME/code/myapp
cobra-cli init
go run main.go

cobra-cli init can also be run from a subdirectory such as how the cobra generator itself is organized. 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. e.g. cobra-cli init --author "Steve Francia [email protected]"

You can provide a license to use with --license e.g. cobra-cli init --license apache

Use the --viper flag to automatically setup viper

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.

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

Let's say you created an app and you wanted the following commands for it:

  • app serve
  • app config
  • app config create

In your project directory (where your main.go file is) you would run the following:

cobra-cli add serve
cobra-cli add config
cobra-cli add create -p 'configCmd'

cobra-cli add supports all the same optional flags as cobra-cli init does (described above).

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.

By default cobra-cli 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. For example, cobra-cli add add-user is incorrect, but cobra-cli add addUser is valid.

Once you have run these three commands you would have an app structure similar to the following:

  β–Ύ app/
    β–Ύ cmd/
        config.go
        create.go
        serve.go
        root.go
      main.go

At this point you can run go run main.go and it would run your app. go run main.go serve, go run main.go config, go run main.go config create along with go run main.go help serve, etc. would all work.

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.

For complete details on using the Cobra library, please read the The Cobra User Guide.

Have fun!

Configuring the cobra generator

The Cobra generator will be easier to use if you provide a simple configuration file which will help you eliminate providing a bunch of repeated information in flags over and over.

An example ~/.cobra.yaml file:

author: Steve Francia <[email protected]>
license: MIT
useViper: true

You can also use built-in licenses. For example, GPLv2, GPLv3, LGPL, AGPL, MIT, 2-Clause BSD or 3-Clause BSD.

You can specify no license by setting license to none or you can specify a custom license:

author: Steve Francia <[email protected]>
year: 2020
license:
  header: This file is part of CLI application foo.
  text: |
    {{ .copyright }}

    This is my license. There are many like it, but this one is mine.
    My license is my best friend. It is my life. I must master it as I must
    master my life.

In the above custom license configuration the copyright line in the License text is generated from the author and year properties. The content of the LICENSE file is

Copyright Β© 2020 Steve Francia <[email protected]>

This is my license. There are many like it, but this one is mine.
My license is my best friend. It is my life. I must master it as I must
master my life.

The header property is used as the license header files. No interpolation is done. This is the example of the go file header.

/*
Copyright Β© 2020 Steve Francia <[email protected]>
This file is part of CLI application foo.
*/

More Repositories

1

cobra

A Commander for modern Go CLI interactions
Go
35,876
star
2

viper

Go configuration with fangs
Go
25,815
star
3

spf13-vim

The ultimate vim distribution
Vim Script
15,549
star
4

afero

A FileSystem Abstraction System for Go
Go
5,660
star
5

cast

safe and easy casting from one type to another in Go
Go
3,073
star
6

hyde

Port of Mdo's excellent theme to Hugo
CSS
533
star
7

nitro

Quick and easy performance analyzer library for golang
Go
300
star
8

jwalterweatherman

So you always leave a note
Go
261
star
9

PIV

PHP Integration environment for Vim
Vim Script
251
star
10

spf13.com

Hugo Based website of Steve Francia
CSS
143
star
11

dagobah

dagobah is an awesome RSS feed aggregator & reader written in Go inspired by planet
Go
135
star
12

kaiju

Open source discussion server
JavaScript
113
star
13

herring-cove

Port of arnp's excellent theme to Hugo
CSS
43
star
14

vim-colors

Collection of color schemes for VIM
Vim Script
40
star
15

mongoProducts

Sample data for Mongo Product Presentation
37
star
16

tri

Todo CLI app
Go
32
star
17

dotfiles

spf13's dotfiles
Shell
27
star
18

firstGoApp-Planet

Built for OSCON2014 .. Each step is in it's own branch
Go
20
star
19

Vogon

A MongoDB PHP based blog
JavaScript
16
star
20

Milieu

Location based check in application powered by MongoDB written in Ruby
Ruby
13
star
21

cobra.dev

Cobra website
HTML
7
star
22

relativeTime

Golang relative date formatting (1 day ago, in 2 weeks), etc.
7
star
23

stevefrancia.com

Homepage Website for SteveFrancia.com
HTML
6
star
24

wp_GitHub_Code_Viewer

Plugin for Wordpress to insert file from github into a blog post
PHP
5
star
25

spf13

5
star
26

spf13.github.com

Git Hub Page for spf13
4
star
27

vim-gocode

Vim Plugin of nsf/gocode for use with Vundle/Pathogen
Vim Script
3
star
28

vimbrant

Harnessing the power of vim to turn source into highlighted html
3
star
29

One-More-For-The-Road

A Skeleton App for Sinatra using MongoDB
JavaScript
3
star
30

instant

Instant theme for Hugo (derived from Black Tie Instant)
HTML
2
star
31

Autostamp

Auto update the time and version stamp in the header for VIM
Vim Script
1
star
32

YourFirstRubyMongoDBApp

Skeleton for your first ruby MongoDB app to go along with my workshop
Ruby
1
star