Class: ShellRunner
- Inherits:
-
Object
- Object
- ShellRunner
- Defined in:
- lib/shell_runner.rb
Direct Known Subclasses
Constant Summary collapse
- CommandFailureError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#log_path ⇒ Object
Returns the value of attribute log_path.
-
#working_directory ⇒ Object
Returns the value of attribute working_directory.
Instance Method Summary collapse
- #confirm?(question) ⇒ Boolean
- #deny?(question) ⇒ Boolean
- #enqueue(cmd, dir: working_directory) ⇒ Object
- #exec(cmd, dir: working_directory) ⇒ Object
- #exec_queue ⇒ Object
-
#initialize(log_path, working_directory) ⇒ ShellRunner
constructor
A new instance of ShellRunner.
- #log(msg) ⇒ Object
- #notify(msg) ⇒ Object
- #reset_log ⇒ Object
- #run(cmd, dir: working_directory) ⇒ Object
- #warn(msg) ⇒ Object
Constructor Details
#initialize(log_path, working_directory) ⇒ ShellRunner
Returns a new instance of ShellRunner.
8 9 10 11 12 13 |
# File 'lib/shell_runner.rb', line 8 def initialize(log_path, working_directory) @working_directory = working_directory @queue = '' @log_path = log_path reset_log end |
Instance Attribute Details
#log_path ⇒ Object
Returns the value of attribute log_path.
6 7 8 |
# File 'lib/shell_runner.rb', line 6 def log_path @log_path end |
#working_directory ⇒ Object
Returns the value of attribute working_directory.
6 7 8 |
# File 'lib/shell_runner.rb', line 6 def working_directory @working_directory end |
Instance Method Details
#confirm?(question) ⇒ Boolean
35 36 37 38 39 |
# File 'lib/shell_runner.rb', line 35 def confirm?(question) warn "#{question} [Yn]" answer = STDIN.gets.strip.downcase return answer != 'n' end |
#deny?(question) ⇒ Boolean
41 42 43 |
# File 'lib/shell_runner.rb', line 41 def deny?(question) ! confirm?(question) end |
#enqueue(cmd, dir: working_directory) ⇒ Object
30 31 32 33 |
# File 'lib/shell_runner.rb', line 30 def enqueue(cmd, dir: working_directory) command = "cd #{dir} && #{cmd} && cd -" @queue += "#{command};\n" end |
#exec(cmd, dir: working_directory) ⇒ Object
24 25 26 27 28 |
# File 'lib/shell_runner.rb', line 24 def exec(cmd, dir: working_directory) command = "cd #{dir} && #{cmd}" notify "\n#{command}" Kernel.exec command end |
#exec_queue ⇒ Object
45 46 47 48 49 50 |
# File 'lib/shell_runner.rb', line 45 def exec_queue notify 'running: ' notify '' notify @queue Kernel.exec @queue end |
#log(msg) ⇒ Object
62 63 64 |
# File 'lib/shell_runner.rb', line 62 def log(msg) %x{echo "#{msg}" >> #{log_path}} end |
#notify(msg) ⇒ Object
57 58 59 60 |
# File 'lib/shell_runner.rb', line 57 def notify(msg) log msg puts msg.yellow end |
#reset_log ⇒ Object
66 67 68 |
# File 'lib/shell_runner.rb', line 66 def reset_log %x{echo "" > #{log_path}} end |
#run(cmd, dir: working_directory) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/shell_runner.rb', line 15 def run(cmd, dir: working_directory) command = "cd #{dir} && #{cmd}" puts command log command %x{ set -o pipefail && #{command} 2>> #{log_path} | tee -a #{log_path} }.chomp.tap do raise CommandFailureError, "The following command has failed: #{command}. See #{log_path} for a full log." if ($?.exitstatus != 0) end end |
#warn(msg) ⇒ Object
52 53 54 55 |
# File 'lib/shell_runner.rb', line 52 def warn(msg) log msg puts msg.red end |