Merge pull request #216 from microsoft/feature/plist-dicts
Implement basic dict support for plist resource
This commit is contained in:
Коммит
cfbcfcdef4
|
@ -3,6 +3,9 @@ module MacOS
|
|||
def convert_to_data_type_from_string(type, value)
|
||||
case type
|
||||
when 'boolean'
|
||||
# Since we've determined this is a boolean data type, we can assume that:
|
||||
# If the value as an int is 1, return true
|
||||
# If the value as an int is 0 (not 1), return false
|
||||
value.to_i == 1
|
||||
when 'integer'
|
||||
value.to_i
|
||||
|
@ -10,6 +13,8 @@ module MacOS
|
|||
value.to_f
|
||||
when 'string'
|
||||
value
|
||||
when 'dictionary'
|
||||
value
|
||||
when nil
|
||||
''
|
||||
else
|
||||
|
@ -49,8 +54,9 @@ module MacOS
|
|||
end
|
||||
end
|
||||
|
||||
def print_entry_value(entry, path)
|
||||
cmd = shell_out(plistbuddy_command(:print, entry, path))
|
||||
def entry_in_plist?(entry, path)
|
||||
print_entry = plistbuddy_command :print, entry, path
|
||||
cmd = shell_out print_entry
|
||||
cmd.exitstatus == 0
|
||||
end
|
||||
|
||||
|
@ -61,23 +67,36 @@ module MacOS
|
|||
end
|
||||
|
||||
def plistbuddy_command(subcommand, entry, path, value = nil)
|
||||
sep = ' '
|
||||
arg = case subcommand.to_s
|
||||
when 'add'
|
||||
type_to_commandline_string(value)
|
||||
when 'print'
|
||||
''
|
||||
when 'set'
|
||||
if value.class == Hash
|
||||
sep = ':'
|
||||
value.map { |k, v| "#{k} #{v}" }
|
||||
else
|
||||
value
|
||||
end
|
||||
else
|
||||
value
|
||||
''
|
||||
end
|
||||
entry_with_arg = ["\"#{entry}\"", arg].join(' ').strip
|
||||
entry_with_arg = ["\"#{entry}\"", arg].join(sep).strip
|
||||
subcommand = "#{subcommand.capitalize} :#{entry_with_arg}"
|
||||
[plistbuddy_executable, '-c', "\'#{subcommand}\'", "\"#{path}\""].join(' ')
|
||||
end
|
||||
|
||||
def setting_from_plist(entry, path)
|
||||
defaults_read_type_output = shell_out(defaults_executable, 'read-type', path, entry).stdout
|
||||
defaults_read_output = shell_out(defaults_executable, 'read', path, entry).stdout
|
||||
{ key_type: defaults_read_type_output.split.last, key_value: defaults_read_output.strip }
|
||||
data_type = defaults_read_type_output.split.last
|
||||
|
||||
if value.class == Hash
|
||||
plutil_output = shell_out(plutil_executable, '-extract', entry, 'xml1', '-o', '-', path).stdout.chomp
|
||||
{ key_type: data_type, key_value: Plist.parse_xml(plutil_output) }
|
||||
else
|
||||
defaults_read_output = shell_out(defaults_executable, 'read', path, entry).stdout
|
||||
{ key_type: data_type, key_value: defaults_read_output.strip }
|
||||
end
|
||||
end
|
||||
|
||||
def plutil_format_map
|
||||
|
@ -89,6 +108,10 @@ module MacOS
|
|||
|
||||
private
|
||||
|
||||
def plutil_executable
|
||||
'/usr/bin/plutil'
|
||||
end
|
||||
|
||||
def defaults_executable
|
||||
'/usr/bin/defaults'
|
||||
end
|
||||
|
|
|
@ -2,7 +2,7 @@ resource_name :plist
|
|||
|
||||
property :path, String, name_property: true, desired_state: true
|
||||
property :entry, String, desired_state: true
|
||||
property :value, [TrueClass, FalseClass, String, Integer, Float], desired_state: true
|
||||
property :value, [TrueClass, FalseClass, String, Integer, Float, Hash], desired_state: true
|
||||
property :encoding, String, desired_state: true, default: 'binary'
|
||||
property :owner, String, desired_state: true, default: 'root'
|
||||
property :group, String, desired_state: true, default: 'wheel'
|
||||
|
@ -10,7 +10,7 @@ property :mode, [String, Integer]
|
|||
|
||||
load_current_value do |desired|
|
||||
current_value_does_not_exist! unless ::File.exist? desired.path
|
||||
entry desired.entry if print_entry_value desired.entry, desired.path
|
||||
entry desired.entry if entry_in_plist? desired.entry, desired.path
|
||||
|
||||
setting = setting_from_plist desired.entry, desired.path
|
||||
value convert_to_data_type_from_string(setting[:key_type], setting[:key_value])
|
||||
|
@ -61,7 +61,7 @@ action :set do
|
|||
Chef::Application.fatal!(
|
||||
"Option encoding must be equal to one of: #{plutil_format_map.keys}! You passed \"#{new_resource.encoding}\"."
|
||||
) unless plutil_format_map.key?(new_resource.encoding)
|
||||
execute ['/usr/bin/plutil', '-convert', plutil_format_map[new_resource.encoding], new_resource.path] do
|
||||
execute [plutil_executable, '-convert', plutil_format_map[new_resource.encoding], new_resource.path] do
|
||||
action :run
|
||||
end
|
||||
end
|
||||
|
|
|
@ -23,6 +23,10 @@ describe MacOS::PlistHelpers, '#plist_command' do
|
|||
it 'the print command is formatted properly' do
|
||||
expect(plistbuddy_command(:print, 'QuxEntry', 'path/to/file.plist')).to eq "/usr/libexec/PlistBuddy -c 'Print :\"QuxEntry\"' \"path/to/file.plist\""
|
||||
end
|
||||
|
||||
it 'the command to set a dictionary data type is formatted properly' do
|
||||
expect(plistbuddy_command(:set, 'AppleFirstWeekday', 'path/to/file.plist', gregorian: 4)).to eq "/usr/libexec/PlistBuddy -c 'Set :\"AppleFirstWeekday\":gregorian 4' \"path/to/file.plist\""
|
||||
end
|
||||
end
|
||||
|
||||
context 'The value provided contains spaces' do
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe 'setting the first day of the week to monday' do
|
||||
step_into :plist
|
||||
|
||||
platform 'mac_os_x', 10.14
|
||||
|
||||
recipe do
|
||||
plist 'make Monday the first DOW' do
|
||||
path '/Users/somebody/Library/Preferences/.GlobalPreferences.plist'
|
||||
entry 'AppleFirstWeekday'
|
||||
value(gregorian: 4)
|
||||
owner 'somebody'
|
||||
group 'staff'
|
||||
end
|
||||
end
|
||||
|
||||
it { is_expected.to run_execute("/usr/libexec/PlistBuddy -c 'Set :\"AppleFirstWeekday\":gregorian 4' \"/Users/somebody/Library/Preferences/.GlobalPreferences.plist\"") }
|
||||
end
|
Загрузка…
Ссылка в новой задаче