command

package
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package command implements both rules and profiles, providing a single API for command execution, file watching, and event handling.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoCommandForPath = errors.New("no command for path")

ErrNoCommandForPath is returned when no command is found for a path.

Functions

This section is empty.

Types

type Commander added in v0.25.0

type Commander interface {
	Run() Output
	RunContext(ctx context.Context) Output
	RunOnEvent()
	String() string
	Subscribe(ch chan<- Event)
	GetProfiles() map[string]*profile.Profile
	GetCurrentProfile() (string, *profile.Profile)
	GetPath() string
	FindProfiles(path string) ([]ProfileMatch, error)
	Configure(opts ...RunnerOpt) error
	ConfigureContext(ctx context.Context, opts ...RunnerOpt) error
	RunPlugin(name string) Output
	RunPluginContext(ctx context.Context, name string) Output
	FS() (*FilteredFS, error)
	SendEvent(evt Event)
}

type Config

type Config struct {
	// Profiles contains a map of profile names to profile configurations.
	Profiles map[string]*profile.Profile `json:"profiles,omitempty" jsonschema:"title=Profiles"`
	// Rules defines the rules for matching files to profiles.
	Rules []*rule.Rule `json:"rules,omitempty" jsonschema:"title=Rules"`
}

Config defines the core (non-UI) kat configuration.

func NewConfig

func NewConfig() *Config

NewConfig creates a new Config with default profiles and rules.

func (*Config) EnsureDefaults

func (c *Config) EnsureDefaults()

func (*Config) Merge added in v0.28.0

func (c *Config) Merge(project *Config)

Merge merges another Config into this one. Project profiles override global profiles with the same key. Project rules are prepended to global rules (evaluated first, higher priority).

func (*Config) Validate

func (c *Config) Validate() error

type Event

type Event interface {
	GetContext() context.Context
}

Event represents an event related to command execution.

type EventCancel

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

EventCancel indicates that a command execution has been canceled.

func NewEventCancel added in v0.25.0

func NewEventCancel(ctx context.Context) EventCancel

NewEventCancel creates a new EventCancel with the given context.

func (EventCancel) GetContext added in v0.25.0

func (e EventCancel) GetContext() context.Context

GetContext returns the context associated with the EventCancel.

type EventConfigure added in v0.25.0

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

EventConfigure indicates that a command has been configured (or re-configured).

func NewEventConfigure added in v0.25.0

func NewEventConfigure(ctx context.Context) EventConfigure

NewEventConfigure creates a new EventConfigure with the given context.

func (EventConfigure) GetContext added in v0.25.0

func (e EventConfigure) GetContext() context.Context

GetContext returns the context associated with the EventConfigure.

type EventEnd

type EventEnd struct {
	Output Output
	// contains filtered or unexported fields
}

EventEnd indicates that a command execution has ended. This event carries the output of the command execution, which could be an error if the command failed.

func NewEventEnd added in v0.25.0

func NewEventEnd(ctx context.Context, output Output) EventEnd

NewEventEnd creates a new EventEnd with the given context and output.

func (EventEnd) GetContext added in v0.25.0

func (e EventEnd) GetContext() context.Context

GetContext returns the context associated with the EventEnd.

type EventListResources added in v0.25.0

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

EventListResources indicates that a list of resources was requested.

func NewEventListResources added in v0.25.0

func NewEventListResources(ctx context.Context) EventListResources

NewEventListResources creates a new EventListResources with the given context.

func (EventListResources) GetContext added in v0.25.0

func (e EventListResources) GetContext() context.Context

GetContext returns the context associated with the EventListResources.

type EventOpenResource added in v0.25.0

type EventOpenResource struct {
	Resource kube.Resource
	// contains filtered or unexported fields
}

EventOpenResource indicates that a specific resource was opened.

func NewEventOpenResource added in v0.25.0

func NewEventOpenResource(ctx context.Context, resource kube.Resource) EventOpenResource

NewEventOpenResource creates a new EventOpenResource with the given context and resource.

func (EventOpenResource) GetContext added in v0.25.0

func (e EventOpenResource) GetContext() context.Context

GetContext returns the context associated with the EventOpenResource.

type EventStart

type EventStart struct {
	Type Type
	// contains filtered or unexported fields
}

EventStart indicates that a command execution has started.

func NewEventStart added in v0.25.0

func NewEventStart(ctx context.Context, t Type) EventStart

NewEventStart creates a new EventStart with the given context and type.

func (EventStart) GetContext added in v0.25.0

func (e EventStart) GetContext() context.Context

GetContext returns the context associated with the EventStart.

type FilteredFS added in v0.25.0

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

FilteredFS wraps a RootFS and filters the tree based on rule matches. It hides files and directories that do not match any rules. Note that it does not directly prevent access to any files in the tree. (However, os.Root will prevent leaving the initially provided directory tree.)

func NewFilteredFS added in v0.25.0

func NewFilteredFS(root RootFS, rules ...*rule.Rule) (*FilteredFS, error)

NewFilteredFS creates a new FilteredFS with the given directory path and rules.

func NewFilteredFSFromPath added in v0.25.0

func NewFilteredFSFromPath(dirPath string, rules ...*rule.Rule) (*FilteredFS, error)

func (*FilteredFS) Close added in v0.25.0

func (f *FilteredFS) Close() error

Close closes the FilteredFS. After Close is called, methods on FilteredFS return errors.

func (*FilteredFS) Name added in v0.25.0

func (f *FilteredFS) Name() string

Name returns the name of the directory presented to OpenRoot. It is safe to call Name after FilteredFS.Close.

func (*FilteredFS) Open added in v0.25.0

func (f *FilteredFS) Open(name string) (*os.File, error)

Open opens the named file in the root for reading. See os.Open for more details.

func (*FilteredFS) OpenFile added in v0.25.0

func (f *FilteredFS) OpenFile(name string, flag int, perm fs.FileMode) (*os.File, error)

OpenFile opens the named file in the root. See os.OpenFile for more details. If perm contains bits other than the nine least-significant bits (0o777), OpenFile returns an error.

func (*FilteredFS) ReadDir added in v0.25.0

func (f *FilteredFS) ReadDir(name string) ([]os.DirEntry, error)

ReadDir reads the named directory, returning any entries that match at least one rule.Rule (recursively), sorted by filename.

func (*FilteredFS) Stat added in v0.25.0

func (f *FilteredFS) Stat(name string) (fs.FileInfo, error)

Stat returns a fs.FileInfo describing the named file in the root. See os.Stat for more details.

type Output

type Output struct {
	Timestamp time.Time
	Error     error
	Stdout    string
	Stderr    string
	Resources []*kube.Resource
	Type      Type
}

func NewOutput added in v0.25.0

func NewOutput(t Type, opts ...OutputOpt) Output

NewOutput creates a new Output timestamped with the current time.

type OutputOpt added in v0.25.0

type OutputOpt func(*Output)

func WithError added in v0.25.0

func WithError(err error) OutputOpt

WithError sets the error for the output.

type ProfileMatch added in v0.25.0

type ProfileMatch struct {
	Profile *profile.Profile
	Name    string
}

type RootFS added in v0.25.0

type RootFS interface {
	Close() error
	FS() fs.FS
	Name() string
	Open(name string) (*os.File, error)
	OpenFile(name string, flag int, perm os.FileMode) (*os.File, error)
	Remove(name string) error
	Stat(name string) (os.FileInfo, error)
}

RootFS is an interface for a filesystem that quacks like os.Root.

type Runner

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

Runner wraps one or more Rule objects. It manages:

  • File-to-command mappings.
  • Filesystem notifications / watching.
  • Concurrent command execution.

func NewRunner

func NewRunner(path string, opts ...RunnerOpt) (*Runner, error)

NewRunner creates a new Runner. It uses the current working directory as the filesystem root.

func NewRunnerWithRoot added in v0.25.0

func NewRunnerWithRoot(root RootFS, path string, opts ...RunnerOpt) (*Runner, error)

NewRunner creates a new Runner using the provided RootFS. This is not a normal opt since it cannot be re-configured after the runner has been created.

func (*Runner) Close

func (cr *Runner) Close()

func (*Runner) Configure added in v0.25.0

func (cr *Runner) Configure(opts ...RunnerOpt) error

func (*Runner) ConfigureContext added in v0.25.0

func (cr *Runner) ConfigureContext(ctx context.Context, opts ...RunnerOpt) error

Configure applies options to an existing runner. This allows reconfiguration after creation.

func (*Runner) FS added in v0.25.0

func (cr *Runner) FS() (*FilteredFS, error)

FS creates a FilteredFS for the runner that hides directories and files unless they match at least one of the configured rules.

func (*Runner) FindProfile added in v0.25.0

func (cr *Runner) FindProfile(path string) (string, *profile.Profile, error)

func (*Runner) FindProfiles added in v0.25.0

func (cr *Runner) FindProfiles(path string) ([]ProfileMatch, error)

FindProfiles finds matching profiles for the given path using the configured rules. The results are returned in order of priority.

func (*Runner) GetCurrentProfile

func (cr *Runner) GetCurrentProfile() (string, *profile.Profile)

func (*Runner) GetPath added in v0.27.0

func (cr *Runner) GetPath() string

func (*Runner) GetProfiles added in v0.25.0

func (cr *Runner) GetProfiles() map[string]*profile.Profile

func (*Runner) Run

func (cr *Runner) Run() Output

RunFirstMatch executes the first matching command for the given path. If path is a file, it checks for direct matches. If path is a directory, it checks all files in the directory for matches.

func (*Runner) RunContext

func (cr *Runner) RunContext(ctx context.Context) Output

RunContext executes the first matching command for the given path with the provided context. If path is a file, it checks for direct matches. If path is a directory, it checks all files in the directory for matches. The context can be used for cancellation, timeouts, and tracing.

func (*Runner) RunOnEvent

func (cr *Runner) RunOnEvent()

RunOnEvent listens for file system events and runs the command in response. The output should be collected via Runner.Subscribe.

func (*Runner) RunPlugin

func (cr *Runner) RunPlugin(name string) Output

RunPlugin executes a plugin by name.

func (*Runner) RunPluginContext

func (cr *Runner) RunPluginContext(ctx context.Context, name string) Output

RunPluginContext executes a plugin by name with the provided context.

func (*Runner) SendEvent added in v0.25.0

func (cr *Runner) SendEvent(evt Event)

SendEvent allows external components to send events to all listeners.

func (*Runner) SetProfile added in v0.25.0

func (cr *Runner) SetProfile(name string) error

func (*Runner) String

func (cr *Runner) String() string

func (*Runner) Subscribe

func (cr *Runner) Subscribe(ch chan<- Event)

Subscribe allows other components to listen for command events.

type RunnerOpt

type RunnerOpt func(cr *Runner) error

func WithAutoProfile added in v0.25.0

func WithAutoProfile() RunnerOpt

WithAutoProfile configures the runner to determine the profile via rules.

func WithCustomProfile added in v0.25.0

func WithCustomProfile(name string, p *profile.Profile) RunnerOpt

WithProfile sets a custom profile to use.

func WithExtraArgs added in v0.25.0

func WithExtraArgs(args ...string) RunnerOpt

WithExtraArgs sets additional arguments to pass to the command. This will override defined ExtraArgs on whatever profile was selected.

func WithPath added in v0.25.0

func WithPath(path string) RunnerOpt

WithPath sets the path for the runner (relative to the initial root). If the path tries to escape the root, it returns an error early to avoid runtime errors deeper in the stack.

func WithProfile

func WithProfile(name string) RunnerOpt

WithProfile sets a specific profile to use.

func WithProfiles added in v0.25.0

func WithProfiles(profiles map[string]*profile.Profile) RunnerOpt

WithProfiles adds additional profiles to the runner's profile map. This allows profiles to be available for switching even if they don't have associated rules.

func WithRules

func WithRules(rs []*rule.Rule) RunnerOpt

WithRules sets multiple rules from which the first matching rule will be used.

func WithWatch added in v0.25.0

func WithWatch(watch bool) RunnerOpt

WithWatch sets the watch flag for the runner.

func WithWatcherBatchDuration added in v0.25.0

func WithWatcherBatchDuration(dur time.Duration) RunnerOpt

WithWatcherBatchDuration sets the batch duration for file system events.

type Static

type Static struct {
	Resources []*kube.Resource
	// contains filtered or unexported fields
}

func NewStatic

func NewStatic(input string) (*Static, error)

func (*Static) Close

func (rg *Static) Close()

func (*Static) Configure added in v0.25.0

func (rg *Static) Configure(_ ...RunnerOpt) error

func (*Static) ConfigureContext added in v0.25.0

func (rg *Static) ConfigureContext(_ context.Context, _ ...RunnerOpt) error

func (*Static) FS added in v0.25.0

func (rg *Static) FS() (*FilteredFS, error)

func (*Static) FindProfile added in v0.25.0

func (rg *Static) FindProfile(_ string) (string, *profile.Profile, error)

func (*Static) FindProfiles added in v0.25.0

func (rg *Static) FindProfiles(_ string) ([]ProfileMatch, error)

func (*Static) GetCurrentProfile

func (rg *Static) GetCurrentProfile() (string, *profile.Profile)

func (*Static) GetPath added in v0.27.0

func (rg *Static) GetPath() string

func (*Static) GetProfiles added in v0.25.0

func (rg *Static) GetProfiles() map[string]*profile.Profile

func (*Static) GetRules added in v0.25.0

func (rg *Static) GetRules() []*rule.Rule

func (*Static) Run

func (rg *Static) Run() Output

func (*Static) RunContext added in v0.25.0

func (rg *Static) RunContext(ctx context.Context) Output

func (*Static) RunOnEvent

func (rg *Static) RunOnEvent()

func (*Static) RunPlugin

func (rg *Static) RunPlugin(_ string) Output

func (*Static) RunPluginContext added in v0.25.0

func (rg *Static) RunPluginContext(ctx context.Context, _ string) Output

func (*Static) SendEvent added in v0.25.0

func (rg *Static) SendEvent(evt Event)

SendEvent allows external components to send events to all listeners.

func (*Static) String

func (rg *Static) String() string

func (*Static) Subscribe

func (rg *Static) Subscribe(ch chan<- Event)

type Type

type Type int
const (
	// TypeRun indicates a command execution.
	TypeRun Type = iota
	// TypePlugin indicates a plugin execution.
	TypePlugin
)

Jump to

Keyboard shortcuts

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