File size: 6,616 Bytes
3b67767 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
// Package shell provides cross-platform shell execution capabilities.
//
// This package offers two main types:
// - Shell: A general-purpose shell executor for one-off or managed commands
// - PersistentShell: A singleton shell that maintains state across the application
//
// WINDOWS COMPATIBILITY:
// This implementation provides both POSIX shell emulation (mvdan.cc/sh/v3),
// even on Windows. Some caution has to be taken: commands should have forward
// slashes (/) as path separators to work, even on Windows.
package shell
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"slices"
"strings"
"sync"
"github.com/charmbracelet/x/exp/slice"
"mvdan.cc/sh/moreinterp/coreutils"
"mvdan.cc/sh/v3/expand"
"mvdan.cc/sh/v3/interp"
"mvdan.cc/sh/v3/syntax"
)
// ShellType represents the type of shell to use
type ShellType int
const (
ShellTypePOSIX ShellType = iota
ShellTypeCmd
ShellTypePowerShell
)
// Logger interface for optional logging
type Logger interface {
InfoPersist(msg string, keysAndValues ...any)
}
// noopLogger is a logger that does nothing
type noopLogger struct{}
func (noopLogger) InfoPersist(msg string, keysAndValues ...any) {}
// BlockFunc is a function that determines if a command should be blocked
type BlockFunc func(args []string) bool
// Shell provides cross-platform shell execution with optional state persistence
type Shell struct {
env []string
cwd string
mu sync.Mutex
logger Logger
blockFuncs []BlockFunc
}
// Options for creating a new shell
type Options struct {
WorkingDir string
Env []string
Logger Logger
BlockFuncs []BlockFunc
}
// NewShell creates a new shell instance with the given options
func NewShell(opts *Options) *Shell {
if opts == nil {
opts = &Options{}
}
cwd := opts.WorkingDir
if cwd == "" {
cwd, _ = os.Getwd()
}
env := opts.Env
if env == nil {
env = os.Environ()
}
logger := opts.Logger
if logger == nil {
logger = noopLogger{}
}
return &Shell{
cwd: cwd,
env: env,
logger: logger,
blockFuncs: opts.BlockFuncs,
}
}
// Exec executes a command in the shell
func (s *Shell) Exec(ctx context.Context, command string) (string, string, error) {
s.mu.Lock()
defer s.mu.Unlock()
return s.execPOSIX(ctx, command)
}
// GetWorkingDir returns the current working directory
func (s *Shell) GetWorkingDir() string {
s.mu.Lock()
defer s.mu.Unlock()
return s.cwd
}
// SetWorkingDir sets the working directory
func (s *Shell) SetWorkingDir(dir string) error {
s.mu.Lock()
defer s.mu.Unlock()
// Verify the directory exists
if _, err := os.Stat(dir); err != nil {
return fmt.Errorf("directory does not exist: %w", err)
}
s.cwd = dir
return nil
}
// GetEnv returns a copy of the environment variables
func (s *Shell) GetEnv() []string {
s.mu.Lock()
defer s.mu.Unlock()
env := make([]string, len(s.env))
copy(env, s.env)
return env
}
// SetEnv sets an environment variable
func (s *Shell) SetEnv(key, value string) {
s.mu.Lock()
defer s.mu.Unlock()
// Update or add the environment variable
keyPrefix := key + "="
for i, env := range s.env {
if strings.HasPrefix(env, keyPrefix) {
s.env[i] = keyPrefix + value
return
}
}
s.env = append(s.env, keyPrefix+value)
}
// SetBlockFuncs sets the command block functions for the shell
func (s *Shell) SetBlockFuncs(blockFuncs []BlockFunc) {
s.mu.Lock()
defer s.mu.Unlock()
s.blockFuncs = blockFuncs
}
// CommandsBlocker creates a BlockFunc that blocks exact command matches
func CommandsBlocker(cmds []string) BlockFunc {
bannedSet := make(map[string]struct{})
for _, cmd := range cmds {
bannedSet[cmd] = struct{}{}
}
return func(args []string) bool {
if len(args) == 0 {
return false
}
_, ok := bannedSet[args[0]]
return ok
}
}
// ArgumentsBlocker creates a BlockFunc that blocks specific subcommand
func ArgumentsBlocker(cmd string, args []string, flags []string) BlockFunc {
return func(parts []string) bool {
if len(parts) == 0 || parts[0] != cmd {
return false
}
argParts, flagParts := splitArgsFlags(parts[1:])
if len(argParts) < len(args) || len(flagParts) < len(flags) {
return false
}
argsMatch := slices.Equal(argParts[:len(args)], args)
flagsMatch := slice.IsSubset(flags, flagParts)
return argsMatch && flagsMatch
}
}
func splitArgsFlags(parts []string) (args []string, flags []string) {
args = make([]string, 0, len(parts))
flags = make([]string, 0, len(parts))
for _, part := range parts {
if strings.HasPrefix(part, "-") {
// Extract flag name before '=' if present
flag := part
if idx := strings.IndexByte(part, '='); idx != -1 {
flag = part[:idx]
}
flags = append(flags, flag)
} else {
args = append(args, part)
}
}
return
}
func (s *Shell) blockHandler() func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc {
return func(next interp.ExecHandlerFunc) interp.ExecHandlerFunc {
return func(ctx context.Context, args []string) error {
if len(args) == 0 {
return next(ctx, args)
}
for _, blockFunc := range s.blockFuncs {
if blockFunc(args) {
return fmt.Errorf("command is not allowed for security reasons: %s", strings.Join(args, " "))
}
}
return next(ctx, args)
}
}
}
// execPOSIX executes commands using POSIX shell emulation (cross-platform)
func (s *Shell) execPOSIX(ctx context.Context, command string) (string, string, error) {
line, err := syntax.NewParser().Parse(strings.NewReader(command), "")
if err != nil {
return "", "", fmt.Errorf("could not parse command: %w", err)
}
var stdout, stderr bytes.Buffer
runner, err := interp.New(
interp.StdIO(nil, &stdout, &stderr),
interp.Interactive(false),
interp.Env(expand.ListEnviron(s.env...)),
interp.Dir(s.cwd),
interp.ExecHandlers(s.blockHandler(), coreutils.ExecHandler),
)
if err != nil {
return "", "", fmt.Errorf("could not run command: %w", err)
}
err = runner.Run(ctx, line)
s.cwd = runner.Dir
s.env = []string{}
for name, vr := range runner.Vars {
s.env = append(s.env, fmt.Sprintf("%s=%s", name, vr.Str))
}
s.logger.InfoPersist("POSIX command finished", "command", command, "err", err)
return stdout.String(), stderr.String(), err
}
// IsInterrupt checks if an error is due to interruption
func IsInterrupt(err error) bool {
return errors.Is(err, context.Canceled) ||
errors.Is(err, context.DeadlineExceeded)
}
// ExitCode extracts the exit code from an error
func ExitCode(err error) int {
if err == nil {
return 0
}
var exitErr interp.ExitStatus
if errors.As(err, &exitErr) {
return int(exitErr)
}
return 1
}
|