cobra-gen is a boilerplate code generator for the Cobra CLI framework in Go.
- Go
- gofmt
- goimports: Install with
go install golang.org/x/tools/cmd/goimports@latest
To install cobra-gen, run:
go install github.com/umutbasal/cobra-gen@latestLet's create a basic AWS CLI with cobra-gen that handles s3 and ec2 commands:
aws --profileaws s3 ls mybucket --page-size 10aws ec2 create-instance
-
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
-
Generate Code
Run
cobra-genwithout 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.
This project is licensed under the MIT License. See the LICENSE file for details.