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)


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

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



59
60
61
62
63
64
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 59

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

#get_number_list_for_question(question) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 42

def get_number_list_for_question(question)
  warn question 
  answer = STDIN.gets.strip

  answer.split(',').map do |term|
    if term.include?('-')
      (term.split('-')[0].to_i..term.split('-')[1].to_i).to_a
    else
      term.to_i
    end
  end.flatten
end

#handle_output_for(cmd) ⇒ Object



84
85
86
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 84

def handle_output_for(cmd)
  log(cmd)
end

#log(msg) ⇒ Object



76
77
78
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 76

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

#notify(msg) ⇒ Object



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

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

#reset_logObject



80
81
82
# File 'lib/sfb_scripts/shells/shell_runner.rb', line 80

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



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

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