common

package
v0.0.21-beta Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package common provides shared utilities and types for CLI command handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExitOnShow added in v0.0.13

func ExitOnShow(show root.ShowFuncType, args ...string) bool

ExitOnShow checks if the command should exit early based on the show function and arguments. Returns true if the command should exit without further processing. ExitOnShow determines if the command should exit early when show function is active and no arguments are provided.

func KCreateSubcommandPreRunE added in v0.0.13

func KCreateSubcommandPreRunE(
	cmd *cobra.Command,
	cfg Trackable,
	show root.ShowFuncType,
) func(_ *cobra.Command, _ []string) error

KCreateSubcommandPreRunE creates a PreRunE function for Cobra commands that handles configuration loading, environment variable processing, flag parsing, and validation.

func SetSubcommandDefaults added in v0.0.13

func SetSubcommandDefaults(cmd *cobra.Command, local any, show root.ShowFuncType)

SetSubcommandDefaults configures default behavior for a subcommand, including setting up the PersistentPreRunE function with the provided configuration and show function. SetSubcommandDefaults configures default settings for subcommands including pre-run hooks and configuration handling.

func SetupLogger added in v0.0.13

func SetupLogger(level string) (*logger.Logger, error)

SetupLogger creates a new logger instance with the specified log level.

Types

type CommandPath added in v0.0.13

type CommandPath []string

CommandPath represents a hierarchical command path as a slice of strings, used for building configuration sections and environment variable prefixes.

func BuildCommandPath added in v0.0.13

func BuildCommandPath(cmd *cobra.Command) CommandPath

BuildCommandPath constructs a CommandPath from a Cobra command's full path.

func (CommandPath) Env added in v0.0.13

func (c CommandPath) Env() iutils.Prefix

Env returns the environment variable prefix for the command path, using underscore notation and uppercase letters.

func (CommandPath) Section added in v0.0.13

func (c CommandPath) Section() iutils.Prefix

Section returns the configuration section prefix for the command path, excluding the root command and using dot notation.

func (CommandPath) WithoutRoot added in v0.0.13

func (c CommandPath) WithoutRoot() CommandPath

WithoutRoot returns the command path excluding the root command.

type Context added in v0.0.13

type Context struct {
	// Config holds a map of the parsed configuration file.
	Config *koanfx.Koanf
	// DotEnv holds the parsed .env files
	DotEnv *env.Env
	// Env holds the parsed environment variables.
	Env *env.Env
}

Context holds the parsed configuration.

var GlobalContext Context

GlobalContext is a global variable, allowing the root parsed configuration to be accessed from anywhere in the application.

type Embedded added in v0.0.13

type Embedded struct {
	// Defaults to be set for each tool (not flags).
	Defaults []byte
	// Default list of tools that can be used to either view or dump out.
	Tools []byte
	// A template for the cleanup script.
	Template []byte
}

Embedded holds the embedded files for the application.

func NewEmbeddedFiles added in v0.0.13

func NewEmbeddedFiles(embeds embed.FS) (*Embedded, error)

NewEmbeddedFiles loads embedded configuration files and templates.

type Handler added in v0.0.13

type Handler struct {
	// contains filtered or unexported fields
}

Handler manages the common operations for CLI commands including configuration resolution, logging, and embedded resource management. Handler manages configuration resolution, logging, and embedded resources for CLI operations.

func NewHandler added in v0.0.13

func NewHandler(cfg root.Config, embedded Embedded) *Handler

NewHandler creates a new Handler instance with the provided configuration and embedded resources. NewHandler creates a new Handler instance with the provided configuration and embedded resources.

func (*Handler) Logger added in v0.0.13

func (c *Handler) Logger() *logger.Logger

Logger returns the logger instance associated with the handler.

func (*Handler) Resolve added in v0.0.13

func (c *Handler) Resolve(defaultFile file.File, tools *tools.Tools) (err error)

Resolve loads and processes the configuration for tools, including defaults, inheritance, and platform-specific settings. Resolve processes the configuration hierarchy by loading defaults, applying inheritance, and merging platform-specific settings for all tools.

func (*Handler) SetupLogger added in v0.0.13

func (c *Handler) SetupLogger(level string) (err error)

SetupLogger creates and configures a logger with the specified log level.

type Input added in v0.0.13

type Input struct {
	// Global contains the global configuration.
	Global *root.Config
	// Embedded contains the embedded configuration.
	Embedded *Embedded
	// Cmd is the current command being executed.
	Cmd *cobra.Command
	// Args are the arguments passed to the command.
	Args []string
}

Input is the input structure for the CLI commands.

func (Input) Unpack added in v0.0.13

func (i Input) Unpack() (*root.Config, *Embedded, *Context, *cobra.Command, []string)

Unpack is a convenience method to unpack the Input structure into its components.

type Trackable added in v0.0.13

type Trackable interface {
	Store(tracker *koanfx.Tracker)
	Validate() error
}

Trackable defines the interface for configuration objects that can track and validate their state.

Jump to

Keyboard shortcuts

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