Documentation
¶
Overview ¶
Package builder converts OpenAPI specs to Cobra command trees.
The builder package generates Cobra command hierarchies from OpenAPI specifications, applying CLI extensions to customize command structure, names, flags, and behavior. It supports multiple command organization strategies including tag-based and path-based grouping.
Command Organization ¶
- Tag-based: Group commands by OpenAPI tags (recommended)
- Path-based: Build hierarchy from URL path structure
- Flat: All commands at root level (simple APIs)
Example Usage ¶
// Create builder
config := &builder.BuilderConfig{
RootName: "mycli",
GroupByTags: true,
}
b := builder.NewBuilder(spec, config)
// Build command tree
rootCmd, _ := b.Build()
// Execute
rootCmd.Execute()
Tag-Based Grouping ¶
OpenAPI operations with the same tag are grouped together:
paths:
/users:
get:
tags: [users]
/users/{id}:
get:
tags: [users]
Generates:
mycli users list mycli users get <id>
Command Customization ¶
Use x-cli-command to override generated command names:
paths:
/users:
get:
operationId: listUsers
x-cli-command: list
x-cli-aliases: [ls]
Path-Based Hierarchy ¶
Path structure determines command nesting:
/api/v1/clusters/{id}/nodes -> mycli api v1 clusters nodes
The builder automatically extracts path parameters, generates flags from operation parameters, and wires up execution handlers.
Package builder provides nested resource handling for OpenAPI-based CLI commands.
This file implements support for the x-cli-parent-resource extension, which enables hierarchical resource relationships in CLI commands. When an operation specifies a parent resource, the builder automatically adds required flags and handles path parameter substitution.
Example ¶
Given an OpenAPI operation:
paths:
/api/v1/clusters/{cluster_id}/machine_pools:
post:
operationId: createMachinePool
x-cli-parent-resource: "cluster"
The builder generates:
mycli create machinepool --cluster my-cluster --name pool1
The --cluster flag is automatically added, marked as required, and used to substitute {cluster_id} in the path before making the API request.
Index ¶
- func BuildRequestBody(cmd *cobra.Command) (map[string]interface{}, error)
- func BuildRequestParams(cmd *cobra.Command) (map[string]interface{}, error)
- func GetFlagValue(flags *pflag.FlagSet, name string) (interface{}, error)
- func ValidateEnumFlags(cmd *cobra.Command) error
- func ValidateRequiredFlags(cmd *cobra.Command) error
- type Builder
- type BuilderConfig
- type FlagBuilder
- type GlobalFlags
- type NestedResourceHandler
- func (h *NestedResourceHandler) AddParentResourceFlag(cmd *cobra.Command, op *openapi.Operation) error
- func (h *NestedResourceHandler) GetNestedResourceLevel(path string) int
- func (h *NestedResourceHandler) SubstituteParentResource(cmd *cobra.Command, path string) (string, error)
- func (h *NestedResourceHandler) ValidateNestedResourceFlags(cmd *cobra.Command) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildRequestBody ¶
BuildRequestBody builds request body from flags.
func BuildRequestParams ¶
BuildRequestParams builds request parameters from flags.
func GetFlagValue ¶
GetFlagValue retrieves a flag value with type conversion.
func ValidateEnumFlags ¶
ValidateEnumFlags validates enum flag values.
func ValidateRequiredFlags ¶
ValidateRequiredFlags validates that all required flags are set.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder builds Cobra commands from OpenAPI specifications.
func NewBuilder ¶
func NewBuilder(spec *openapi.ParsedSpec, config *BuilderConfig) *Builder
NewBuilder creates a new command builder.
type BuilderConfig ¶
type BuilderConfig struct {
// RootName is the name of the root command
RootName string
// RootDescription is the description of the root command
RootDescription string
// GroupByTags groups commands by OpenAPI tags
GroupByTags bool
// FlattenSingleOperations removes unnecessary nesting
FlattenSingleOperations bool
// DefaultExecutor is the default command executor function
DefaultExecutor func(cmd *cobra.Command, args []string) error
}
BuilderConfig configures command building behavior.
func DefaultBuilderConfig ¶
func DefaultBuilderConfig() *BuilderConfig
DefaultBuilderConfig returns default builder configuration.
type FlagBuilder ¶
type FlagBuilder struct {
// contains filtered or unexported fields
}
FlagBuilder builds flags for Cobra commands from OpenAPI parameters and schemas.
func NewFlagBuilder ¶
func NewFlagBuilder(config *openapi.CLIConfig) *FlagBuilder
NewFlagBuilder creates a new flag builder.
func (*FlagBuilder) AddGlobalFlags ¶
func (fb *FlagBuilder) AddGlobalFlags(cmd *cobra.Command)
AddGlobalFlags adds global flags to a command.
func (*FlagBuilder) AddOperationFlags ¶
AddOperationFlags adds flags for an operation to a command.
type GlobalFlags ¶
type GlobalFlags struct {
Output string
Verbose bool
NoColor bool
ConfigFile string
Profile string
DryRun bool
Debug bool
Interactive bool
}
GlobalFlags contains global flags from config.
type NestedResourceHandler ¶
type NestedResourceHandler struct {
// contains filtered or unexported fields
}
NestedResourceHandler manages nested resource relationships and path parameters.
func NewNestedResourceHandler ¶
func NewNestedResourceHandler() *NestedResourceHandler
NewNestedResourceHandler creates a new nested resource handler.
func (*NestedResourceHandler) AddParentResourceFlag ¶
func (h *NestedResourceHandler) AddParentResourceFlag(cmd *cobra.Command, op *openapi.Operation) error
AddParentResourceFlag adds a required flag for the parent resource ID. For example, if an operation has path "/api/v1/clusters/{cluster_id}/machine_pools" and x-cli-parent-resource: "cluster", it adds a --cluster flag.
func (*NestedResourceHandler) GetNestedResourceLevel ¶
func (h *NestedResourceHandler) GetNestedResourceLevel(path string) int
GetNestedResourceLevel determines the nesting level of a resource. For example:
- "/api/v1/clusters" -> 0 (top-level resource)
- "/api/v1/clusters/{cluster_id}/machine_pools" -> 1 (nested under cluster)
- "/api/v1/clusters/{cluster_id}/machine_pools/{machinepool_id}/nodes" -> 2 (nested under machine pool)
func (*NestedResourceHandler) SubstituteParentResource ¶
func (h *NestedResourceHandler) SubstituteParentResource(cmd *cobra.Command, path string) (string, error)
SubstituteParentResource substitutes the parent resource ID into the path template. It reads the parent resource flag value and replaces the corresponding path parameter.
func (*NestedResourceHandler) ValidateNestedResourceFlags ¶
func (h *NestedResourceHandler) ValidateNestedResourceFlags(cmd *cobra.Command) error
ValidateNestedResourceFlags validates that parent resource flags are properly set.