Sha256: e8654cfe2791f51cf344c13e623a77a92a77e6600577b61b1ae7ddc216122cfa

Contents?: true

Size: 1.71 KB

Versions: 12

Compression:

Stored size: 1.71 KB

Contents

# coding: utf-8
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

12 entries across 12 versions & 1 rubygems

Version Path
gaptool-client-0.8.2 lib/gaptool_client/helpers.rb
gaptool-client-0.8.1 lib/gaptool_client/helpers.rb
gaptool-client-0.8.0 lib/gaptool_client/helpers.rb
gaptool-client-0.8.0.pre.beta5 lib/gaptool_client/helpers.rb
gaptool-client-0.8.0.pre.beta4 lib/gaptool_client/helpers.rb
gaptool-client-0.8.0.pre.beta3 lib/gaptool_client/helpers.rb
gaptool-client-0.8.0.pre.beta2 lib/gaptool_client/helpers.rb
gaptool-client-0.8.0.pre.beta1 lib/gaptool_client/helpers.rb
gaptool-client-0.8.0.pre.beta lib/gaptool_client/helpers.rb
gaptool-client-0.8.0.pre.alpha10 lib/gaptool_client/helpers.rb
gaptool-client-0.8.0.pre.alpha9 lib/gaptool_client/helpers.rb
gaptool-client-0.8.0.pre.alpha8 lib/gaptool_client/helpers.rb