Sha256: dd1bedca8a7981fff21d6771af6d3bf75ea530e9414e703e19bf9bdf8301ff6f

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

require 'rainbow'

module Gaptool
  module Helpers
    def self.info(nodes, parseable, grepable, short = false)
      if parseable && !short
        puts({ nodes: nodes }.to_json)
      elsif parseable
        puts({ nodes: nodes.map { |node| node.select { |k, _v| %w(role environment instance).include?(k) } } }.to_json)
      else
        nodes.each do |node|
          host = "#{node['role']}:#{node['environment']}:#{node['instance']}"
          if grepable && short
            puts host
          elsif !grepable
            puts Rainbow(host).green
          end
          next if short
          keys = node.keys.sort
          keys.each do |key|
            value = node[key]
            if grepable
              puts "#{host}|#{key}|#{value}"
            else
              value = Time.at(node[key].to_i) if key == 'launch_time'
              if key == keys.last
                puts "  ┖  #{Rainbow(key).cyan}: #{value}\n\n"
              else
                puts "  ┠  #{Rainbow(key).cyan}: #{value}"
              end
            end
          end
        end
      end
    end

    def self.error(message, opts = {})
      code = opts[:code] || 1
      color = opts[:color] || :red
      STDERR.puts(Rainbow(message).send(color))
      exit code
    end

    def self.split_attrs(attribute_list)
      opts = {}
      attribute_list.each do |attr_|
        key, value = attr_.split('=', 2)
        split = key.split('.')
        cur = opts
        split.each_with_index do |part, idx|
          if idx == split.size - 1
            # leaf, add the value
            cur[part] = value
          else
            cur[part] ||= {}
          end
          cur = cur[part]
        end
      end
      opts
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gaptool-client-0.8.0.pre.alpha7 lib/gaptool_client/helpers.rb
gaptool-client-0.8.0.pre.alpha6 lib/gaptool_client/helpers.rb
gaptool-client-0.8.0.pre.alpha5 lib/gaptool_client/helpers.rb