summaryrefslogtreecommitdiff
path: root/lib/chef/resource/macos_userdefaults.rb
blob: c0066c84b57c2a97913378558450e990079e7f2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#
# Copyright:: 2011-2018, Joshua Timberman
# Copyright:: 2018, Chef Software, Inc.
#
# 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

      introduced "14.0"

      description "Use the macos_userdefaults resource to manage the macOS user defaults"\
                  " system. The properties to the resource are passed to the defaults command"\
                  " and the parameters follow convention of the macOS command. See the defaults(1)"\
                  " man page for details on how the tool works."

      property :domain,
               String,
               description: "The domain the defaults belong to.",
               required: true

      property :global,
               [true, false],
               description: "Whether the domain is global.",
               default: false

      property :key,
               String,
               description: "The preference key."

      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,
               String,
               description: "Value type of the preference key.",
               default: ""

      property :user,
               String,
               description: "User for which to set the default."

      property :sudo,
               [true, false],
               description: "Set to true if the setting requires privileged access to modify.",
               default: false,
               desired_state: false

      property :is_set,
               [true, false],
               description: "",
               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

      load_current_value do |desired|
        drcmd = "defaults read '#{desired.domain}' "
        drcmd << "'#{desired.key}' " if desired.key
        shell_out_opts = {}
        shell_out_opts[:user] = desired.user unless desired.user.nil?
        vc = shell_out("#{drcmd} | grep -qx '#{desired.value}'", shell_out_opts)
        is_set vc.exitstatus == 0 ? true : false
      end

      action :write do
        description "Write the setting to the specified domain"

        unless current_value.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