diff options
author | Tim Smith <tsmith@chef.io> | 2018-02-20 15:11:18 -0800 |
---|---|---|
committer | Tim Smith <tsmith@chef.io> | 2018-02-20 15:11:18 -0800 |
commit | 89a2601cd37ff2f5a00fced46d767515210561cd (patch) | |
tree | dd78a7e4baa40aeb287b5d32bc7f0d4fe7fe0ba7 | |
parent | 1882b513f8f85e5e27ff08d440956a4db72256b6 (diff) | |
download | chef-89a2601cd37ff2f5a00fced46d767515210561cd.tar.gz |
Properly coerce the various boolean inputs for state checking
We should coerce the value right on the property instead of creating another value we use when fetching state.
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r-- | lib/chef/resource/macos_userdefaults.rb | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/lib/chef/resource/macos_userdefaults.rb b/lib/chef/resource/macos_userdefaults.rb index 7fc1da28df..435e3682b3 100644 --- a/lib/chef/resource/macos_userdefaults.rb +++ b/lib/chef/resource/macos_userdefaults.rb @@ -49,6 +49,7 @@ class Chef property :value, [Integer, Float, String, true, false, Hash, Array], description: "The value of the key.", + coerce: proc { |v| coerce_booleans(v) }, required: true property :type, @@ -72,6 +73,15 @@ class Chef default: false, desired_state: false + # coerce various ways of representing a boolean into either 0 (false) or 1 (true) + # which is what the defaults CLI expects. Why? Well defaults itself accepts a few + # different formats, but when you do a read command it all comes back as 1 or 0. + def coerce_booleans(val) + return 1 if [true, "TRUE", "1", "true", "YES", "yes"].include?(val) + return 0 if [false, "FALSE", "0", "false", "NO", "no"].include?(val) + val + end + action :write do description "Write the setting to the specified domain" @@ -79,14 +89,12 @@ class Chef @userdefaults.key(new_resource.key) @userdefaults.domain(new_resource.domain) Chef::Log.debug("Checking #{new_resource.domain} value") - truefalse = 1 if [true, "TRUE", "1", "true", "YES", "yes"].include?(new_resource.value) - truefalse = 0 if [false, "FALSE", "0", "false", "NO", "no"].include?(new_resource.value) drcmd = "defaults read '#{new_resource.domain}' " drcmd << "'#{new_resource.key}' " if new_resource.key shell_out_opts = {} shell_out_opts[:user] = new_resource.user unless new_resource.user.nil? - vc = shell_out("#{drcmd} | grep -qx '#{truefalse || new_resource.value}'", shell_out_opts) + vc = shell_out("#{drcmd} | grep -qx '#{new_resource.value}'", shell_out_opts) is_set = vc.exitstatus == 0 ? true : false @userdefaults.is_set(is_set) |