From b6b7511a3d35392bdd735d47d6d3e14099c648cd Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Thu, 1 Jun 2017 18:32:56 -0700 Subject: Only shellout to sw_vers in darwin hardware once Avoid 2 extra shell_outs by just parsing the fields Signed-off-by: Tim Smith --- lib/ohai/plugins/darwin/hardware.rb | 22 ++++++++++++++-------- spec/unit/plugins/darwin/hardware_spec.rb | 16 ++-------------- 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( -- cgit v1.2.1