### Specifying Command Path Directly Source: https://github.com/thoughtbot/terrapin/blob/main/README.md Shows how to provide the full path to the command directly when creating the `Terrapin::CommandLine` instance. ```ruby line = Terrapin::CommandLine.new("/opt/bin/lolwut") line.command # => "/opt/bin/lolwut" ``` -------------------------------- ### Basic Command Execution Source: https://github.com/thoughtbot/terrapin/blob/main/README.md Execute a simple shell command and capture its output. The command and its arguments are directly provided. ```ruby line = Terrapin::CommandLine.new("echo", "hello 'world'") line.command # => "echo hello 'world'" line.run # => "hello world\n" ``` -------------------------------- ### Specifying Command Search Path Source: https://github.com/thoughtbot/terrapin/blob/main/README.md Demonstrates how to set a global search path for executables using `Terrapin::CommandLine.path =`. ```ruby Terrapin::CommandLine.path = "/opt/bin" line = Terrapin::CommandLine.new("lolwut") line.command # => "lolwut", but it looks in /opt/bin for it. ``` -------------------------------- ### Multiple Search Paths for Commands Source: https://github.com/thoughtbot/terrapin/blob/main/README.md Allows specifying an array of directories to search for executables, enabling commands to be found across multiple locations. ```ruby FileUtils.rm("/opt/bin/lolwut") File.open('/usr/local/bin/lolwut') { |f| f.write('echo Hello') } Terrapin::CommandLine.path = ["/opt/bin", "/usr/local/bin"] line = Terrapin::CommandLine.new("lolwut") line.run # => prints 'Hello', because it searches the path ``` -------------------------------- ### Securely Interpolating User Data with `run` Source: https://github.com/thoughtbot/terrapin/blob/main/README.md Shows the recommended way to handle potentially unsafe input by interpolating it via the `run` method, ensuring it is properly escaped. ```ruby line = Terrapin::CommandLine.new("echo", "haha:whoami") line.run(whoami: "`whoami`") # => "haha`whoami`\n" ``` -------------------------------- ### Logging Command Execution (Single Instance) Source: https://github.com/thoughtbot/terrapin/blob/main/README.md Configures a logger for a specific `Terrapin::CommandLine` instance to log the command being executed. ```ruby line = Terrapin::CommandLine.new("echo", ":var", logger: Logger.new(STDOUT)) line.run(var: "LOL!") # => Logs this with #info -> Command :: echo 'LOL!' ``` -------------------------------- ### Handling Command Errors with Exceptions Source: https://github.com/thoughtbot/terrapin/blob/main/README.md Demonstrates how Terrapin raises an `ExitStatusError` when a command returns a non-zero exit code, and how to catch it. ```ruby line = Terrapin::CommandLine.new("git", "commit") begin line.run rescue Terrapin::ExitStatusError => e e.message # => "Command 'git commit' returned 1. Expected 0" end ``` -------------------------------- ### Security Note: Arguments to `new` are not escaped Source: https://github.com/thoughtbot/terrapin/blob/main/README.md Illustrates that arguments passed directly to `Terrapin::CommandLine.new` are not escaped, unlike those interpolated via `run` or `command`. ```ruby line = Terrapin::CommandLine.new("echo", "haha`whoami`") line.command # => "echo haha`whoami`" line.run # => "hahawebserver\n" ``` -------------------------------- ### Logging All Command Executions (Global) Source: https://github.com/thoughtbot/terrapin/blob/main/README.md Sets a global logger for all `Terrapin::CommandLine` instances to log every command executed. ```ruby Terrapin::CommandLine.logger = Logger.new(STDOUT) Terrapin::CommandLine.new("date").run # => Logs this -> Command :: date ``` -------------------------------- ### Using Backticks Runner Source: https://github.com/thoughtbot/terrapin/blob/main/README.md Configures Terrapin to use Ruby's backticks (` `) for command execution instead of the default `Process.spawn`. ```ruby Terrapin::CommandLine.runner = Terrapin::CommandLine::BackticksRunner.new ``` -------------------------------- ### Use PopenRunner to Avoid Spawn Warnings Source: https://github.com/thoughtbot/terrapin/blob/main/README.md If you encounter an 'unsupported spawn option: out' warning, configure Terrapin to use PopenRunner. This is a common solution for compatibility issues. ```ruby Terrapin::CommandLine.runner = Terrapin::CommandLine::PopenRunner.new ``` -------------------------------- ### Handling Command Not Found Errors Source: https://github.com/thoughtbot/terrapin/blob/main/README.md Shows how Terrapin raises a `CommandNotFoundError` if the executable is not found in the system's PATH. ```ruby line = Terrapin::CommandLine.new("lolwut") begin line.run rescue Terrapin::CommandNotFoundError => e e # => the command isn't in the $PATH for this process. ``` -------------------------------- ### Handling Expected Non-Zero Exit Codes Source: https://github.com/thoughtbot/terrapin/blob/main/README.md Allows specifying a list of expected non-zero exit codes, preventing `ExitStatusError` from being raised for those specific codes. ```ruby line = Terrapin::CommandLine.new("/usr/bin/false", "", expected_outcodes: [0, 1]) begin line.run rescue Terrapin::ExitStatusError => e # => You never get here! end ``` -------------------------------- ### Preventing Command Injection (Interpolated) Source: https://github.com/thoughtbot/terrapin/blob/main/README.md Demonstrates how Terrapin escapes potentially malicious input when arguments are interpolated using the `command` method. ```ruby line = Terrapin::CommandLine.new("cat", ":file") line.command(file: "haha`rm -rf /`.txt") # => "cat 'haha`rm -rf /`.txt'" line = Terrapin::CommandLine.new("cat", ":file") line.command(file: "ohyeah?'`rm -rf /`.ha!") # => "cat 'ohyeah?'\''`rm -rf /`.ha!'" ``` -------------------------------- ### Ignoring Command Output and Stderr Source: https://github.com/thoughtbot/terrapin/blob/main/README.md Configure Terrapin to suppress stderr output by redirecting it to `/dev/null` (Unix-like) or `NUL` (Windows). ```ruby line = Terrapin::CommandLine.new("noisy", "--extra-verbose", swallow_stderr: true) line.command # => "noisy --extra-verbose 2>/dev/null" ``` -------------------------------- ### Interpolated Arguments Source: https://github.com/thoughtbot/terrapin/blob/main/README.md Use named placeholders for arguments that will be interpolated into the command string. Terrapin automatically escapes these interpolated arguments for security. ```ruby line = Terrapin::CommandLine.new("convert", ":in -scale :resolution :out") line.command(in: "omg.jpg", resolution: "32x32", out: "omg_thumb.jpg") # => "convert 'omg.jpg' -scale '32x32' 'omg_thumb.jpg'" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.