Class: Enviera::Subcommands::Hierarchy
- Inherits:
-
Enviera::Subcommand
- Object
- Enviera::Subcommand
- Enviera::Subcommands::Hierarchy
- Defined in:
- lib/enviera/subcommands/hierarchy.rb
Class Method Summary collapse
Methods inherited from Enviera::Subcommand
all_options, attach_option, find, hidden?, load_config_file, parse, prettyname
Class Method Details
.description ⇒ Object
38 39 40 |
# File 'lib/enviera/subcommands/hierarchy.rb', line 38 def self.description "display the hierarchy in use with the given environment" end |
.execute ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/enviera/subcommands/hierarchy.rb', line 59 def self.execute begin environment = JSON.parse(Enviera::Options[:input_data]) rescue raise "Unable to parse input as valid JSON. Please ensure structure is proper." end hiera_config = Hiera::Config.load(Enviera::Options[:hiera_config]) hiera= Hiera.new(:config=>hiera_config) sources = [] hiera.config[:backends].each do |backend| sources.push("backend: "+backend) Hiera::Backend.datasources(environment) { |source| # Presently I'm only handling 'yaml' extensions. I hope someday this works for other stuff # But I haven't figured out a smart way to do this with the tools available # And it's not urgent for me sourcefile = Hiera::Backend.datafile(backend,environment,source,"yaml") or next sources.push(sourcefile) } end sources = {'hierarchy' => sources} case Enviera::Options[:output] when "block" sources when "json" sources.to_json when "yaml" sources.to_yaml else sources.to_yaml end end |
.options ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/enviera/subcommands/hierarchy.rb', line 13 def self. [{:name => :string, :description => "Source input is a json string provided as an argument", :short => 's', :type => :string}, {:name => :file, :description => "Source input is a regular json file", :short => 'f', :type => :string}, {:name => :stdin, :description => "Source input is taken from stdin (json string)", :short => :none}, {:name => :hiera_config, :description => "Path to your hiera config yaml file", :short => 'c', :type => :string, :default => '/etc/puppet/hiera.yaml'}, {:name => :output, :description => "What format to output in. [json|yaml|block]", :short => 'o', :type => :string, :default => 'yaml'} ] end |
.validate(options) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/enviera/subcommands/hierarchy.rb', line 42 def self.validate sources = [:string, :file, :stdin].collect {|x| x if [x]}.compact Trollop::die "You must specify a source (I.E. -s '{}')" if sources.count.zero? Trollop::die "You can only specify one of (#{sources.join(', ')})" if sources.count > 1 [:source] = sources.first [:input_data] = case [:source] when :stdin STDIN.read when :string [:string] when :file File.read [:file] end end |