Class: GDKBox::Shell
- Inherits:
-
Object
- Object
- GDKBox::Shell
- Defined in:
- lib/gdkbox/shell.rb
Overview
Thin wrapper around shelling out to external commands.
All arguments are passed as an explicit argv array (never a single interpolated string) so user-supplied values cannot be interpreted by a shell. Tests inject a fake that records the commands instead of running them.
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
-
#capture(*args, input: nil) ⇒ Object
Run a command and return its stdout, raising on failure.
-
#run(*args, input: nil) ⇒ Object
Run a command, returning a Result.
-
#run!(*args, input: nil) ⇒ Object
Run a command, raising CommandError on a non-zero exit.
-
#which(command) ⇒ Object
Resolve an executable on PATH, returning its path or nil.
Instance Method Details
#capture(*args, input: nil) ⇒ Object
Run a command and return its stdout, raising on failure.
47 48 49 |
# File 'lib/gdkbox/shell.rb', line 47 def capture(*args, input: nil) run!(*args, input: input).stdout end |
#run(*args, input: nil) ⇒ Object
Run a command, returning a Result. Never raises on a non-zero exit.
32 33 34 35 36 |
# File 'lib/gdkbox/shell.rb', line 32 def run(*args, input: nil) argv = args.map(&:to_s) stdout, stderr, status = Open3.capture3(*argv, stdin_data: input) Result.new(stdout, stderr, status.exitstatus || 1) end |
#run!(*args, input: nil) ⇒ Object
Run a command, raising CommandError on a non-zero exit.
39 40 41 42 43 44 |
# File 'lib/gdkbox/shell.rb', line 39 def run!(*args, input: nil) result = run(*args, input: input) raise CommandError.new(args, result.status, result.stderr) unless result.success? result end |
#which(command) ⇒ Object
Resolve an executable on PATH, returning its path or nil.
52 53 54 55 |
# File 'lib/gdkbox/shell.rb', line 52 def which(command) result = run("sh", "-c", "command -v #{command}") result.success? ? result.stdout.strip : nil end |