forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReloadLaunchAgent.swift
More file actions
53 lines (44 loc) · 1.36 KB
/
ReloadLaunchAgent.swift
File metadata and controls
53 lines (44 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import ArgumentParser
import Foundation
struct ReloadLaunchAgent: ParsableCommand {
static var configuration = CommandConfiguration(
abstract: "Reload the launch agent"
)
@Option(name: .long, help: "The service identifier of the service.")
var serviceIdentifier: String
var launchAgentDirURL: URL {
FileManager.default.homeDirectoryForCurrentUser
.appendingPathComponent("Library/LaunchAgents")
}
var launchAgentPath: String {
launchAgentDirURL.appendingPathComponent("\(serviceIdentifier).plist").path
}
func run() throws {
try? launchctl("unload", launchAgentPath)
try launchctl("load", launchAgentPath)
}
}
private func launchctl(_ args: String...) throws {
return try process("/bin/launchctl", args)
}
private func process(_ launchPath: String, _ args: [String]) throws {
let task = Process()
task.launchPath = launchPath
task.arguments = args
task.environment = [
"PATH": "/usr/bin",
]
let outpipe = Pipe()
task.standardOutput = outpipe
try task.run()
task.waitUntilExit()
struct E: Error, LocalizedError {
var errorDescription: String?
}
if task.terminationStatus == 0 {
return
}
throw E(
errorDescription: "Failed to restart. Please make sure the launch agent is already loaded."
)
}