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