class Enc::Cli

Attributes

found[R]
node_name[R]
options[RW]

Public Class Methods

new(argv) click to toggle source
# File lib/enc/cli.rb, line 9
def initialize(argv)
  @options = { config_file: '/etc/puppet/enc.yaml', cli_nodes: [], fail: false }

  optparse.parse!(argv)

  if (@node_name = argv[0]).nil?
    puts "ERROR: Didn't specify a node name to look up!"
    puts ''
    puts optparse.to_s
    exit 1
  end

  @found = find
end

Private Instance Methods

cli_nodes() click to toggle source
# File lib/enc/cli.rb, line 64
def cli_nodes
  return [] if options[:cli_nodes].empty?
  collect_nodes(options[:cli_nodes])
end
collect_nodes(nodes) click to toggle source
# File lib/enc/cli.rb, line 53
def collect_nodes(nodes)
  collection = []

  nodes.each do |node|
    collection << node if File.file?(node)
    collection += Dir["#{node}/*.yaml"].sort if File.directory?(node)
  end

  collection
end
config() click to toggle source
# File lib/enc/cli.rb, line 69
def config
  return {} unless File.exist?(config_file)
  Hash(YAML.load_file(config_file))
end
config_file() click to toggle source
# File lib/enc/cli.rb, line 49
def config_file
  options[:config_file]
end
config_nodes() click to toggle source
# File lib/enc/cli.rb, line 74
def config_nodes
  return [] unless config.include?('nodes')
  collect_nodes(Array(config['nodes']))
end
find() click to toggle source
# File lib/enc/cli.rb, line 26
def find
  Node.lookup(node_name, nodes) unless nodes.empty?
end
nodes() click to toggle source
# File lib/enc/cli.rb, line 79
def nodes
  fail('No node files specified!') if config['nodes'].nil? && cli_nodes.empty?

  cli_nodes + config_nodes
end
optparse() click to toggle source
# File lib/enc/cli.rb, line 30
def optparse
  OptionParser.new do |opt|
    opt.banner = "Usage: #{$PROGRAM_NAME} <options> <hostname>"
    opt.separator ''
    opt.separator 'Available options:'
    opt.on('-c', '--config [path to configuration file]', String, 'Path to configuration file (Default: /etc/puppet/enc.yaml).') { |c| options[:config_file] = c if c }
    opt.on('-n', '--nodes [path to data file]', String, 'Path to a data file to add to the end of the "nodes" array') { |n| options[:cli_nodes] << n if n }
    opt.on('-f', '--fail', 'Fail if no nodes are found.') { |f| options[:fail] = f }
    opt.on('-v', '--version', 'Print the version') do
      puts Enc::VERSION
      exit 0
    end
    opt.on_tail('-h', '--help', 'Show this message') do
      puts opt
      exit 0
    end
  end
end