Methods

Facter::Application

Public Class Methods

run(argv) click to toggle source
# File lib/facter/application.rb, line 3
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.
  unless names.empty?
    facts = {}
    names.each do |name|
      begin
        facts[name] = Facter.value(name)
      rescue => error
        $stderr.puts "Could not retrieve #{name}: #{error}"
        exit 10
      end
    end
  end

  # Print everything if they didn't ask for specific facts.
  facts ||= Facter.to_hash

  # Print the facts as YAML and exit
  if options[:yaml]
    require 'yaml'
    puts YAML.dump(facts)
    exit(0)
  end

  # Print the facts as JSON and exit
  if options[:json]
    begin
      require 'json'
      puts JSON.dump(facts)
      exit(0)
    rescue LoadError
      $stderr.puts "You do not have JSON support in your version of Ruby. JSON output disabled"
      exit(1)
    end
  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
  if options && options[:trace]
    raise e
  else
    $stderr.puts "Error: #{e}"
    exit(12)
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.