cobra-cli

repository·main·Indexed 21 days ago

https://github.com/spf13/cobra-cli

A 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.

Tokens
3.7K
Snippets
19
Records
21
Agent score
75%

What's inside cobra-cli

  1. Add commands to a Cobra project

    main

    Use cobra-cli add <command> to add new commands to your existing application.

    Important Rules:

    • Naming: Use camelCase for command names (e.g., addUser, not add-user). The generator appends Cmd to the name for the internal variable.
    • Parenting: Use the -p flag to assign a command to a specific parent command. If omitted, the command defaults to rootCmd.
    • Flags: 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'
  2. Initialize a new Cobra CLI application

    main

    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:

    1. Create and enter a new directory.
    2. Run go mod init <MODNAME>.
    3. Run 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.go
  3. Configure the cobra generator via ~/.cobra.yaml

    main

    To 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: true
  4. Configure the license for generated projects

    main

    When 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.

    Selection Priority

    The tool resolves the license in the following order:

    1. Command-line flag: If a license is explicitly provided via the CLI flag, it takes precedence.
    2. Custom Configuration: If license.header or license.text are set in your configuration (e.g., via Viper), these are used to create a custom license.
    3. Configured License: If the license key is set in your configuration, the tool attempts to match it against built-in licenses.
    4. Default: If no license is specified, it defaults to none.

    Built-in Licenses

    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)
  5. Define a custom license in cobra-cli configuration

    main

    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.
  6. Configure cobra-cli via YAML

    main

    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 init
  7. Customizing license headers and text via configuration

    main

    If 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.

  8. Use flags with cobra-cli init

    main

    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 --viper
  9. Add a new command with Command.Create()

    main

    Once 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() error
  10. Initialize a new project with Project.Create()

    main

    The Project.Create() method performs the initial scaffolding for a new Cobra application. It performs the following actions at the AbsolutePath:

    1. Creates the project directory if it does not exist.
    2. Generates a main.go file using the tpl.MainTemplate().
    3. Creates a cmd/ directory.
    4. Generates a cmd/root.go file using the tpl.RootTemplate().
    5. Generates a LICENSE file based on the Legal field provided in the Project struct.
    func (p *Project) Create() error
  11. Reference: cobra-cli add flags

    main

    The 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")