Module: StateMate::Adapters::LaunchD
Overview
Constant Summary
collapse
- EXE =
'/bin/launchctl'
API_METHOD_NAMES, DEFAULT_KEY_SEP
Class Method Summary
collapse
get, included, register
Class Method Details
.disabled?(label, user = ) ⇒ Boolean
50
51
52
53
54
|
# File 'lib/state_mate/adapters/launchd.rb', line 50
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
65
66
67
68
|
# File 'lib/state_mate/adapters/launchd.rb', line 65
def self.load file_path
Cmds! "%{exe} load -w %{file_path}", exe: EXE,
file_path: file_path
end
|
.loaded?(label) ⇒ Boolean
56
57
58
|
# File 'lib/state_mate/adapters/launchd.rb', line 56
def self.loaded? label
Cmds.ok? "%{exe} list -x %{label}", exe: EXE, label: label
end
|
.read(key, options = {}) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/state_mate/adapters/launchd.rb', line 75
def self.read key, options = {}
file_path, key_segs = parse_key key
plist = CFPropertyList::List.new file: file_path
data = CFPropertyList.native_types plist.value
label = data["Label"]
case key_segs
when ['Disabled']
disabled? label
else
raise "unprocessable key: #{ key.inspect }"
end
end
|
.truncate_values(hash, length) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/state_mate/adapters/launchd.rb', line 23
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
70
71
72
73
|
# File 'lib/state_mate/adapters/launchd.rb', line 70
def self.unload file_path
Cmds! "%{exe} unload -w %{file_path}", exe: EXE,
file_path: file_path
end
|
.user_overrides_db(user = ) ⇒ Object
45
46
47
48
|
# File 'lib/state_mate/adapters/launchd.rb', line 45
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
36
37
38
39
40
41
42
43
|
# File 'lib/state_mate/adapters/launchd.rb', line 36
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/state_mate/adapters/launchd.rb', line 92
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
|