core

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExitOnShow

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

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

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

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

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

Types

type CommandPath

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

func BuildCommandPath(cmd *cobra.Command) CommandPath

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

func (CommandPath) Env

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

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

func (c CommandPath) WithoutRoot() CommandPath

WithoutRoot returns the command path excluding the root command.

type Context

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

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

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

NewEmbeddedFiles loads embedded configuration files and templates.

type Handler

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

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

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

Logger returns the logger instance associated with the handler.

func (*Handler) Resolve

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

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

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

type Input

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

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

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