summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-07-06 23:48:13 -0700
committerTim Smith <tsmith84@gmail.com>2020-07-20 11:45:27 -0700
commit4d06b6dd29be5186dcd12b25956e3271811d600d (patch)
treef217efc607c4afe88a0431a441d669f190dc9ebc
parent7c007aab2f2a9713cf5414527d69af7efa30b6b9 (diff)
downloadchef-4d06b6dd29be5186dcd12b25956e3271811d600d.tar.gz
Parse out the data structure to build current_resource
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/resource/macos_userdefaults.rb35
1 files changed, 31 insertions, 4 deletions
diff --git a/lib/chef/resource/macos_userdefaults.rb b/lib/chef/resource/macos_userdefaults.rb
index c78523bf23..ffa5404b7a 100644
--- a/lib/chef/resource/macos_userdefaults.rb
+++ b/lib/chef/resource/macos_userdefaults.rb
@@ -122,17 +122,44 @@ class Chef
current_value_does_not_exist!
end
- value state.stdout.strip
+ # parse the output from the defaults command to ruby data type
+ # todo: This is a pretty basic implementation. PRs welcome ;)
+ case state.stdout[0]
+ when "{" # dict aka hash
+ # https://rubular.com/r/cBnFu1nttMdsXq
+ data = /^\s{2,}(.*)\s=\s(.*);/.match(state.stdout)
+ fail_if_unparsable(data, state.stdout)
+ value Hash[*data.captures]
+ when "(" # array
+ # https://rubular.com/r/TfYejXUJny11OG
+ data = /^\s{2,}(.*),?/.match(state.stdout)
+ fail_if_unparsable(data, state.stdout)
+ value data.captures
+ else # a string/int/float/bool
+ value state.stdout.strip
+ end
+ end
+
+ #
+ # If there were not matches raise a warning that we couldn't parse the output of the defaults
+ # CLI and return a nil current_resource by calling current_value_does_not_exist!
+ #
+ # @param [MatchData] match_data
+ # @param [String] stdout
+ #
+ def fail_if_unparsable(match_data, stdout)
+ return unless match_data.captures.empty?
+
+ Chef::Log.warn("Could not parse macos defaults CLI value data: #{stdout}.")
+ current_value_does_not_exist!
end
action :write do
description "Write the value to the specified domain/key."
converge_if_changed do
- # FIXME: this should use cmd directly as an array argument, but then the quoting
- # of individual args above needs to be removed as well.
cmd = defaults_write_cmd
- Chef::Log.debug("Updating defaults value by shelling out: #{cmd}")
+ Chef::Log.debug("Updating defaults value by shelling out: #{cmd.join(" ")}")
if new_resource.user.nil?
shell_out!(cmd)