Class: Jarl::Docker::Ps

Inherits:
Object
  • Object
show all
Defined in:
lib/jarl/docker.rb

Overview

‘docker ps` info inspector

Constant Summary collapse

FIELDS =
[
  'CONTAINER ID', 'IMAGE', 'COMMAND', 'CREATED', 'STATUS', 'PORTS', 'NAMES'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePs

Returns a new instance of Ps.



154
155
156
157
158
159
160
161
# File 'lib/jarl/docker.rb', line 154

def initialize
  lines = `docker ps`.split("\n")
  @headers = lines.shift
  @data = lines
  @entries = lines.map do |line|
    FIELDS.map { |name| [name, field(line, name)] }.to_h
  end
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



152
153
154
# File 'lib/jarl/docker.rb', line 152

def entries
  @entries
end

Instance Method Details

#[](id) ⇒ Object



186
187
188
# File 'lib/jarl/docker.rb', line 186

def [](id)
  entries.find { |e| id == e['CONTAINER ID'] }
end

#field(line, name) ⇒ Object



182
183
184
# File 'lib/jarl/docker.rb', line 182

def field(line, name)
  line[field_i_range(name)].strip
end

#field_i_end(name) ⇒ nil, Integer

Returns right index for field or -1 if the field is rightmost.

Returns:

  • (nil, Integer)

    right index for field or -1 if the field is rightmost



171
172
173
174
175
176
# File 'lib/jarl/docker.rb', line 171

def field_i_end(name)
  i_start = field_i_start(name)
  r_headers = @headers[i_start..-1].sub(/^#{name}/, '')
  m = r_headers.match(/^(\s+)/)
  m ? i_start + name.size + m[1].size - 1 : -1
end

#field_i_range(name) ⇒ Object



178
179
180
# File 'lib/jarl/docker.rb', line 178

def field_i_range(name)
  field_i_start(name)..field_i_end(name)
end

#field_i_start(name) ⇒ Object



163
164
165
166
167
# File 'lib/jarl/docker.rb', line 163

def field_i_start(name)
  i_start = @headers.index(name)
  fail "Failed to find the field #{name} in docker ps" unless i_start
  i_start
end