Module: DockerOutput

Defined in:
lib/docker_output.rb

Class Method Summary collapse

Class Method Details

.parse(chunk) ⇒ Object



5
6
7
8
9
# File 'lib/docker_output.rb', line 5

def self.parse(chunk)
  chunk.each_line do |line|
    yield self.parse_line(line)
  end
end

.parse_line(line) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/docker_output.rb', line 11

def self.parse_line(line)
  begin
    json = JSON.parse(line.strip)
  rescue JSON::ParserError
    return line
  end

  # Skip progress and aux as they are covered by other status messages
  return '' if json['progress'] && json['status']
  return '' if json['aux']

  # Return error flag as a second result
  return [json['error'], true] if json['error']

  return json['stream'] if json['stream']

  if json['status']
    if json['id']
      return json['id'] + ': ' + json['status'] + "\n"
    else
      return json['status'] + "\n"
    end
  end

  return line # Nothing else matches
end


38
39
40
41
42
43
44
45
46
# File 'lib/docker_output.rb', line 38

def self.print(chunk)
  self.parse(chunk) do |text, is_error|
    if is_error
      $stdout.print text.red + "\n"
      raise text
    end
    $stdout.print text unless text.empty?
  end
end