class Defaulty

Constants

VERSION

Public Class Methods

all_defaults() click to toggle source
# File lib/defaulty.rb, line 70
def self.all_defaults
  all_domains = domains + ['NSGlobalDomain']
  selected_domains = all_domains
  pairs = selected_domains.map do |domain|
    [domain, defaults(domain)]
  end
  Hash[pairs]
end
defaults(domain) click to toggle source
# File lib/defaulty.rb, line 63
def self.defaults(domain)
  Plist::parse_xml `defaults export #{domain} -`
rescue 
  puts "failed to parse #{domain}"
  []
end
domains() click to toggle source
# File lib/defaulty.rb, line 59
def self.domains
  `defaults domains`.split(",").map { |domain| domain.strip }
end
load(*urls) click to toggle source
# File lib/defaulty.rb, line 46
def self.load(*urls)
  defs = urls.map do |url|
    if github_index_url?(url)
      load_ymls_from_github(url)
    else
      load_ymls_from_path(url)
    end
  end 

  Defaulty.new(defs.flatten)
end
new(defs) click to toggle source
# File lib/defaulty.rb, line 125
def initialize(defs)
  domains = defs.map { |d| Domain.new(d) }
  @domains = Hash[ domains.map { |domain| [domain.name, domain] } ]
end

Private Class Methods

github_index_url?(url) click to toggle source
# File lib/defaulty.rb, line 94
def self.github_index_url?(url)
  url =~ %r(^https://api.github.com/repos/.*/contents/?$)
end
load_yml(url) click to toggle source
# File lib/defaulty.rb, line 121
def self.load_yml(url)
  YAML.load(open(url).read)
end
load_ymls_from_github(contents_url) click to toggle source
# File lib/defaulty.rb, line 102
def self.load_ymls_from_github(contents_url)
  begin
    contents = JSON.parse(open(contents_url, :http_basic_authentication=>[ENV['GITHUB_USERNAME'], ENV['BOXY_GITHUB_API_TOKEN']]).read)
    yml_contents = contents.select { |f| yml_url?(f['name']) }
    yml_contents.map { |yml| load_yml(yml['download_url']) }
  rescue => e
    puts "Problem loading content from github: #{contents_url}"
    puts e.inspect
    puts e.io.read
    puts "Exiting..."
    exit
  end
end
load_ymls_from_path(contents_url) click to toggle source
# File lib/defaulty.rb, line 116
def self.load_ymls_from_path(contents_url)
  yml_files = Dir.glob("#{contents_url}/**/*.yml")
  yml_files.map { |yml_file| load_yml(yml_file) }
end
yml_url?(url) click to toggle source
# File lib/defaulty.rb, line 98
def self.yml_url?(url)
  url =~ /\.yml$/ 
end

Public Instance Methods

write(app, options) click to toggle source
# File lib/defaulty.rb, line 79
def write(app, options)
  domain = @domains[app]
  raise "Can't find domain for '#{app}'" unless domain
  options.each do |key, value|
    key = key.to_s
    property = domain.properties[key]
    raise "Can't find a property '#{key}' in domain '#{domain.name}'" unless property
    cmd = "defaults write #{domain.name} #{property.name} -#{property.type} #{value}"
    puts cmd
    `#{cmd}`
  end
end