Facter::Application

Public Class Methods

run(argv) click to toggle source
    # File lib/facter/application.rb, line 3
 3:     def self.run(argv)
 4:       require 'optparse'
 5:       require 'facter'
 6: 
 7:       options = parse(argv)
 8: 
 9:       # Accept fact names to return from the command line
10:       names = argv
11: 
12:       # Create the facts hash that is printed to standard out.
13:       unless names.empty?
14:         facts = {}
15:         names.each do |name|
16:           begin
17:             facts[name] = Facter.value(name)
18:           rescue => error
19:             $stderr.puts "Could not retrieve #{name}: #{error}"
20:             exit 10
21:           end
22:         end
23:       end
24: 
25:       # Print everything if they didn't ask for specific facts.
26:       facts ||= Facter.to_hash
27: 
28:       # Print the facts as YAML and exit
29:       if options[:yaml]
30:         require 'yaml'
31:         puts YAML.dump(facts)
32:         exit(0)
33:       end
34: 
35:       # Print the facts as JSON and exit
36:       if options[:json]
37:         begin
38:           require 'rubygems'
39:           require 'json'
40:           puts JSON.dump(facts)
41:           exit(0)
42:         rescue LoadError
43:           $stderr.puts "You do not have JSON support in your version of Ruby. JSON output disabled"
44:           exit(1)
45:         end
46:       end
47: 
48:       # Print the value of a single fact, otherwise print a list sorted by fact
49:       # name and separated by "=>"
50:       if facts.length == 1
51:         if value = facts.values.first
52:           puts value
53:         end
54:       else
55:         facts.sort_by{ |fact| fact.first }.each do |name,value|
56:           puts "#{name} => #{value}"
57:         end
58:       end
59: 
60:     rescue => e
61:       if options && options[:trace]
62:         raise e
63:       else
64:         $stderr.puts "Error: #{e}"
65:         exit(12)
66:       end
67:     end

Private Class Methods

load_puppet() click to toggle source
     # File lib/facter/application.rb, line 107
107:     def self.load_puppet
108:       require 'puppet'
109:       Puppet.parse_config
110: 
111:       # If you've set 'vardir' but not 'libdir' in your
112:       # puppet.conf, then the hook to add libdir to $:
113:       # won't get triggered.  This makes sure that it's setup
114:       # correctly.
115:       unless $LOAD_PATH.include?(Puppet[:libdir])
116:         $LOAD_PATH << Puppet[:libdir]
117:       end
118:     rescue LoadError => detail
119:       $stderr.puts "Could not load Puppet: #{detail}"
120:     end
parse(argv) click to toggle source
     # File lib/facter/application.rb, line 71
 71:     def self.parse(argv)
 72:       options = {}
 73:       OptionParser.new do |opts|
 74:         opts.on("-y", "--yaml")   { |v| options[:yaml]   = v }
 75:         opts.on("-j", "--json")   { |v| options[:json]   = v }
 76:         opts.on(      "--trace")  { |v| options[:trace]  = v }
 77:         opts.on("-d", "--debug")  { |v| Facter.debugging(1) }
 78:         opts.on("-t", "--timing") { |v| Facter.timing(1) }
 79:         opts.on("-p", "--puppet") { |v| load_puppet }
 80: 
 81:         opts.on_tail("-v", "--version") do
 82:           puts Facter.version
 83:           exit(0)
 84:         end
 85: 
 86:         opts.on_tail("-h", "--help") do
 87:           begin
 88:             require 'rdoc/ri/ri_paths'
 89:             require 'rdoc/usage'
 90:             RDoc.usage # print usage and exit
 91:           rescue LoadError
 92:             $stderr.puts "No help available unless your RDoc has RDoc.usage"
 93:             exit(1)
 94:           rescue => e
 95:             $stderr.puts "fatal: #{e}"
 96:             exit(1)
 97:           end
 98:         end
 99:       end.parse!
100: 
101:       options
102:     rescue OptionParser::InvalidOption => e
103:       $stderr.puts e.message
104:       exit(12)
105:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.