diff --git a/lib/facter/current_user.rb b/lib/facter/current_user.rb new file mode 100644 index 0000000..fe22951 --- /dev/null +++ b/lib/facter/current_user.rb @@ -0,0 +1,10 @@ +# +# Credit to Graham Gilbert for this fact. +# current_user.rb +# +Facter.add('current_user') do + confine kernel: 'Darwin' + setcode do + Facter::Util::Resolution.exec('/bin/ls -l /dev/console').split(' ')[2] + end +end diff --git a/manifests/init.pp b/manifests/init.pp index 08bd703..22d7acb 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,45 +1,56 @@ # Note that type can be one of: # string, data, int, float, bool, data, array, array-add, dict, dict-add -define macdefaults($domain, $key, $value = false, $type = 'string', $action = 'write') { +define macdefaults($domain, $key, $value = false, $type = 'string', $action = 'write', $runas = 'root', $currenthost = false) { - case $type { - 'bool': { - if $value { - $write_value = 'TRUE' - } - else { - $write_value = 'FALSE' - } + if $runas != 'root' { + $user = $::current_user + } + + if $currenthost { + $writecommand = '/usr/bin/defaults -currentHost write' + $readcommand = '/usr/bin/defaults -currentHost read' + $deletecommand = '/usr/bin/defaults -currentHost delete' + } + else { + $writecommand = '/usr/bin/defaults write' + $readcommand = '/usr/bin/defaults read' + $deletecommand = '/usr/bin/defaults delete' + } + + case $value { + true : { + $grep = 1 + } + false : { + $grep = 0 } default: { - $write_value = $value + $grep = $value } } - case $operatingsystem { - 'Darwin':{ - case $action { - 'write': { - exec {"/usr/bin/defaults write ${domain} ${key} -${type} '${write_value}'": - unless => $type ? { - 'bool' => $value ? { - true => "/usr/bin/defaults read ${domain} ${key} | /usr/bin/grep -qx 1", - false => "/usr/bin/defaults read ${domain} ${key} | /usr/bin/grep -qx 0" - }, - default => "/usr/bin/defaults read ${domain} ${key} | /usr/bin/grep -qx ${value}" + case $::operatingsystem { + 'Darwin':{ + case $action { + 'write': { + exec { "${writecommand} ${domain} ${key} -${type} '${value}'": + user => $user, + unless => "${readcommand} ${domain} ${key} | /usr/bin/grep -q '${grep}'" } } - } - 'delete': { - exec {"/usr/bin/defaults delete ${domain} ${key}": - logoutput => false, - onlyif => "/usr/bin/defaults read ${domain} | /usr/bin/grep -q '${key}'" + 'delete': { + exec { "${deletecommand} ${domain} ${key}": + logoutput => false, + user => $user, + onlyif => "${readcommand} ${domain} | /usr/bin/grep -q '${key}'" + } + } + default: { + fail('Only write and delete are supported values for action.') } } } - } + default: { + } } - - } -