summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2017-06-01 18:32:56 -0700
committerTim Smith <tsmith@chef.io>2017-06-01 21:59:28 -0700
commitb6b7511a3d35392bdd735d47d6d3e14099c648cd (patch)
tree1b32ced2b62650820e2e8eaf8bb6d9543eff63e1
parent9c46af258797298ac90f4080584ec912a9f722d4 (diff)
downloadohai-hardware_shellout.tar.gz
Only shellout to sw_vers in darwin hardware oncehardware_shellout
Avoid 2 extra shell_outs by just parsing the fields Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/ohai/plugins/darwin/hardware.rb22
-rw-r--r--spec/unit/plugins/darwin/hardware_spec.rb16
2 files changed, 16 insertions, 22 deletions
diff --git a/lib/ohai/plugins/darwin/hardware.rb b/lib/ohai/plugins/darwin/hardware.rb
index 5497f36a..c9c4bab0 100644
--- a/lib/ohai/plugins/darwin/hardware.rb
+++ b/lib/ohai/plugins/darwin/hardware.rb
@@ -46,16 +46,22 @@ Ohai.plugin(:Hardware) do
hw_hash[0]["_items"][0].delete("_name")
hardware.merge!(hw_hash[0]["_items"][0])
- {
- "operating_system" => "sw_vers -productName",
- "operating_system_version" => "sw_vers -productVersion",
- "build_version" => "sw_vers -buildVersion",
- "architecture" => "uname -m",
- }.each do |var, cmd|
- os_info = shell_out(cmd).stdout
- hardware[var] = os_info.strip unless os_info.nil?
+ # ProductName: Mac OS X
+ # ProductVersion: 10.12.5
+ # BuildVersion: 16F73
+ shell_out("sw_vers").stdout.lines.each do |line|
+ case line
+ when /^ProductName:\s*(.*)$/
+ hardware["operating_system"] = Regexp.last_match[1].strip
+ when /^ProductVersion:\s*(.*)$/
+ hardware["operating_system_version"] = Regexp.last_match[1].strip
+ when /^BuildVersion:\s*(.*)$/
+ hardware["build_version"] = Regexp.last_match[1].strip
+ end
end
+ hardware["architecture"] = shell_out("uname -m").stdout.strip
+
# Storage queries
storage = []
storage_hash = system_profiler("SPStorageDataType")
diff --git a/spec/unit/plugins/darwin/hardware_spec.rb b/spec/unit/plugins/darwin/hardware_spec.rb
index 3f0f6b0d..f8c8e1d5 100644
--- a/spec/unit/plugins/darwin/hardware_spec.rb
+++ b/spec/unit/plugins/darwin/hardware_spec.rb
@@ -31,21 +31,9 @@ describe Ohai::System, "Darwin hardware plugin", :unix_only do
)
allow(plugin).to receive(:shell_out).with(
- "sw_vers -productName"
+ "sw_vers"
).and_return(
- mock_shell_out(0, "Mac OS X", "")
- )
-
- allow(plugin).to receive(:shell_out).with(
- "sw_vers -productVersion"
- ).and_return(
- mock_shell_out(0, "10.12", "")
- )
-
- allow(plugin).to receive(:shell_out).with(
- "sw_vers -buildVersion"
- ).and_return(
- mock_shell_out(0, "16A239j", "")
+ mock_shell_out(0, "ProductName: Mac OS X\nProductVersion: 10.12\nBuildVersion: 16A239j", "")
)
allow(plugin).to receive(:shell_out).with(