Class: ShellRunner
- Inherits:
-
Object
- Object
- ShellRunner
- Defined in:
- lib/sfb_scripts/shells/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.
6 7 8 9 10 11 |
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 6 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.
4 5 6 |
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 4 def log_path @log_path end |
#working_directory ⇒ Object
Returns the value of attribute working_directory.
4 5 6 |
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 4 def working_directory @working_directory end |
Instance Method Details
#confirm?(question) ⇒ Boolean
33 34 35 36 37 |
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 33 def confirm?(question) warn "#{question} [Yn]" answer = STDIN.gets.strip.downcase return answer != 'n' end |
#deny?(question) ⇒ Boolean
39 40 41 |
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 39 def deny?(question) ! confirm?(question) end |
#enqueue(cmd, dir: working_directory) ⇒ Object
28 29 30 31 |
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 28 def enqueue(cmd, dir: working_directory) command = "cd #{dir} && #{cmd} && cd -" @queue += "#{command};\n" end |
#exec(cmd, dir: working_directory) ⇒ Object
22 23 24 25 26 |
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 22 def exec(cmd, dir: working_directory) command = "cd #{dir} && #{cmd}" notify "\n#{command}" Kernel.exec command end |
#exec_queue ⇒ Object
43 44 45 46 47 48 |
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 43 def exec_queue notify 'running: ' notify '' notify @queue Kernel.exec @queue end |
#log(msg) ⇒ Object
60 61 62 |
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 60 def log(msg) %x{echo "#{msg}" >> #{log_path}} end |
#notify(msg) ⇒ Object
55 56 57 58 |
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 55 def notify(msg) log msg puts msg.yellow end |
#reset_log ⇒ Object
64 65 66 |
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 64 def reset_log %x{echo "" > #{log_path}} end |
#run(cmd, dir: working_directory) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 13 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
50 51 52 53 |
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 50 def warn(msg) log msg puts msg.red end |