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 #18 from dgoodlad/support-host-option-for-osx-defa…
Browse files Browse the repository at this point in the history
…ults

Support -host/-currentHost options to defaults
  • Loading branch information
wfarr committed Mar 14, 2013
2 parents d2b86aa + 91b8e8e commit c525324
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
20 changes: 13 additions & 7 deletions manifests/osx_defaults.pp
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
define boxen::osx_defaults(
$ensure = 'present',
$host = undef,
$domain = undef,
$key = undef,
$value = undef,
$user = undef,
$type = undef,
) {
$defaults_cmd = '/usr/bin/defaults'
case $host {
'currentHost': { $host_option = ' -currentHost' }
undef: { $host_option = '' }
default: { $host_option = " -host ${host}" }
}

if $ensure == 'present' {
if ($domain != undef) and ($key != undef) and ($value != undef) {
if ($type != undef) {
$cmd = "${defaults_cmd} write ${domain} ${key} -${type} '${value}'"
$cmd = "${defaults_cmd}${host_option} write ${domain} ${key} -${type} '${value}'"
} else {
$cmd = "${defaults_cmd} write ${domain} ${key} '${value}'"
$cmd = "${defaults_cmd}${host_option} write ${domain} ${key} '${value}'"
}
exec { "osx_defaults write ${domain}:${key}=>${value}":
exec { "osx_defaults write ${host} ${domain}:${key}=>${value}":
command => "${cmd}",
unless => "${defaults_cmd} read ${domain} ${key} && (${defaults_cmd} read ${domain} ${key} | awk '{ exit \$0 != \"${value}\" }')",
unless => "${defaults_cmd}${host_option} read ${domain} ${key} && (${defaults_cmd}${host_option} read ${domain} ${key} | awk '{ exit \$0 != \"${value}\" }')",
user => $user
}
} else {
warning('Cannot ensure present without domain, key, and value attributes')
}
} else {
exec { "osx_defaults delete ${domain}:${key}":
command => "${defaults_cmd} delete ${domain} ${key}",
onlyif => "${defaults_cmd} read ${domain} | 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}",
user => $user
}
}
Expand Down
48 changes: 48 additions & 0 deletions spec/defines/osx_defaults_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'spec_helper'

describe 'boxen::osx_defaults' do
let(:title) { 'example' }
let(:domain) { 'com.example' }
let(:key) { 'testkey' }
let(:value) { 'yes' }

let(:params) {
{ :domain => domain,
:key => key,
:value => value,
}
}

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

context "with a host" do
let(:params) {
{ :domain => domain,
:key => key,
:value => value,
:host => host
}
}

context "currentHost" do
let(:host) { 'currentHost' }

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

context "specific host" do
let(:host) { 'mybox.example.com' }

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
end
end

0 comments on commit c525324

Please sign in to comment.