cli

package
v0.1.0-alpha.3 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

CLI - Command-Line Interface Utilities

The cli package provides foundational components for Kure's CLI tools (kure and kurel). It implements the Factory pattern for dependency injection, I/O stream abstraction, and output formatting.

Overview

This package is used internally by the pkg/cmd/ packages to build CLI commands. It provides a clean separation between command logic and I/O handling.

Key Components

Factory

Dependency injection container for CLI commands. Provides access to global options, I/O streams, and configuration.

import "github.com/go-kure/kure/pkg/cli"

factory := cli.NewFactory(globalOpts)
streams := factory.IOStreams()
IOStreams

Standard I/O stream abstraction for testable CLI commands:

// Default streams (stdin/stdout/stderr)
streams := cli.NewIOStreams()

// Use in commands
fmt.Fprintln(streams.Out, "output")
fmt.Fprintln(streams.ErrOut, "error")

Fields:

  • In io.Reader - Standard input
  • Out io.Writer - Standard output
  • ErrOut io.Writer - Standard error
Printer

Output formatting for CLI commands with support for text, YAML, and JSON formats:

printer := cli.NewPrinter(cli.PrintOptions{
    Format: "yaml",
})
Config

Configuration file handling for CLI tools:

config, err := cli.LoadConfig("~/.kure/config.yaml")
  • pkg/cmd/kure - kure CLI command implementation
  • pkg/cmd/kurel - kurel CLI command implementation
  • io - Resource printing and serialization

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:

  1. Default values
  2. Configuration file
  3. Environment variables (KURE_*)
  4. 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 GetConfigPath

func GetConfigPath() string

GetConfigPath returns the default config file path

func PrintObjects

func PrintObjects(objects []runtime.Object, globalOpts *options.GlobalOptions, writer io.Writer) error

PrintObjects is a convenience function for printing objects

func SaveConfig

func SaveConfig(config *Config, configFile string) error

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

func LoadConfig(configFile string) (*Config, error)

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

type IOStreams struct {
	In     io.Reader
	Out    io.Writer
	ErrOut io.Writer
}

IOStreams represents the standard input, output, and error streams

func NewIOStreams

func NewIOStreams() IOStreams

NewIOStreams creates IOStreams with standard streams

type Printer

type Printer interface {
	Print(objects []runtime.Object, writer io.Writer) error
}

Printer interface for outputting resources

func NewPrinter

func NewPrinter(globalOpts *options.GlobalOptions) Printer

NewPrinter creates a new printer based on global options

type PrinterOptions

type PrinterOptions struct {
	OutputFormat string
	NoHeaders    bool
	ShowLabels   bool
	Wide         bool
}

PrinterOptions contains options for printing

Jump to

Keyboard shortcuts

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