[Pkg-puppet-devel] [facter] 118/352: (FACT-159) Extract facter command line formatting
Stig Sandbeck Mathisen
ssm at debian.org
Sun Apr 6 22:21:37 UTC 2014
This is an automated email from the git hooks/post-receive script.
ssm pushed a commit to branch master
in repository facter.
commit f1bf77b9af4bb61c4597f19bfe443f6844e1b1fc
Author: Adrien Thebo <git at somethingsinistral.net>
Date: Mon Dec 16 14:03:10 2013 -0800
(FACT-159) Extract facter command line formatting
This extracts facter's command line formatting to a separate file and
calls out plaintext formatting as a specific case. This allows us a
clean way of removing the plaintext behavior as we move towards
structured facts.
---
lib/facter/application.rb | 41 ++++++++++++++---------------------------
lib/facter/util/formatter.rb | 38 ++++++++++++++++++++++++++++++++++++++
spec/unit/application_spec.rb | 32 ++++++++++++++++++++++++++++++++
3 files changed, 84 insertions(+), 27 deletions(-)
diff --git a/lib/facter/application.rb b/lib/facter/application.rb
index 94268c2..4300ac5 100644
--- a/lib/facter/application.rb
+++ b/lib/facter/application.rb
@@ -1,5 +1,6 @@
require 'optparse'
require 'facter'
+require 'facter/util/formatter'
module Facter
module Application
@@ -44,37 +45,21 @@ module Facter
# 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
+ output = nil
- # 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
+ if options[:yaml]
+ output = Facter::Util::Formatter.format_yaml(facts)
+ elsif options[:json]
+ output = Facter::Util::Formatter.format_json(facts)
+ elsif options[:plaintext]
+ output = Facter::Util::Formatter.format_plaintext(facts)
else
- facts.sort_by { |(name, value)| name }.each do |name,value|
- puts "#{name} => #{value}"
- end
+ output = Facter::Util::Formatter.format_plaintext(facts)
end
+ puts output
+ exit(0)
+
rescue => e
if options && options[:trace]
raise e
@@ -135,6 +120,8 @@ USAGE
opts.on("-j",
"--json",
"Emit facts in JSON format.") { |v| options[:json] = v }
+ opts.on("--plaintext",
+ "Emit facts in plaintext format.") { |v| options[:plaintext] = v }
opts.on("--trace",
"Enable backtraces.") { |v| options[:trace] = v }
opts.on("--external-dir DIR",
diff --git a/lib/facter/util/formatter.rb b/lib/facter/util/formatter.rb
new file mode 100644
index 0000000..55eb380
--- /dev/null
+++ b/lib/facter/util/formatter.rb
@@ -0,0 +1,38 @@
+require 'yaml'
+
+module Facter
+ module Util
+ module Formatter
+
+ def self.format_json(hash)
+ if Facter.json?
+ JSON.pretty_generate(hash)
+ else
+ raise "Cannot format facts as JSON; 'json' library is not present"
+ end
+ end
+
+ def self.format_yaml(hash)
+ YAML.dump(hash)
+ end
+
+ def self.format_plaintext(hash)
+ output = ''
+
+ # Print the value of a single fact, otherwise print a list sorted by fact
+ # name and separated by "=>"
+ if hash.length == 1
+ if value = hash.values.first
+ output = value
+ end
+ else
+ hash.sort_by { |(name, value)| name }.each do |name,value|
+ output << "#{name} => #{value}\n"
+ end
+ end
+
+ output
+ end
+ end
+ end
+end
diff --git a/spec/unit/application_spec.rb b/spec/unit/application_spec.rb
index 8cb9584..c881090 100644
--- a/spec/unit/application_spec.rb
+++ b/spec/unit/application_spec.rb
@@ -53,4 +53,36 @@ describe Facter::Application do
argv.should == ['uptime', 'virtual']
end
end
+
+ describe "formatting facts" do
+ before do
+ Facter.stubs(:to_hash)
+ Facter.stubs(:value)
+ Facter::Application.stubs(:puts)
+ end
+
+ it "delegates YAML formatting" do
+ Facter::Util::Formatter.expects(:format_yaml)
+ Facter::Application.stubs(:exit).with(0)
+ Facter::Application.run(['--yaml'])
+ end
+
+ it "delegates JSON formatting", :if => Facter.json? do
+ Facter::Util::Formatter.expects(:format_json)
+ Facter::Application.stubs(:exit).with(0)
+ Facter::Application.run(['--json'])
+ end
+
+ it "delegates plaintext formatting" do
+ Facter::Util::Formatter.expects(:format_plaintext)
+ Facter::Application.stubs(:exit).with(0)
+ Facter::Application.run(['--plaintext'])
+ end
+
+ it "defaults to plaintext" do
+ Facter::Util::Formatter.expects(:format_plaintext)
+ Facter::Application.stubs(:exit).with(0)
+ Facter::Application.run([])
+ end
+ end
end
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git
More information about the Pkg-puppet-devel
mailing list