Documentation
¶
Overview ¶
Package cli provides shared utilities and abstractions for building command-line interfaces in the Kure and kurel tools.
Overview ¶
The cli package provides the foundational components for CLI commands:
- Factory: Dependency injection container for commands
- IOStreams: Abstraction for stdin/stdout/stderr
- Printer: Output formatting for various output modes
- Config: Configuration file handling
Factory Pattern ¶
The Factory interface provides dependency injection for CLI commands, making them easier to test and configure:
factory := cli.NewFactory(globalOpts)
cmd := &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
streams := factory.IOStreams()
opts := factory.GlobalOptions()
fmt.Fprintf(streams.Out, "Running with verbose=%v\n", opts.Verbose)
return nil
},
}
IOStreams ¶
IOStreams abstracts the standard I/O streams, enabling testable commands:
// Production usage
streams := cli.NewIOStreams() // Uses os.Stdin/Stdout/Stderr
// Test usage
var buf bytes.Buffer
streams := cli.IOStreams{
In: strings.NewReader("input"),
Out: &buf,
ErrOut: &buf,
}
Output Formatting ¶
The Printer type supports multiple output formats compatible with kubectl conventions:
printer := cli.NewPrinter(cli.PrintOptions{
Format: cli.OutputFormatYAML,
NoHeaders: false,
})
err := printer.Print(obj, streams.Out)
Supported formats:
- YAML (default)
- JSON
- Table (wide and narrow)
- Name (resource name only)
Configuration ¶
The Config type handles configuration file loading and merging:
cfg, err := cli.LoadConfig("~/.kure/config.yaml")
if err != nil {
// handle error
}
Configuration is merged from multiple sources:
- Default values
- Configuration file
- Environment variables (KURE_*)
- Command-line flags
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EnsureConfigDir ¶
func EnsureConfigDir() error
EnsureConfigDir ensures the config directory exists
func PrintObjects ¶
func PrintObjects(objects []runtime.Object, globalOpts *options.GlobalOptions, writer io.Writer) error
PrintObjects is a convenience function for printing objects
func SaveConfig ¶
SaveConfig saves configuration to file
Types ¶
type Config ¶
type Config struct {
// Default settings
Defaults struct {
Output string `yaml:"output"`
Namespace string `yaml:"namespace"`
Verbose bool `yaml:"verbose"`
Debug bool `yaml:"debug"`
} `yaml:"defaults"`
// Layout settings
Layout struct {
ManifestsDir string `yaml:"manifestsDir"`
BundleGrouping string `yaml:"bundleGrouping"`
ApplicationGrouping string `yaml:"applicationGrouping"`
FluxPlacement string `yaml:"fluxPlacement"`
} `yaml:"layout"`
// GitOps settings
GitOps struct {
Type string `yaml:"type"`
Repository string `yaml:"repository"`
Branch string `yaml:"branch"`
Path string `yaml:"path"`
} `yaml:"gitops"`
}
Config represents the kure configuration structure
func LoadConfig ¶
LoadConfig loads configuration from file
func NewDefaultConfig ¶
func NewDefaultConfig() *Config
NewDefaultConfig returns a config with default values
type Factory ¶
type Factory interface {
// Configuration
GlobalOptions() *options.GlobalOptions
// IO
IOStreams() IOStreams
// Validation
Validate() error
}
Factory provides access to common dependencies and configuration
func NewFactory ¶
func NewFactory(globalOpts *options.GlobalOptions) Factory
NewFactory creates a new Factory implementation
type IOStreams ¶
IOStreams represents the standard input, output, and error streams
func NewIOStreams ¶
func NewIOStreams() IOStreams
NewIOStreams creates IOStreams with standard streams
type Printer ¶
Printer interface for outputting resources
func NewPrinter ¶
func NewPrinter(globalOpts *options.GlobalOptions) Printer
NewPrinter creates a new printer based on global options