Skip to content
This repository has been archived by the owner on May 22, 2018. It is now read-only.

Commit

Permalink
Merge pull request #24 from tommeier/master
Browse files Browse the repository at this point in the history
Support OSX defaults keys containing spaces
  • Loading branch information
dgoodlad committed Nov 21, 2013
2 parents e7eee73 + dd83650 commit f10c8c1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 33 deletions.
43 changes: 23 additions & 20 deletions manifests/osx_defaults.pp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
$user = undef,
$type = undef,
) {
$defaults_cmd = '/usr/bin/defaults'

$host_option = $host ? {
'currentHost' => ' -currentHost',
undef => '',
default => " -host ${host}"
$defaults_cmd = '/usr/bin/defaults'
$default_cmds = $host ? {
'currentHost' => [ $defaults_cmd, '-currentHost' ],
undef => [ $defaults_cmd ],
default => [ $defaults_cmd, '-host', $host ]
}

case $ensure {
Expand All @@ -23,36 +22,40 @@
fail('Cannot ensure present without domain, key, and value attributes')
}

if ($type == undef) and (($value == true) or ($value == false)) {
if (($type == undef) and (($value == true) or ($value == false))) or ($type =~ /^bool/) {
$type_ = 'bool'
} else {
$type_ = $type
}

$cmd = $type_ ? {
undef => "${defaults_cmd}${host_option} write ${domain} '${key}' '${value}'",
default => "${defaults_cmd}${host_option} write ${domain} '${key}' -${type_} '${value}'"
}

if ($type_ =~ /^bool/) {
$checkvalue = $value ? {
/(true|yes)/ => '1',
/(false|no)/ => '0',
}

} else {
$type_ = $type
$checkvalue = $value
}

$write_cmd = $type_ ? {
undef => shellquote($default_cmds, 'write', $domain, $key, "${value}"),
default => shellquote($default_cmds, 'write', $domain, $key, "-${type_}", "${value}")
}

$read_cmd = shellquote($default_cmds, 'read', $domain, $key)

exec { "osx_defaults write ${host} ${domain}:${key}=>${value}":
command => $cmd,
unless => "${defaults_cmd}${host_option} read ${domain} '${key}' && (${defaults_cmd}${host_option} read ${domain} '${key}' | awk '{ exit \$0 != \"${checkvalue}\" }')",
command => $write_cmd,
unless => "${read_cmd} && (${read_cmd} | awk '{ exit \$0 != \"${checkvalue}\" }')",
user => $user
}
} # end present

default: {
$list_cmd = shellquote($default_cmds, 'read', $domain)
$key_search = shellquote('grep', $key)

exec { "osx_defaults delete ${host} ${domain}:${key}":
command => "${defaults_cmd}${host_option} delete ${domain} '${key}'",
onlyif => "${defaults_cmd}${host_option} read ${domain} | grep '${key}'",
command => shellquote($default_cmds, 'delete', $domain, $key),
onlyif => "${list_cmd} | ${key_search}",
user => $user
}
} # end default
Expand Down
49 changes: 36 additions & 13 deletions spec/defines/osx_defaults_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,38 @@

it do
should contain_exec("osx_defaults write #{domain}:#{key}=>#{value}").
with(:command => "/usr/bin/defaults write #{domain} '#{key}' '#{value}'")
with(:command => "/usr/bin/defaults write #{domain} #{key} #{value}")
end

context 'with a key with spaces' do
let(:key) { 'test key' }
context 'with quoting for shell values' do
let(:domain) { 'NSGlobalDomain With Space' }
let(:key) { 'Key With Spaces' }
let(:value) { 'Long String With Spaces' }
let(:host) { 'com.example.long/host' }

it 'quotes the key' do
should contain_exec("osx_defaults write #{domain}:#{key}=>#{value}").
with(:command => "/usr/bin/defaults write #{domain} '#{key}' '#{value}'")
let(:default_params) {
{ :domain => domain,
:key => key,
:value => value,
:host => host
}
}
let(:params) { default_params }

context "for writing" do
it do
should contain_exec("osx_defaults write #{host} #{domain}:#{key}=>#{value}").
with(:command => "/usr/bin/defaults -host #{host} write \"#{domain}\" \"#{key}\" \"#{value}\"")
end
end

context "for deleting" do
let(:params) { default_params.merge(:ensure => 'delete') }

it do
should contain_exec("osx_defaults delete #{host} #{domain}:#{key}").
with(:command => "/usr/bin/defaults -host #{host} delete \"#{domain}\" \"#{key}\"")
end
end
end

Expand All @@ -40,31 +63,31 @@
let(:value) { 'yes' }
it 'converts yes to 1 for checking' do
should contain_exec("osx_defaults write #{domain}:#{key}=>#{value}").
with(:unless => "/usr/bin/defaults read #{domain} '#{key}' && (/usr/bin/defaults read #{domain} '#{key}' | awk '{ exit $0 != \"1\" }')")
with(:unless => "/usr/bin/defaults read #{domain} #{key} && (/usr/bin/defaults read #{domain} #{key} | awk '{ exit $0 != \"1\" }')")
end
end

context 'no' do
let(:value) { 'no' }
it 'converts no to 0 for checking' do
should contain_exec("osx_defaults write #{domain}:#{key}=>#{value}").
with(:unless => "/usr/bin/defaults read #{domain} '#{key}' && (/usr/bin/defaults read #{domain} '#{key}' | awk '{ exit $0 != \"0\" }')")
with(:unless => "/usr/bin/defaults read #{domain} #{key} && (/usr/bin/defaults read #{domain} #{key} | awk '{ exit $0 != \"0\" }')")
end
end

context 'true' do
let(:value) { 'true' }
it 'converts true to 1 for checking' do
should contain_exec("osx_defaults write #{domain}:#{key}=>#{value}").
with(:unless => "/usr/bin/defaults read #{domain} '#{key}' && (/usr/bin/defaults read #{domain} '#{key}' | awk '{ exit $0 != \"1\" }')")
with(:unless => "/usr/bin/defaults read #{domain} #{key} && (/usr/bin/defaults read #{domain} #{key} | awk '{ exit $0 != \"1\" }')")
end
end

context 'false' do
let(:value) { 'false' }
it 'converts false to 0 for checking' do
should contain_exec("osx_defaults write #{domain}:#{key}=>#{value}").
with(:unless => "/usr/bin/defaults read #{domain} '#{key}' && (/usr/bin/defaults read #{domain} '#{key}' | awk '{ exit $0 != \"0\" }')")
with(:unless => "/usr/bin/defaults read #{domain} #{key} && (/usr/bin/defaults read #{domain} #{key} | awk '{ exit $0 != \"0\" }')")
end
end
end
Expand All @@ -80,7 +103,7 @@

it do
should contain_exec("osx_defaults write #{domain}:#{key}=>#{value}").with(
:command => "/usr/bin/defaults write #{domain} '#{key}' -bool '#{value}'"
:command => "/usr/bin/defaults write #{domain} #{key} -bool #{value}"
)
end
end
Expand All @@ -99,7 +122,7 @@

it do
should contain_exec("osx_defaults write #{host} #{domain}:#{key}=>#{value}").
with(:command => "/usr/bin/defaults -currentHost write #{domain} '#{key}' '#{value}'")
with(:command => "/usr/bin/defaults -currentHost write #{domain} #{key} #{value}")
end
end

Expand All @@ -108,7 +131,7 @@

it do
should contain_exec("osx_defaults write #{host} #{domain}:#{key}=>#{value}").
with(:command => "/usr/bin/defaults -host #{host} write #{domain} '#{key}' '#{value}'")
with(:command => "/usr/bin/defaults -host #{host} write #{domain} #{key} #{value}")
end
end
end
Expand Down

0 comments on commit f10c8c1

Please sign in to comment.