Class: ShellRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/sfb_scripts/shells/shell_runner.rb

Direct Known Subclasses

LoudShellRunner

Constant Summary collapse

CommandFailureError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

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_pathObject

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_directoryObject

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

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 36

def confirm?(question)
  warn "#{question} [Yn]"
  answer = STDIN.gets.strip.downcase
  return answer != 'n'
end

#deny?(question) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 42

def deny?(question)
  ! confirm?(question)
end

#enqueue(cmd, dir: working_directory) ⇒ Object



31
32
33
34
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 31

def enqueue(cmd, dir: working_directory)
  command = "cd #{dir} && #{cmd} && cd -"
  @queue += "#{command};\n"
end

#exec(cmd, dir: working_directory) ⇒ Object



25
26
27
28
29
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 25

def exec(cmd, dir: working_directory)
  command = "cd #{dir} && #{cmd}"
  notify "\n#{command}"
  Kernel.exec command
end

#exec_queueObject



46
47
48
49
50
51
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 46

def exec_queue
  notify 'running: '
  notify ''
  notify @queue
  Kernel.exec @queue
end

#handle_output_for(cmd) ⇒ Object



71
72
73
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 71

def handle_output_for(cmd)
  log(cmd)
end

#log(msg) ⇒ Object



63
64
65
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 63

def log(msg)
  %x{echo "#{msg}" >> #{log_path}}
end

#notify(msg) ⇒ Object



58
59
60
61
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 58

def notify(msg)
  log msg
  puts msg.yellow
end

#reset_logObject



67
68
69
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 67

def reset_log
  %x{echo "" > #{log_path}}
end

#run(cmd, dir: working_directory) ⇒ Object



13
14
15
16
17
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 13

def run(cmd, dir: working_directory)
  command = "cd #{dir} && #{cmd}"
  handle_output_for(command)
  shell_out(command)
end

#shell_out(command) ⇒ Object



19
20
21
22
23
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 19

def shell_out(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



53
54
55
56
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 53

def warn(msg)
  log msg
  puts msg.red
end