diff options
author | Tim Smith <tsmith@chef.io> | 2018-02-20 09:46:13 -0800 |
---|---|---|
committer | Tim Smith <tsmith@chef.io> | 2018-02-20 09:46:13 -0800 |
commit | c030d48be75a927b015a5c93f310ed7a1b0e2dfc (patch) | |
tree | 1090536e1121aab9e97af6331ae5bccce656c582 /lib/chef | |
parent | 96a3ea931e21f2f579e4322457c5b88c0f18ba14 (diff) | |
download | chef-c030d48be75a927b015a5c93f310ed7a1b0e2dfc.tar.gz |
Add macos_user_defaults resource from mac_os_x cookbook
Brought in as-is from the upstream cookbook
Signed-off-by: Tim Smith <tsmith@chef.io>
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/resource/macos_userdefaults.rb | 82 | ||||
-rw-r--r-- | lib/chef/resources.rb | 1 |
2 files changed, 83 insertions, 0 deletions
diff --git a/lib/chef/resource/macos_userdefaults.rb b/lib/chef/resource/macos_userdefaults.rb new file mode 100644 index 0000000000..dbc71bf768 --- /dev/null +++ b/lib/chef/resource/macos_userdefaults.rb @@ -0,0 +1,82 @@ +# +# Copyright:: 2011-2018, Joshua Timberman +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require "chef/resource" + +class Chef + class Resource + class MacosUserDefaults < Chef::Resource + # align with apple's marketing department + resource_name :macos_userdefaults + provides :mac_os_x_userdefaults + provides :macos_userdefaults + + property :domain, String, required: true + property :global, [true, false], default: false + property :key, String + property :value, [Integer, Float, String, true, false, Hash, Array], required: true + property :type, String, default: "" + property :user, String + property :sudo, [true, false], default: false + property :is_set, [true, false], default: false + + action :write do + @userdefaults = Chef::Resource.resource_for_node(:macos_userdefaults, node).new(new_resource.name) + @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) + + is_set = vc.exitstatus == 0 ? true : false + @userdefaults.is_set(is_set) + + unless @userdefaults.is_set + cmd = ["defaults write"] + cmd.unshift("sudo") if new_resource.sudo + + cmd << if new_resource.global + "NSGlobalDomain" + else + "'#{new_resource.domain}'" + end + + cmd << "'#{new_resource.key}'" if new_resource.key + value = new_resource.value + type = new_resource.type.empty? ? value_type(value) : new_resource.type + # creates a string of Key1 Value1 Key2 Value2... + value = value.map { |k, v| "\"#{k}\" \"#{v}\"" }.join(" ") if type == "dict" + if type == "array" + value = value.join("' '") + value = "'#{value}'" + end + cmd << "-#{type}" if type + cmd << value + + declare_resource(:execute, cmd.join(" ")) do + user new_resource.user unless new_resource.user.nil? + end + end + end + end + end +end diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb index 60f3ad51f0..c10bf98b2f 100644 --- a/lib/chef/resources.rb +++ b/lib/chef/resources.rb @@ -49,6 +49,7 @@ require "chef/resource/launchd" require "chef/resource/link" require "chef/resource/log" require "chef/resource/macports_package" +require "chef/resource/macos_userdefaults" require "chef/resource/mdadm" require "chef/resource/mount" require "chef/resource/ohai" |