shell

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package shell provides cross-platform shell execution capabilities.

This package provides Shell instances for executing commands with their own working directory and environment. Each shell execution is independent.

WINDOWS COMPATIBILITY: This implementation provides POSIX shell emulation (mvdan.cc/sh/v3) even on Windows. Commands should use forward slashes (/) as path separators to work correctly on all platforms.

Index

Constants

View Source
const (
	// MaxBackgroundJobs is the maximum number of concurrent background jobs allowed
	MaxBackgroundJobs = 50
	// CompletedJobRetentionMinutes is how long to keep completed jobs before auto-cleanup (8 hours)
	CompletedJobRetentionMinutes = 8 * 60
)

Variables

This section is empty.

Functions

func ExitCode

func ExitCode(err error) int

ExitCode extracts the exit code from an error

func IsInterrupt

func IsInterrupt(err error) bool

IsInterrupt checks if an error is due to interruption

Types

type BackgroundShell

type BackgroundShell struct {
	ID          string
	Command     string
	Description string
	Shell       *Shell
	WorkingDir  string
	// contains filtered or unexported fields
}

BackgroundShell represents a shell running in the background.

func (*BackgroundShell) GetOutput

func (bs *BackgroundShell) GetOutput() (stdout string, stderr string, done bool, err error)

GetOutput returns the current output of a background shell.

func (*BackgroundShell) IsDone

func (bs *BackgroundShell) IsDone() bool

IsDone checks if the background shell has finished execution.

func (*BackgroundShell) Wait

func (bs *BackgroundShell) Wait()

Wait blocks until the background shell completes.

type BackgroundShellInfo

type BackgroundShellInfo struct {
	ID          string
	Command     string
	Description string
}

BackgroundShellInfo contains information about a background shell.

type BackgroundShellManager

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

BackgroundShellManager manages background shell instances.

func GetBackgroundShellManager

func GetBackgroundShellManager() *BackgroundShellManager

GetBackgroundShellManager returns the singleton background shell manager.

func (*BackgroundShellManager) Cleanup

func (m *BackgroundShellManager) Cleanup() int

Cleanup removes completed jobs that have been finished for more than the retention period

func (*BackgroundShellManager) Get

Get retrieves a background shell by ID.

func (*BackgroundShellManager) Kill

func (m *BackgroundShellManager) Kill(id string) error

Kill terminates a background shell by ID.

func (*BackgroundShellManager) KillAll

func (m *BackgroundShellManager) KillAll()

KillAll terminates all background shells.

func (*BackgroundShellManager) List

func (m *BackgroundShellManager) List() []string

List returns all background shell IDs.

func (*BackgroundShellManager) Remove

func (m *BackgroundShellManager) Remove(id string) error

Remove removes a background shell from the manager without terminating it. This is useful when a shell has already completed and you just want to clean up tracking.

func (*BackgroundShellManager) Start

func (m *BackgroundShellManager) Start(ctx context.Context, workingDir string, blockFuncs []BlockFunc, command string, description string) (*BackgroundShell, error)

Start creates and starts a new background shell with the given command.

type BlockFunc

type BlockFunc func(args []string) bool

BlockFunc is a function that determines if a command should be blocked

func ArgumentsBlocker

func ArgumentsBlocker(cmd string, args []string, flags []string) BlockFunc

ArgumentsBlocker creates a BlockFunc that blocks specific subcommand

func CommandsBlocker

func CommandsBlocker(cmds []string) BlockFunc

CommandsBlocker creates a BlockFunc that blocks exact command matches

type Logger

type Logger interface {
	InfoPersist(msg string, keysAndValues ...any)
}

Logger interface for optional logging

type Options

type Options struct {
	WorkingDir string
	Env        []string
	Logger     Logger
	BlockFuncs []BlockFunc
}

Options for creating a new shell

type Shell

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

Shell provides cross-platform shell execution with optional state persistence

func NewShell

func NewShell(opts *Options) *Shell

NewShell creates a new shell instance with the given options

func (*Shell) Exec

func (s *Shell) Exec(ctx context.Context, command string) (string, string, error)

Exec executes a command in the shell

func (*Shell) ExecStream

func (s *Shell) ExecStream(ctx context.Context, command string, stdout, stderr io.Writer) error

ExecStream executes a command in the shell with streaming output to provided writers

func (*Shell) GetEnv

func (s *Shell) GetEnv() []string

GetEnv returns a copy of the environment variables

func (*Shell) GetWorkingDir

func (s *Shell) GetWorkingDir() string

GetWorkingDir returns the current working directory

func (*Shell) SetBlockFuncs

func (s *Shell) SetBlockFuncs(blockFuncs []BlockFunc)

SetBlockFuncs sets the command block functions for the shell

func (*Shell) SetEnv

func (s *Shell) SetEnv(key, value string)

SetEnv sets an environment variable

func (*Shell) SetWorkingDir

func (s *Shell) SetWorkingDir(dir string) error

SetWorkingDir sets the working directory

type ShellType

type ShellType int

ShellType represents the type of shell to use

const (
	ShellTypePOSIX ShellType = iota
	ShellTypeCmd
	ShellTypePowerShell
)

Jump to

Keyboard shortcuts

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