Class: GTA::Commander
- Inherits:
-
Object
- Object
- GTA::Commander
- Defined in:
- lib/gta/commander.rb
Defined Under Namespace
Classes: CommandFailure
Constant Summary collapse
- LINE_LENGTH =
80
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#exit_status ⇒ Object
readonly
Returns the value of attribute exit_status.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
-
#_perform(should_raise) ⇒ Object
————–.
- #handle_failure(should_raise, e = nil) ⇒ Object
-
#initialize(command = nil) ⇒ Commander
constructor
A new instance of Commander.
- #logger ⇒ Object
- #normalize_output(output) ⇒ Object
- #perform ⇒ Object
- #perform! ⇒ Object
- #puts_reset ⇒ Object
- #run_command(should_raise) ⇒ Object
- #write_command ⇒ Object
- #write_failure(message = command) ⇒ Object
- #write_output(line) ⇒ Object
- #write_to_log(response) ⇒ Object
Constructor Details
#initialize(command = nil) ⇒ Commander
Returns a new instance of Commander.
7 8 9 10 |
# File 'lib/gta/commander.rb', line 7 def initialize(command=nil) @output = [] @command = command end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
3 4 5 |
# File 'lib/gta/commander.rb', line 3 def command @command end |
#exit_status ⇒ Object (readonly)
Returns the value of attribute exit_status.
3 4 5 |
# File 'lib/gta/commander.rb', line 3 def exit_status @exit_status end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
3 4 5 |
# File 'lib/gta/commander.rb', line 3 def output @output end |
Instance Method Details
#_perform(should_raise) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/gta/commander.rb', line 35 def _perform(should_raise) run_command(should_raise) puts_reset write_to_log(output.join) handle_failure(should_raise) unless exit_status && exit_status.success? output.join end |
#handle_failure(should_raise, e = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/gta/commander.rb', line 65 def handle_failure(should_raise, e=nil) write_failure if e output << e. write_failure(e.) write_failure(e.backtrace) end raise CommandFailure, "FAILED! #{command}" if should_raise end |
#logger ⇒ Object
93 94 95 |
# File 'lib/gta/commander.rb', line 93 def logger @logger ||= FileLogger.new end |
#normalize_output(output) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/gta/commander.rb', line 75 def normalize_output(output) normalized = output.gsub("\n",'') normalized += " "*(LINE_LENGTH-normalized.size-1) if normalized.size < LINE_LENGTH normalized += ' ' normalized end |
#perform ⇒ Object
12 13 14 |
# File 'lib/gta/commander.rb', line 12 def perform _perform(false) end |
#perform! ⇒ Object
16 17 18 |
# File 'lib/gta/commander.rb', line 16 def perform! _perform(true) end |
#puts_reset ⇒ Object
89 90 91 |
# File 'lib/gta/commander.rb', line 89 def puts_reset puts "#{ANSI.ansi}" end |
#run_command(should_raise) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/gta/commander.rb', line 45 def run_command(should_raise) write_command Open3.popen3(command) do |stdin, stdout, stderr, process_status| stderr.sync = true stdout.sync = true while (line = stderr.gets) write_output line end while (line = stdout.gets) write_output line end @exit_status = process_status.value end rescue Errno::ENOENT => e handle_failure(should_raise, e) end |
#write_command ⇒ Object
82 83 84 85 86 87 |
# File 'lib/gta/commander.rb', line 82 def write_command = normalize_output(" GTA: #{command}") puts "#{ANSI.black_on_white}#{}#{ANSI.ansi}" write_to_log(command) write_to_log("-"*LINE_LENGTH) end |
#write_failure(message = command) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/gta/commander.rb', line 26 def write_failure( = command) = normalize_output(" Command failed: #{}") puts "#{ANSI.red_on_black}#{}#{ANSI.ansi}" write_to_log() write_to_log("-"*LINE_LENGTH) end |
#write_output(line) ⇒ Object
20 21 22 23 24 |
# File 'lib/gta/commander.rb', line 20 def write_output(line) = normalize_output(" #{line}") puts "#{ANSI.white_on_black}#{}" output << line end |
#write_to_log(response) ⇒ Object
97 98 99 |
# File 'lib/gta/commander.rb', line 97 def write_to_log(response) logger.write(response) end |