Sha256: ef09f3e43a927a0928a32d48cc77f29a70b013a01c72925a2f947a59ef8dabd4

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

module Dockage
  module Docker
    module Parse
      class << self
        def parse_docker_ps(string)
          header = string.shift

          spaces = column_width = 0
          keys = {}
          header.chars.each_with_index do |char, i|
            if i == (header.size - 1) || (char !~ /\s/ && spaces > 1)
              keys.merge!(slice_column_from_string(header, i, column_width))
              column_width = 0
            end
            spaces = char =~ /\s/ ? spaces + 1 : 0
            column_width += 1
          end

          string.map do |container_string|
            container           = Hash[keys.map { |k, v| [k, container_string[v[:start]..v[:stop]].strip] }]
            container[:name]    = container[:names].to_s
                                                   .split(',')
                                                   .reject{ |v| v.include?('/') }
                                                   .first
            container[:running] = container[:status].downcase
                                                    .include?('up') ? true : false
            container
          end
        end

        def slice_column_from_string(string, index, column_width)
          start = index - column_width
          stop = index < string.length - 1 ? (index - 1) : -1
          header_key = string[start..stop].strip
                                          .downcase
                                          .gsub(/\s/, '_')
                                          .to_sym

          { header_key => { start: start, stop: stop } }
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dockage-0.1.2 lib/dockage/docker/parse.rb
dockage-0.1.1 lib/dockage/docker/parse.rb
dockage-0.1.0 lib/dockage/docker/parse.rb