Skip to content

cobra-gen is a boilerplate code generator for the Cobra CLI framework in Go.

License

Notifications You must be signed in to change notification settings

umutbasal/cobra-gen

Repository files navigation

cobra-gen

cobra-gen is a boilerplate code generator for the Cobra CLI framework in Go.

Dependencies

  • Go
  • gofmt
  • goimports: Install with go install golang.org/x/tools/cmd/goimports@latest

Installation

To install cobra-gen, run:

go install github.com/umutbasal/cobra-gen@latest

Usage

Example: Designing a Simple AWS CLI

Let's create a basic AWS CLI with cobra-gen that handles s3 and ec2 commands:

  • aws --profile
  • aws s3 ls mybucket --page-size 10
  • aws ec2 create-instance
  1. Generate Config

    Convert your commands to the following format and run them in your Go project:

    cobra-gen --profile
    cobra-gen s3 ls +mybucket --page-size
    cobra-gen ec2 create-instance

    This will generate a YAML configuration file named .cobra-gen.yaml. You can modify this file as needed:

    cmd:
    - --profile
    - s3:
      - ls:
        - +mybucket # `+` denotes arguments
        - --page-size # `--` denotes flags; do not add values for flags
    - ec2:
      - create-instance
  2. Generate Code

    Run cobra-gen without any arguments to generate the boilerplate code:

    cobra-gen

    This will create the following files:

    • ./cmd/cmd.go
    • ./cmd/s3/ls.go
    • ./cmd/s3/s3.go
    • ./cmd/ec2/create-instance.go
    • ./cmd/ec2/ec2.go

See the examples/aws directory for the generated code.

License

This project is licensed under the MIT License. See the LICENSE file for details.