shell

package
v1.6.15 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package shell provides a cross-platform shell interface for executing commands.

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

This section is empty.

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
	Cmd       string
	Args      []string
	StartedAt time.Time
	// contains filtered or unexported fields
}

BackgroundShell represents a running background shell command.

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 BackgroundShellManager

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

BackgroundShellManager manages background shell commands.

func NewBackgroundShellManager

func NewBackgroundShellManager() *BackgroundShellManager

NewBackgroundShellManager creates a new BackgroundShellManager.

func (*BackgroundShellManager) Cleanup

func (m *BackgroundShellManager) Cleanup()

Cleanup removes all background shells.

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.

func (*BackgroundShellManager) List

List returns all running background shells.

func (*BackgroundShellManager) Start

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

Start starts a new background shell 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