mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-11-07 21:20:07 +01:00
🏗 WIP: Service toggling
This commit is contained in:
@@ -65,23 +65,6 @@ class Actions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Third Party Services
|
|
||||||
public static func stopService(name: String) async {
|
|
||||||
await brew(
|
|
||||||
"services stop \(name)",
|
|
||||||
sudo: ServicesManager.shared[name]?.formula.elevated ?? false
|
|
||||||
)
|
|
||||||
await ServicesManager.shared.reloadServicesStatus()
|
|
||||||
}
|
|
||||||
|
|
||||||
public static func startService(name: String) async {
|
|
||||||
await brew(
|
|
||||||
"services start \(name)",
|
|
||||||
sudo: ServicesManager.shared[name]?.formula.elevated ?? false
|
|
||||||
)
|
|
||||||
await ServicesManager.shared.reloadServicesStatus()
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - Finding Config Files
|
// MARK: - Finding Config Files
|
||||||
|
|
||||||
public static func openGenericPhpConfigFolder() {
|
public static func openGenericPhpConfigFolder() {
|
||||||
|
|||||||
@@ -63,12 +63,11 @@ class HomebrewService: Decodable, Equatable, Hashable {
|
|||||||
hasher.combine(service_name)
|
hasher.combine(service_name)
|
||||||
hasher.combine(pid)
|
hasher.combine(pid)
|
||||||
hasher.combine(status)
|
hasher.combine(status)
|
||||||
|
hasher.combine(running)
|
||||||
|
hasher.combine(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
static func == (lhs: HomebrewService, rhs: HomebrewService) -> Bool {
|
static func == (lhs: HomebrewService, rhs: HomebrewService) -> Bool {
|
||||||
return lhs.name == rhs.name
|
return lhs.hashValue == rhs.hashValue
|
||||||
&& lhs.service_name == rhs.service_name
|
|
||||||
&& lhs.pid == rhs.pid
|
|
||||||
&& lhs.status == rhs.status
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import Foundation
|
|||||||
public enum ServiceStatus: String {
|
public enum ServiceStatus: String {
|
||||||
case active
|
case active
|
||||||
case inactive
|
case inactive
|
||||||
case loading
|
|
||||||
case missing
|
case missing
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,17 +25,11 @@ public class ServiceWrapper: ObservableObject, Identifiable, Hashable {
|
|||||||
var formula: HomebrewFormula
|
var formula: HomebrewFormula
|
||||||
var service: HomebrewService?
|
var service: HomebrewService?
|
||||||
|
|
||||||
var isBusy: Bool = false
|
|
||||||
|
|
||||||
public var name: String {
|
public var name: String {
|
||||||
return formula.name
|
return formula.name
|
||||||
}
|
}
|
||||||
|
|
||||||
public var status: ServiceStatus {
|
public var status: ServiceStatus {
|
||||||
if isBusy {
|
|
||||||
return .loading
|
|
||||||
}
|
|
||||||
|
|
||||||
guard let service = self.service else {
|
guard let service = self.service else {
|
||||||
return .missing
|
return .missing
|
||||||
}
|
}
|
||||||
@@ -46,16 +39,20 @@ public class ServiceWrapper: ObservableObject, Identifiable, Hashable {
|
|||||||
|
|
||||||
init(formula: HomebrewFormula) {
|
init(formula: HomebrewFormula) {
|
||||||
self.formula = formula
|
self.formula = formula
|
||||||
self.isBusy = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func == (lhs: ServiceWrapper, rhs: ServiceWrapper) -> Bool {
|
public static func == (lhs: ServiceWrapper, rhs: ServiceWrapper) -> Bool {
|
||||||
return lhs.service == rhs.service
|
return lhs.hashValue == rhs.hashValue
|
||||||
&& lhs.formula == rhs.formula
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func hash(into hasher: inout Hasher) {
|
public func hash(into hasher: inout Hasher) {
|
||||||
hasher.combine(formula)
|
hasher.combine(formula)
|
||||||
hasher.combine(service)
|
hasher.combine(service)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func broadcastChanged() {
|
||||||
|
Task { @MainActor in
|
||||||
|
self.objectWillChange.send()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ class ValetServicesManager: ServicesManager {
|
|||||||
*/
|
*/
|
||||||
override func reloadServicesStatus() async {
|
override func reloadServicesStatus() async {
|
||||||
await withTaskGroup(of: [HomebrewService].self, body: { group in
|
await withTaskGroup(of: [HomebrewService].self, body: { group in
|
||||||
|
// First, retrieve the status of the formulae that run as root
|
||||||
group.addTask {
|
group.addTask {
|
||||||
let rootServiceNames = self.formulae
|
let rootServiceNames = self.formulae
|
||||||
.filter { $0.elevated }
|
.filter { $0.elevated }
|
||||||
@@ -37,6 +38,7 @@ class ValetServicesManager: ServicesManager {
|
|||||||
.filter({ return rootServiceNames.contains($0.name) })
|
.filter({ return rootServiceNames.contains($0.name) })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// At the same time, retrieve the status of the formulae that run as user
|
||||||
group.addTask {
|
group.addTask {
|
||||||
let userServiceNames = self.formulae
|
let userServiceNames = self.formulae
|
||||||
.filter { !$0.elevated }
|
.filter { !$0.elevated }
|
||||||
@@ -59,17 +61,28 @@ class ValetServicesManager: ServicesManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure that every wrapper is considered no longer busy
|
|
||||||
for wrapper in serviceWrappers {
|
|
||||||
wrapper.isBusy = false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Broadcast that all services have been updated
|
// Broadcast that all services have been updated
|
||||||
self.broadcastServicesUpdated()
|
self.broadcastServicesUpdated()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
override func toggleService(named: String) async {
|
override func toggleService(named: String) async {
|
||||||
// TODO
|
guard let wrapper = self[named] else {
|
||||||
|
return Log.err("The wrapper for '\(named)' is missing.")
|
||||||
|
}
|
||||||
|
|
||||||
|
if wrapper.service == nil {
|
||||||
|
return Log.err("The Homebrew service for \(named) is missing.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare the appropriate command to stop or start a service
|
||||||
|
let action = wrapper.service!.running ? "stop" : "start"
|
||||||
|
let command = "services \(action) \(wrapper.formula.name)"
|
||||||
|
|
||||||
|
// Run the command
|
||||||
|
await brew(command, sudo: wrapper.formula.elevated)
|
||||||
|
|
||||||
|
// Reload the services status to confirm this worked
|
||||||
|
await ServicesManager.shared.reloadServicesStatus()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user