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

Use plist over rexml #599

Merged
merged 1 commit into from
Oct 28, 2023
Merged
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
11 changes: 6 additions & 5 deletions lib/service/formula_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,13 @@ def service_file_present?(opts = { for: false })

def owner
if System.launchctl? && dest.exist?
require "rexml/document"

# read the username from the plist file
plist = REXML::Document.new(dest.read)
username_xpath = "/plist/dict/key[text()='UserName']/following-sibling::*[1]"
plist_username = REXML::XPath.first(plist, username_xpath)&.text
plist = begin
Plist.parse_xml(dest.read, marshal: false)
rescue
nil
end
plist_username = plist["UserName"] if plist

return plist_username if plist_username.present?
end
Expand Down
13 changes: 4 additions & 9 deletions lib/service/services_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def take_root_ownership(service)
chown "root", group, service.dest
plist_data = service.dest.read
plist = begin
Plist.parse_xml(plist_data)
Plist.parse_xml(plist_data, marshal: false)
rescue
nil
end
Expand Down Expand Up @@ -307,16 +307,11 @@ def install_service_file(service, file)
contents = service.service_file.read

if sudo_service_user && System.launchctl?
require "rexml/document"

# set the username in the new plist file
ohai "Setting username in #{service.service_name} to #{System.user}"
plist = REXML::Document.new(contents)
dict_element = REXML::XPath.first(plist, "/plist/dict/")
dict_element.add_element("key").text = "UserName"
dict_element.add_element("string").text = sudo_service_user

plist.to_s
plist_data = Plist.parse_xml(contents, marshal: false)
plist_data["UserName"] = sudo_service_user
plist_data.to_plist
else
contents
end
Expand Down