profile

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package profile provides command profile functionality for executing commands with optional source filtering using CEL expressions.

Profiles define how to execute commands against sets of files, with support for hooks (pre/post execution commands) and source filtering to determine which files should be processed.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoCommandForPath is returned when no command is found for a path.
	ErrNoCommandForPath = errors.New("no command for path")

	// ErrCommandExecution is returned when command execution fails.
	ErrCommandExecution = errors.New("command execution")

	// ErrEmptyCommand is returned when a command is empty.
	ErrEmptyCommand = errors.New("empty command")

	// ErrHookExecution is returned when hook execution fails.
	ErrHookExecution = errors.New("hook execution")
)

Functions

This section is empty.

Types

type CallerRef added in v0.14.0

type CallerRef struct {
	Pattern string `yaml:"pattern,omitempty"`
	Name    string `yaml:"name,omitempty"`
	// contains filtered or unexported fields
}

CallerRef represents a reference to environment variables from the caller process.

func (*CallerRef) Compile added in v0.14.0

func (c *CallerRef) Compile() error

Compile compiles the caller reference pattern into a regex if a pattern is provided.

type EnvFromSource added in v0.14.0

type EnvFromSource struct {
	CallerRef *CallerRef `yaml:"callerRef,omitempty"`
}

EnvFromSource represents a source for inheriting environment variables.

type EnvVar added in v0.14.0

type EnvVar struct {
	ValueFrom *EnvVarSource `yaml:"valueFrom,omitempty"`
	Name      string        `validate:"required"        yaml:"name"`
	Value     string        `yaml:"value,omitempty"`
}

EnvVar represents an environment variable definition.

type EnvVarSource added in v0.14.0

type EnvVarSource struct {
	CallerRef *CallerRef `yaml:"callerRef,omitempty"`
}

EnvVarSource represents a source for an environment variable value.

type Environment added in v0.14.0

type Environment struct {
	Env     []EnvVar        `yaml:"env,omitempty"`
	EnvFrom []EnvFromSource `validate:"dive"      yaml:"envFrom,omitempty"`
	// contains filtered or unexported fields
}

Environment manages environment variables for command execution.

func NewEnvironment added in v0.14.0

func NewEnvironment(baseEnv []string) Environment

NewEnvironment creates a new Environment. It accepts a base environment, which usually will be from os.Environ.

func (*Environment) AddEnvFrom added in v0.14.0

func (e *Environment) AddEnvFrom(envFrom []EnvFromSource)

AddEnvFrom adds environment variable sources.

func (*Environment) AddEnvVar added in v0.14.0

func (e *Environment) AddEnvVar(envVar EnvVar)

AddEnvVar adds a single environment variable.

func (*Environment) Build added in v0.14.0

func (e *Environment) Build() []string

Build constructs the environment variables for command execution.

func (*Environment) CompilePatterns added in v0.14.0

func (e *Environment) CompilePatterns() error

CompilePatterns compiles all regex patterns.

func (*Environment) SetBaseEnv added in v0.14.0

func (e *Environment) SetBaseEnv(baseEnv []string)

type ExecResult

type ExecResult struct {
	Error  error
	Stdout string
	Stderr string
}

ExecResult represents the result of executing a profile.

type HookCommand

type HookCommand struct {
	Environment Environment `yaml:",inline"`
	Command     string      `validate:"required,alphanum" yaml:"command"`
	Args        []string    `yaml:"args,flow"`
}

HookCommand represents a single hook command to execute.

func MustNewHookCommand added in v0.14.0

func MustNewHookCommand(command string, opts ...HookCommandOpt) *HookCommand

MustNewHookCommand creates a new hook command and panics if there's an error.

func NewHookCommand

func NewHookCommand(command string, opts ...HookCommandOpt) (*HookCommand, error)

NewHookCommand creates a new hook command with the given command and options.

func (*HookCommand) Build added in v0.14.0

func (hc *HookCommand) Build() error

func (*HookCommand) Exec

func (hc *HookCommand) Exec(ctx context.Context, dir string, stdin []byte) error

Exec executes the hook command in the given directory with optional stdin.

type HookCommandOpt added in v0.14.0

type HookCommandOpt func(*HookCommand)

HookCommandOpt is a functional option for configuring a HookCommand.

func WithHookArgs added in v0.14.0

func WithHookArgs(args ...string) HookCommandOpt

WithHookArgs sets the command arguments for the hook command.

func WithHookEnvFrom added in v0.14.0

func WithHookEnvFrom(envFrom []EnvFromSource) HookCommandOpt

WithHookEnvFrom sets the envFrom sources for the hook command.

func WithHookEnvVar added in v0.14.0

func WithHookEnvVar(envVar EnvVar) HookCommandOpt

WithHookEnvVar sets a single environment variable for the hook command.

type HookOpts

type HookOpts func(*Hooks)

HookOpts is a functional option for configuring Hooks.

func WithInit

func WithInit(hooks ...*HookCommand) HookOpts

WithInit adds init hooks.

func WithPostRender

func WithPostRender(hooks ...*HookCommand) HookOpts

WithPostRender adds post-render hooks.

func WithPreRender

func WithPreRender(hooks ...*HookCommand) HookOpts

WithPreRender adds pre-render hooks.

type Hooks

type Hooks struct {
	Init       []*HookCommand `yaml:"init,omitempty"`
	PreRender  []*HookCommand `yaml:"preRender,omitempty"`
	PostRender []*HookCommand `yaml:"postRender,omitempty"`
}

Hooks represents the different types of hooks that can be executed.

func MustNewHooks added in v0.14.0

func MustNewHooks(opts ...HookOpts) *Hooks

MustNewHooks creates a new Hooks instance and panics if there's an error.

func NewHooks

func NewHooks(opts ...HookOpts) (*Hooks, error)

NewHooks creates a new Hooks instance with the given options.

func (*Hooks) Build added in v0.14.0

func (h *Hooks) Build() error

type Plugin

type Plugin struct {
	Environment Environment `yaml:",inline"`
	Description string      `yaml:"description"`
	Keys        []keys.Key  `yaml:"keys,omitempty"`
	Command     string      `validate:"required,alphanum" yaml:"command"`
	Args        []string    `yaml:"args,flow"`
}

Plugin represents a command plugin that can be executed on demand with keybinds.

func MustNewPlugin added in v0.14.0

func MustNewPlugin(command, description string, opts ...PluginOpt) *Plugin

MustNewPlugin creates a new plugin and panics if there's an error.

func NewPlugin

func NewPlugin(command, description string, opts ...PluginOpt) (*Plugin, error)

NewPlugin creates a new plugin with the given command and options.

func (*Plugin) Build added in v0.14.0

func (p *Plugin) Build() error

func (*Plugin) Exec

func (p *Plugin) Exec(ctx context.Context, dir string) ExecResult

Exec executes the plugin command in the specified directory.

func (*Plugin) MatchKeys

func (p *Plugin) MatchKeys(keyCode string) bool

MatchKeys checks if any of the plugin's keys match the given key code.

type PluginOpt

type PluginOpt func(*Plugin)

PluginOpt is a functional option for configuring a Plugin.

func WithPluginArgs

func WithPluginArgs(a ...string) PluginOpt

WithPluginArgs sets the command arguments for the plugin.

func WithPluginEnvFrom added in v0.14.0

func WithPluginEnvFrom(envFrom []EnvFromSource) PluginOpt

WithPluginEnvFrom sets the envFrom sources for the plugin.

func WithPluginEnvVar added in v0.14.0

func WithPluginEnvVar(envVar EnvVar) PluginOpt

WithPluginEnvVar sets a single environment variable for the plugin.

func WithPluginKeys

func WithPluginKeys(k ...keys.Key) PluginOpt

WithPluginKeys sets the keybinds for the plugin.

type Profile

type Profile struct {
	Environment Environment        `yaml:",inline"`
	Hooks       *Hooks             `yaml:"hooks,omitempty"`
	UI          *UIConfig          `yaml:"ui,omitempty"` // UI configuration for the profile.
	Plugins     map[string]*Plugin `yaml:"plugins,omitempty"`
	Source      string             `yaml:"source,omitempty"`
	Command     string             `validate:"required,alphanum" yaml:"command"`
	Args        []string           `yaml:"args,flow"`
	// contains filtered or unexported fields
}

Profile represents a command profile with optional source filtering.

The Source field contains a CEL expression that determines which files should be processed by this profile. The expression has access to:

  • `files` (list<string>): All file paths in directory
  • `dir` (string): The directory path being processed

Source CEL expressions must return a list of files:

  • `files.filter(f, pathExt(f) in [".yaml", ".yml"])` - returns all YAML files
  • `files.filter(f, pathBase(f) in ["Chart.yaml", "values.yaml"])` - returns Chart and values files
  • `files.filter(f, pathBase(f) == "Chart.yaml")` - returns files named Chart.yaml
  • `files.filter(f, !pathBase(f).matches(".*test.*")) - returns non-test files
  • `files.filter(f, pathBase(f) == "Chart.yaml" && yamlPath(f, "$.apiVersion") == "v2")` - returns charts with apiVersion v2
  • `files` - unfiltered list means all files should be processed
  • `[]` - empty list means no files should be processed

If no Source expression is provided, the profile will use default file filtering.

func MustNew

func MustNew(command string, opts ...ProfileOpt) *Profile

MustNew creates a new profile and panics if there's an error.

func New

func New(command string, opts ...ProfileOpt) (*Profile, error)

New creates a new profile with the given command and options.

func (*Profile) Build added in v0.14.0

func (p *Profile) Build() error

func (*Profile) CompileSource

func (p *Profile) CompileSource() error

CompileSource compiles the profile's source expression into a CEL program.

func (*Profile) Exec

func (p *Profile) Exec(ctx context.Context, dir string) ExecResult

Exec runs the profile in the specified directory. Returns ExecResult with the command output and any post-render hooks.

func (*Profile) GetPlugin

func (p *Profile) GetPlugin(name string) *Plugin

setEnvFrom applies all envFrom sources to the environment map. GetPlugin returns the plugin with the given name, or nil if not found.

func (*Profile) GetPluginKeyBinds

func (p *Profile) GetPluginKeyBinds() []*keys.KeyBind

func (*Profile) GetPluginNameByKey

func (p *Profile) GetPluginNameByKey(keyCode string) string

GetPluginNameByKey returns the name of the first plugin that matches the given key code.

func (*Profile) MatchFiles

func (p *Profile) MatchFiles(dirPath string, files []string) (bool, []string)

MatchFiles checks if the profile's source expression matches files in a directory. The CEL expression must return a list of strings representing files. An empty list means no files should be processed with this profile.

Returns (matches, files) where: - matches: true if the profile should be used (non-empty file list) - files: specific files that were matched.

type ProfileOpt

type ProfileOpt func(*Profile)

ProfileOpt is a functional option for configuring a Profile.

func WithArgs

func WithArgs(args ...string) ProfileOpt

WithArgs sets the command arguments for the profile.

func WithEnvFrom added in v0.14.0

func WithEnvFrom(envFrom []EnvFromSource) ProfileOpt

WithEnvFrom sets the envFrom sources for the profile.

func WithEnvVar added in v0.14.0

func WithEnvVar(envVar EnvVar) ProfileOpt

WithEnvVar sets a single environment variable for the profile. Call multiple times to set multiple environment variables.

func WithHooks

func WithHooks(hooks *Hooks) ProfileOpt

WithHooks sets the hooks for the profile.

func WithPlugins

func WithPlugins(plugins map[string]*Plugin) ProfileOpt

WithPlugins sets the plugins for the profile.

func WithSource

func WithSource(source string) ProfileOpt

WithSource sets the source filtering expression for the profile.

type UIConfig

type UIConfig struct {
	Compact     *bool  `yaml:"compact"`
	WordWrap    *bool  `yaml:"wordWrap"`
	LineNumbers *bool  `yaml:"lineNumbers"`
	Theme       string `yaml:"theme"`
}

Jump to

Keyboard shortcuts

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