summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2018-02-20 09:46:13 -0800
committerTim Smith <tsmith@chef.io>2018-02-20 09:46:13 -0800
commitc030d48be75a927b015a5c93f310ed7a1b0e2dfc (patch)
tree1090536e1121aab9e97af6331ae5bccce656c582
parent96a3ea931e21f2f579e4322457c5b88c0f18ba14 (diff)
downloadchef-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>
-rw-r--r--lib/chef/resource/macos_userdefaults.rb82
-rw-r--r--lib/chef/resources.rb1
-rw-r--r--spec/unit/resource/macos_user_defaults_spec.rb31
3 files changed, 114 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"
diff --git a/spec/unit/resource/macos_user_defaults_spec.rb b/spec/unit/resource/macos_user_defaults_spec.rb
new file mode 100644
index 0000000000..8787cd0b9a
--- /dev/null
+++ b/spec/unit/resource/macos_user_defaults_spec.rb
@@ -0,0 +1,31 @@
+#
+# Copyright:: Copyright 2018, Chef Software, Inc.
+# License:: Apache License, Version 2.0
+#
+# 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 "spec_helper"
+
+describe Chef::Resource::MacosUserDefaults do
+
+ let(:resource) { Chef::Resource::MacosUserDefaults.new("foo") }
+
+ it "has a resource name of :macos_userdefaults" do
+ expect(resource.resource_name).to eql(:macos_userdefaults)
+ end
+
+ it "has a default action of install" do
+ expect(resource.action).to eql([:write])
+ end
+end