summaryrefslogtreecommitdiffstats
path: root/lib/facter/application.rb
diff options
context:
space:
mode:
authorRein Henrichs <rein@puppetlabs.com>2010-08-17 16:22:43 -0700
committerRein Henrichs <rein@puppetlabs.com>2010-10-04 14:10:18 -0700
commitebcb81be7408cfb8bd2bcb86f98ae6a98a6c70a5 (patch)
tree503dc794cb185754eb2338496c7c04becacaa943 /lib/facter/application.rb
parent51bcebe38cab6088c901f1006339bbe40a36d161 (diff)
downloadfacter-ebcb81be7408cfb8bd2bcb86f98ae6a98a6c70a5.tar.gz
facter-ebcb81be7408cfb8bd2bcb86f98ae6a98a6c70a5.tar.xz
facter-ebcb81be7408cfb8bd2bcb86f98ae6a98a6c70a5.zip
[#4558] Refactor facter binary using optparse
Simplify the binary by moving all application specific code into a new Facter::Application module. This module is then refactored to use OptionParser and to simplify invocation logic, while maintaining existing behavior.
Diffstat (limited to 'lib/facter/application.rb')
-rw-r--r--lib/facter/application.rb99
1 files changed, 99 insertions, 0 deletions
diff --git a/lib/facter/application.rb b/lib/facter/application.rb
new file mode 100644
index 0000000..51dbd14
--- /dev/null
+++ b/lib/facter/application.rb
@@ -0,0 +1,99 @@
+module Facter
+ module Application
+ def self.run(argv)
+ require 'optparse'
+ require 'facter'
+
+ options = parse(argv)
+
+ # Accept fact names to return from the command line
+ names = argv
+
+ # Create the facts hash that is printed to standard out
+ if names.empty?
+ facts = Facter.to_hash
+ else
+ facts = {}
+ names.each { |name|
+ begin
+ facts[name] = Facter.value(name)
+ rescue => error
+ $stderr.puts "Could not retrieve #{name}: #{error}"
+ exit 10
+ end
+ }
+ end
+
+ # Print the facts as YAML and exit
+ if options[:yaml]
+ require 'yaml'
+ puts YAML.dump(facts)
+ exit(0)
+ end
+
+ # Print the value of a single fact, otherwise print a list sorted by fact
+ # name and separated by "=>"
+ if facts.length == 1
+ if value = facts.values.first
+ puts value
+ end
+ else
+ facts.sort_by{ |fact| fact.first }.each do |name,value|
+ puts "#{name} => #{value}"
+ end
+ end
+
+ rescue => e
+ $stderr.puts "Error: #{e}"
+ exit(12)
+ end
+
+ private
+
+ def self.parse(argv)
+ options = {}
+ OptionParser.new do |opts|
+ opts.on("-y", "--yaml") { |v| options[:yaml] = v }
+
+ opts.on("-d", "--debug") { |v| Facter.debugging(1) }
+ opts.on("-p", "--puppet") { |v| load_puppet }
+
+ opts.on_tail("-v", "--version") do
+ puts Facter.version
+ exit(0)
+ end
+
+ opts.on_tail("-h", "--help") do
+ begin
+ require 'rdoc/ri/ri_paths'
+ require 'rdoc/usage'
+ puts RDoc.usage
+ ensure
+ exit
+ end
+ end
+ end.parse!
+
+ options
+ rescue OptionParser::InvalidOption => e
+ $stderr.puts e.message
+ exit(12)
+ end
+
+ def self.load_puppet
+ require 'puppet'
+ Puppet.parse_config
+
+ # If you've set 'vardir' but not 'libdir' in your
+ # puppet.conf, then the hook to add libdir to $:
+ # won't get triggered. This makes sure that it's setup
+ # correctly.
+ unless $LOAD_PATH.include?(Puppet[:libdir])
+ $LOAD_PATH << Puppet[:libdir]
+ end
+ rescue LoadError => detail
+ $stderr.puts "Could not load Puppet: #{detail}"
+ end
+
+ end
+end