Class: Gotsha::BashCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/gotsha/bash_command.rb

Constant Summary collapse

FORCE_OUTPUT_AFTER =
5
MARKER =
"__GOTSHA_EXIT__:"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout, status) ⇒ BashCommand

Returns a new instance of BashCommand.



54
55
56
57
# File 'lib/gotsha/bash_command.rb', line 54

def initialize(stdout, status)
  @stdout = stdout
  @status = status
end

Class Method Details

.run!(command) ⇒ Object

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gotsha/bash_command.rb', line 13

def self.run!(command)
  start_time = Time.now
  UserConfig.get(:verbose) && puts(command)

  stdout = +""
  exit_code = nil

  # This block is an ugly workaround to ensure the color output is stored both on Linux and Mac
  PTY.spawn("bash", "-c", "#{command}; printf \"\\n#{MARKER}%d\\n\" $?") do |r, _w, pid|
    r.each do |line|
      if line.start_with?(MARKER)
        exit_code = line.sub(MARKER, "").to_i
        next
      end
      puts line if UserConfig.get(:verbose) || Time.now - start_time > FORCE_OUTPUT_AFTER
      stdout << line
    end
  rescue Errno::EIO
    # PTY closes when process ends — safe to ignore
  ensure
    Process.wait(pid)
  end

  final_code = exit_code || $CHILD_STATUS.exitstatus
  status = Struct.new(:exitstatus) do
    def success?
      exitstatus.zero?
    end
  end.new(final_code)
  new(stdout, status)
end

.silent_run!(command) ⇒ Object

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/MethodLength



48
49
50
51
52
# File 'lib/gotsha/bash_command.rb', line 48

def self.silent_run!(command)
  return run!(command) if UserConfig.get(:verbose)

  run!("#{command} 2>&1")
end

Instance Method Details

#success?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/gotsha/bash_command.rb', line 59

def success?
  @status.success?
end

#text_outputObject



63
64
65
# File 'lib/gotsha/bash_command.rb', line 63

def text_output
  @stdout.to_s.strip
end