summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xbin/facter132
-rw-r--r--lib/facter/application.rb99
2 files changed, 108 insertions, 123 deletions
diff --git a/bin/facter b/bin/facter
index a6cb717..e9dfc7c 100755
--- a/bin/facter
+++ b/bin/facter
@@ -18,11 +18,8 @@
#
# = Options
#
-# debug::
-# Enable debugging.
-#
-# help::
-# Print this help message
+# yaml::
+# Emit facts in YAML format.
#
# puppet::
# Load the Puppet libraries, thus allowing Facter to load Puppet-specific facts.
@@ -30,8 +27,11 @@
# version::
# Print the version and exit.
#
-# yaml::
-# Emit facts in YAML format.
+# help::
+# Print this help message.
+#
+# debug::
+# Enable debugging.
#
# = Example
#
@@ -46,120 +46,6 @@
# Copyright (c) 2006 Reductive Labs, LLC
# Licensed under the GNU Public License
-require 'getoptlong'
-require 'facter'
-
-$haveusage = true
-
-begin
- require 'rdoc/ri/ri_paths'
- require 'rdoc/usage'
-rescue Exception
- $haveusage = false
-end
-
-def 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
-end
-
-$debug = 0
-
-config = nil
-
-result = GetoptLong.new(
- [ "--version", "-v", GetoptLong::NO_ARGUMENT ],
- [ "--help", "-h", GetoptLong::NO_ARGUMENT ],
- [ "--debug", "-d", GetoptLong::NO_ARGUMENT ],
- [ "--yaml", "-y", GetoptLong::NO_ARGUMENT ],
- [ "--config", "-c", GetoptLong::REQUIRED_ARGUMENT ],
- [ "--puppet", "-p", GetoptLong::NO_ARGUMENT ]
-)
-
-options = {
- :yaml => false
-}
-
-begin
- result.each { |opt,arg|
- case opt
- when "--version"
- puts "%s" % Facter.version
- exit
- when "--puppet"
- begin
- load_puppet()
- rescue LoadError => detail
- $stderr.puts "Could not load Puppet: %s" % detail
- end
- when "--yaml"
- options[:yaml] = true
- when "--debug"
- Facter.debugging(1)
- when "--help"
- if $haveusage
- RDoc::usage && exit
- else
- puts "No help available unless you have RDoc::usage installed"
- exit
- end
- else
- $stderr.puts "Invalid option '#{opt}'"
- exit(12)
- end
- }
-rescue
- exit(12)
-end
-
-names = []
-
-unless config.nil?
- File.open(config) { |file|
- names = file.readlines.collect { |line|
- line.chomp
- }
- }
-end
-
-ARGV.each { |item|
- names.push item
-}
-
-facts = Facter.to_hash
-
-unless names.empty?
- facts = {}
- names.each { |name|
- begin
- facts[name] = Facter.value(name)
- rescue => error
- STDERR.puts "Could not retrieve %s: #{error}" % name
- exit 10
- end
- }
-end
-
-if options[:yaml]
- require 'yaml'
- puts YAML.dump(facts)
- exit(0)
-end
+require 'facter/application'
-facts.sort { |a, b| a[0].to_s <=> b[0].to_s }.each { |name,value|
- if facts.length == 1
- unless value.nil?
- puts value
- end
- else
- puts "%s => %s" % [name,value]
- end
-}
+Facter::Application.run(ARGV)
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