Module: StateMate::Adapters::LaunchD

Defined in:
lib/state_mate/adapters/launchd.rb

Overview

very useful:

http://launchd.info/ 46

Constant Summary collapse

EXE =
'/bin/launchctl'

Class Method Summary collapse

Class Method Details

.disabled?(label, user = ) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/state_mate/adapters/launchd.rb', line 48

def self.disabled? label, user = ENV['USER']
  db = user_overrides_db(user)
  return false unless db.key?(label) && db[label].key?('Disabled')
  db[label]['Disabled']
end

.load(file_path) ⇒ Object



63
64
65
66
# File 'lib/state_mate/adapters/launchd.rb', line 63

def self.load file_path
  Cmds! "%{exe} load -w %{file_path}",  exe: EXE,
                                        file_path: file_path
end

.loaded?(label) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/state_mate/adapters/launchd.rb', line 54

def self.loaded? label
    Cmds.ok? "%{exe} list -x %{label}", exe: EXE, label: label
end

.parse_key(key) ⇒ Object



58
59
60
61
# File 'lib/state_mate/adapters/launchd.rb', line 58

def self.parse_key key
  # use the same key seperation as Defaults
  StateMate::Adapters::Defaults.parse_key key
end

.read(key, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/state_mate/adapters/launchd.rb', line 73

def self.read key, options = {}
  file_path, key_segs = parse_key key

  # get the hash of the plist at the file path and use that to get the label
  plist = CFPropertyList::List.new file: file_path
  data = CFPropertyList.native_types plist.value
  label = data["Label"]

  case key_segs
  # the only thing we can handle right now
  when ['Disabled']
    disabled? label
  else
    raise "unprocessable key: #{ key.inspect }"
  end
end

.truncate_values(hash, length) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/state_mate/adapters/launchd.rb', line 21

def self.truncate_values hash, length
  hash.map {|k, v|
    case v
    when String
      [k, v.truncate(length)]
    when Hash
      [k, truncate_values(v, length)]
    else
      [k ,v]
    end
  }.to_h
end

.unload(file_path) ⇒ Object



68
69
70
71
# File 'lib/state_mate/adapters/launchd.rb', line 68

def self.unload file_path
  Cmds! "%{exe} unload -w %{file_path}",  exe: EXE,
                                          file_path: file_path
end

.user_overrides_db(user = ) ⇒ Object



43
44
45
46
# File 'lib/state_mate/adapters/launchd.rb', line 43

def self.user_overrides_db user = ENV['USER']
  plist = CFPropertyList::List.new file: user_overrides_db_path(user)
  CFPropertyList.native_types plist.value
end

.user_overrides_db_path(user = ) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/state_mate/adapters/launchd.rb', line 34

def self.user_overrides_db_path user = ENV['USER']
  if user == 'root'
    "/var/db/launchd.db/com.apple.launchd/overrides.plist"
  else
    user_id = Cmds!("id -u %{user}", user: user).out.chomp.to_i
    "/var/db/launchd.db/com.apple.launchd.peruser.#{ user_id }/overrides.plist"
  end
end

.write(key, value, options = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/state_mate/adapters/launchd.rb', line 90

def self.write key, value, options = {}
  file_path, key_segs = parse_key key

  case key_segs
  when ['Disabled']
    case value
    when true
      unload file_path

    when false
      load file_path

    else
      raise StateMate::Error::TypeError value, "expected true or false"

    end
  else
    raise "unprocessable key: #{ key.inspect }"
  end
end