Class: GTA::Commander

Inherits:
Object
  • Object
show all
Defined in:
lib/gta/commander.rb

Defined Under Namespace

Classes: CommandFailure

Constant Summary collapse

LINE_LENGTH =
80

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#commandObject (readonly)

Returns the value of attribute command.



3
4
5
# File 'lib/gta/commander.rb', line 3

def command
  @command
end

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



3
4
5
# File 'lib/gta/commander.rb', line 3

def exit_status
  @exit_status
end

#outputObject (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

Raises:



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.message
    write_failure(e.message)
    write_failure(e.backtrace)
  end
  raise CommandFailure, "FAILED! #{command}" if should_raise
end

#loggerObject



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

#performObject



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_resetObject



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_commandObject



82
83
84
85
86
87
# File 'lib/gta/commander.rb', line 82

def write_command
  message = normalize_output(" GTA: #{command}")
  puts "#{ANSI.black_on_white}#{message}#{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(message = command)
  message = normalize_output(" Command failed: #{message}")
  puts "#{ANSI.red_on_black}#{message}#{ANSI.ansi}"
  write_to_log(message)
  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)
  message = normalize_output(" #{line}")
  puts "#{ANSI.white_on_black}#{message}"
  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