Cobra Walkthrough Guide

Cobra is both a library for creating powerful modern CLI applications as well as a program to generate applications and command files.

Install

$ go get -u github.com/spf13/cobra/cobra

Walkthrough Guide

Configuring the cobra generator by ~/.cobra.yaml

It's a good idea to configure Cobra first before generating any commands as this configuration would be refered by the corba tool.

cat > ~/.cobra.yaml <<EOF
author: Bright Zheng <myemail@company.com>
license: AGPL
EOF

In this case, the licensing info will be attached in each file generated by cobra:

/*
Copyright Β© 2021 Bright Zheng <myemail@company.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

corbra init - Initialize Cobra project

corbra add - Add Sub Commands

Assuming we're going to have below commands in our app:

  • app serve

  • app config

  • app config create

Try it out

References:

Last updated

Was this helpful?