Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-Write for User Preferences and Grep Issues #2

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/facter/current_user.rb
Original file line number Diff line number Diff line change
@@ -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
71 changes: 41 additions & 30 deletions manifests/init.pp
Original file line number Diff line number Diff line change
@@ -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: {
}
}


}