builder

package
v0.0.0-...-0bc6d30 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2025 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildRequestBody

func BuildRequestBody(cmd *cobra.Command) (map[string]interface{}, error)

BuildRequestBody builds request body from flags.

func BuildRequestParams

func BuildRequestParams(cmd *cobra.Command) (map[string]interface{}, error)

BuildRequestParams builds request parameters from flags.

func GetFlagValue

func GetFlagValue(flags *pflag.FlagSet, name string) (interface{}, error)

GetFlagValue retrieves a flag value with type conversion.

func ValidateEnumFlags

func ValidateEnumFlags(cmd *cobra.Command) error

ValidateEnumFlags validates enum flag values.

func ValidateRequiredFlags

func ValidateRequiredFlags(cmd *cobra.Command) error

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.

func (*Builder) Build

func (b *Builder) Build() (*cobra.Command, error)

Build builds the complete command tree from the OpenAPI spec.

func (*Builder) GetCommandByOperationID

func (b *Builder) GetCommandByOperationID(rootCmd *cobra.Command, operationID string) *cobra.Command

GetCommandByOperationID retrieves a command by its operation ID.

func (*Builder) GetCommandByPath

func (b *Builder) GetCommandByPath(path string) (*cobra.Command, bool)

GetCommandByPath retrieves a command by its path.

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

func (fb *FlagBuilder) AddOperationFlags(cmd *cobra.Command, op *openapi.Operation) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL