Install the cobra-cli generator
mainInstall the Cobra generator using go install. Ensure your $GOPATH/bin is in your $PATH to use the cobra-cli command directly.
go install github.com/spf13/cobra-cli@latestrepository·main·Indexed 21 days ago
https://github.com/spf13/cobra-cliA scaffolding tool that automates the creation of Go applications using the Cobra CLI library. It handles project structure, command generation, and integration with Viper for configuration management. Key features include the `init` command for project initialization and the `add` command for creating subcommands with support for custom licenses and parent-child command relationships.
Install the Cobra generator using go install. Ensure your $GOPATH/bin is in your $PATH to use the cobra-cli command directly.
go install github.com/spf13/cobra-cli@latestUse cobra-cli add <command> to add new commands to your existing application.
Important Rules:
camelCase for command names (e.g., addUser, not add-user). The generator appends Cmd to the name for the internal variable.-p flag to assign a command to a specific parent command. If omitted, the command defaults to rootCmd.cobra-cli add supports the same optional flags as init (--author, --license, --viper).Example of creating a nested command structure (app config create):
cobra-cli add serve
cobra-cli add config
cobra-cli add create -p 'configCmd'The cobra-cli init command creates the initial application structure. The generator works from within a Go module. If you haven't initialized a module yet, you must run go mod init <MODNAME> first.
To initialize a project:
go mod init <MODNAME>.cobra-cli init.After initialization, you can run your application using go run main.go. You should edit cmd/root.go to define your application's base logic and description.
cd $HOME/code
mkdir myapp
cd myapp
go mod init github.com/spf13/myapp
cobra-cli init
go run main.goTo avoid repeating flags, you can create a ~/.cobra.yaml configuration file. This file supports setting default values for author, license, and useViper.
Supported Licenses:
GPLv2, GPLv3, LGPL, AGPL, MIT, 2-Clause BSD, or 3-Clause BSD.
You can also define a custom license using header and text keys. The text field supports interpolation for {{ .copyright }} (derived from author and year) and {{ .year }}.
author: Steve Francia <spf@spf13.com>
license: MIT
useViper: trueWhen using cobra-cli to generate a project, you can specify which software license should be applied to the generated files. The tool supports built-in license types, custom license text/headers via configuration, or no license at all.
The tool resolves the license in the following order:
license.header or license.text are set in your configuration (e.g., via Viper), these are used to create a custom license.license key is set in your configuration, the tool attempts to match it against built-in licenses.none.The following license types are available for selection:
apache2 (Apache License, Version 2.0)mit (MIT License)bsdClause3 (BSD 3-Clause License)bsdClause2 (BSD 2-Clause License)gpl2 (GNU General Public License v2.0)gpl3 (GNU General Public License v3.0)lgpl (GNU Lesser General Public License)agpl (GNU Affero General Public License)none (No license applied)If built-in licenses are insufficient, use the license object in your configuration to define a custom header and body. The header is used for file headers (no interpolation), and text allows for copyright interpolation.
Example configuration:
author: Steve Francia <spf@spf13.com>
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.author: Steve Francia <spf@spf13.com>
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.By default, cobra-cli looks for a configuration file named .cobra.yaml in your home directory. You can specify a custom configuration file using the --config flag. The tool uses Viper to manage these settings, and environment variables are automatically supported via viper.AutomaticEnv().
# Use a custom config file
cobra-cli --config ./my-config.yaml initIf you want to use a license that is not among the built-in options, you can define a custom license by setting the license.header and license.text keys in your configuration file.
license.header: The specific comment block to be prepended to each generated source file.license.text: The full text of the license agreement.Additionally, the copyright line is generated using the author and year configuration keys. If year is not provided, the current year is used automatically.
When running cobra-cli init, you can use the following flags to configure the generated project:
--author <name>: Specifies the author name and email.--license <license>: Specifies a license (e.g., apache, mit, gplv3).--viper: Automatically sets up Viper for handling environment variables and configuration files.cobra-cli init --author "Steve Francia spf@spf13.com" --license apache --viperOnce a project is initialized, you can add subcommands by calling Command.Create(). This method generates a new Go file in the cmd/ directory named after the command (e.g., cmd/mycommand.go) using the tpl.AddCommandTemplate().
func (c *Command) Create() errorThe Project.Create() method performs the initial scaffolding for a new Cobra application. It performs the following actions at the AbsolutePath:
main.go file using the tpl.MainTemplate().cmd/ directory.cmd/root.go file using the tpl.RootTemplate().LICENSE file based on the Legal field provided in the Project struct.func (p *Project) Create() errorThe add command supports the following flags to configure how the new command is generated and where it is registered.
-t, --package string target package name (e.g. github.com/spf13/hugo) [DEPRECATED]
-p, --parent string variable name of parent command for this command (default "rootCmd")