mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2025-11-07 05:10:06 +01:00
✨ Use ValetInteractor to add links and proxies
This commit is contained in:
@@ -65,16 +65,18 @@ class AddProxyVC: NSViewController, NSTextFieldDelegate {
|
||||
@IBAction func pressedCreateProxy(_ sender: Any) {
|
||||
let domain = self.inputDomainName.stringValue
|
||||
let proxyName = self.inputProxySubject.stringValue
|
||||
let secure = self.buttonSecure.state == .on ? " --secure" : ""
|
||||
let secure = (self.buttonSecure.state == .on)
|
||||
|
||||
dismissView(outcome: .OK)
|
||||
|
||||
App.shared.domainListWindowController?.contentVC.setUIBusy()
|
||||
|
||||
Task { // Ensure we proxy the site asynchronously and reload UI on main thread again
|
||||
#warning("Creating a proxy should happen via the ValetInteractor")
|
||||
await Shell.quiet("\(Paths.valet) proxy \(domain) \(proxyName)\(secure)")
|
||||
await Actions.restartNginx()
|
||||
try! await ValetInteractor.shared.proxy(
|
||||
domain: domain,
|
||||
proxy: proxyName,
|
||||
secure: secure
|
||||
)
|
||||
|
||||
Task { @MainActor in
|
||||
// TODO: Check if this can be removed
|
||||
|
||||
@@ -70,24 +70,24 @@ class AddSiteVC: NSViewController, NSTextFieldDelegate {
|
||||
}
|
||||
|
||||
// Adding `valet links` is a workaround for Valet malforming the config.json file
|
||||
// TODO: I will have to investigate and report this behaviour if possible
|
||||
#warning("Linking a site should happen via the ValetInteractor")
|
||||
Task { await Shell.quiet("cd '\(path)' && \(Paths.valet) link '\(name)' && valet links") }
|
||||
Task {
|
||||
try! await ValetInteractor.shared.link(path: path, domain: name)
|
||||
|
||||
dismissView(outcome: .OK)
|
||||
dismissView(outcome: .OK)
|
||||
|
||||
// Reset search
|
||||
App.shared.domainListWindowController?
|
||||
.searchToolbarItem
|
||||
.searchField.stringValue = ""
|
||||
// Reset search
|
||||
App.shared.domainListWindowController?
|
||||
.searchToolbarItem
|
||||
.searchField.stringValue = ""
|
||||
|
||||
// Add the new item and scrolls to it
|
||||
await App.shared.domainListWindowController?
|
||||
.contentVC
|
||||
.addedNewSite(
|
||||
name: name,
|
||||
secure: buttonSecure.state == .on
|
||||
)
|
||||
// Add the new item and scrolls to it
|
||||
await App.shared.domainListWindowController?
|
||||
.contentVC
|
||||
.addedNewSite(
|
||||
name: name,
|
||||
secureAfterLinking: buttonSecure.state == .on
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@IBAction func pressedCreateLink(_ sender: Any) {
|
||||
|
||||
@@ -166,9 +166,8 @@ extension DomainListVC {
|
||||
style: .critical,
|
||||
onFirstButtonPressed: {
|
||||
self.waitAndExecute {
|
||||
Task { await site.unlink() }
|
||||
} completion: {
|
||||
Task { await self.reloadDomains() }
|
||||
await site.unlink()
|
||||
await self.reloadDomainsWithoutUI()
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -188,9 +187,8 @@ extension DomainListVC {
|
||||
style: .critical,
|
||||
onFirstButtonPressed: {
|
||||
self.waitAndExecute {
|
||||
Task { await proxy.remove() }
|
||||
} completion: {
|
||||
Task { await self.reloadDomains() }
|
||||
await proxy.remove()
|
||||
await self.reloadDomainsWithoutUI()
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
@@ -162,6 +162,12 @@ class DomainListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource
|
||||
}
|
||||
}
|
||||
|
||||
func reloadDomainsWithoutUI() async {
|
||||
await Valet.shared.reloadSites()
|
||||
domains = Valet.shared.sites
|
||||
searchedFor(text: lastSearchedFor)
|
||||
}
|
||||
|
||||
func applySortDescriptor(_ descriptor: NSSortDescriptor) {
|
||||
sortDescriptor = descriptor
|
||||
|
||||
@@ -179,22 +185,22 @@ class DomainListVC: NSViewController, NSTableViewDelegate, NSTableViewDataSource
|
||||
self.domains = descriptor.ascending ? sorted.reversed() : sorted
|
||||
}
|
||||
|
||||
func addedNewSite(name: String, secure: Bool) async {
|
||||
func addedNewSite(name: String, secureAfterLinking: Bool) async {
|
||||
waitAndExecute {
|
||||
await Valet.shared.reloadSites()
|
||||
} completion: { [self] in
|
||||
find(name, secure)
|
||||
find(name, secureAfterLinking)
|
||||
}
|
||||
}
|
||||
|
||||
private func find(_ name: String, _ secure: Bool = false) {
|
||||
private func find(_ name: String, _ shouldSecure: Bool = false) {
|
||||
domains = Valet.getDomainListable()
|
||||
searchedFor(text: "")
|
||||
if let site = domains.enumerated().first(where: { $0.element.getListableName() == name }) {
|
||||
Task { @MainActor in
|
||||
self.tableView.selectRowIndexes([site.offset], byExtendingSelection: false)
|
||||
self.tableView.scrollRowToVisible(site.offset)
|
||||
if secure && !site.element.getListableSecured() {
|
||||
if shouldSecure && !site.element.getListableSecured() {
|
||||
self.toggleSecure()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,14 +11,22 @@ import Foundation
|
||||
class FakeValetInteractor: ValetInteractor {
|
||||
var delayTime: TimeInterval = 1.0
|
||||
|
||||
override func toggleSecure(proxy: ValetProxy) async throws {
|
||||
await delay(seconds: delayTime)
|
||||
proxy.secured = !proxy.secured
|
||||
}
|
||||
// MARK: - Managing Domains
|
||||
|
||||
override func toggleSecure(site: ValetSite) async throws {
|
||||
override func link(path: String, domain: String) async throws {
|
||||
await delay(seconds: delayTime)
|
||||
site.secured = !site.secured
|
||||
|
||||
if let scanner = ValetScanner.active as? FakeDomainScanner {
|
||||
scanner.sites.append(
|
||||
FakeValetSite(
|
||||
fakeWithName: domain,
|
||||
tld: Valet.shared.config.tld,
|
||||
secure: false,
|
||||
path: path,
|
||||
linked: true
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override func unlink(site: ValetSite) async throws {
|
||||
@@ -29,6 +37,41 @@ class FakeValetInteractor: ValetInteractor {
|
||||
}
|
||||
}
|
||||
|
||||
override func proxy(domain: String, proxy: String, secure: Bool) async throws {
|
||||
await delay(seconds: delayTime)
|
||||
|
||||
if let scanner = ValetScanner.active as? FakeDomainScanner {
|
||||
scanner.proxies.append(
|
||||
FakeValetProxy(
|
||||
domain: domain,
|
||||
target: proxy,
|
||||
secure: secure,
|
||||
tld: Valet.shared.config.tld
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override func remove(proxy: ValetProxy) async throws {
|
||||
await delay(seconds: delayTime)
|
||||
|
||||
if let scanner = ValetScanner.active as? FakeDomainScanner {
|
||||
scanner.proxies.removeAll { $0.domain == proxy.domain }
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Modifying Domains
|
||||
|
||||
override func toggleSecure(proxy: ValetProxy) async throws {
|
||||
await delay(seconds: delayTime)
|
||||
proxy.secured = !proxy.secured
|
||||
}
|
||||
|
||||
override func toggleSecure(site: ValetSite) async throws {
|
||||
await delay(seconds: delayTime)
|
||||
site.secured = !site.secured
|
||||
}
|
||||
|
||||
override func isolate(site: ValetSite, version: String) async throws {
|
||||
await delay(seconds: delayTime)
|
||||
|
||||
@@ -42,12 +85,4 @@ class FakeValetInteractor: ValetInteractor {
|
||||
site.isolatedPhpVersion = nil
|
||||
site.evaluateCompatibility()
|
||||
}
|
||||
|
||||
override func remove(proxy: ValetProxy) async throws {
|
||||
await delay(seconds: delayTime)
|
||||
|
||||
if let scanner = ValetScanner.active as? FakeDomainScanner {
|
||||
scanner.proxies.removeAll { $0 === proxy }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,31 @@ class ValetInteractor {
|
||||
ValetInteractor.shared = FakeValetInteractor()
|
||||
}
|
||||
|
||||
// MARK: - Managing Domains
|
||||
|
||||
public func link(path: String, domain: String) async throws {
|
||||
await Shell.quiet("cd '\(path)' && \(Paths.valet) link '\(domain)' && valet links")
|
||||
}
|
||||
|
||||
public func unlink(site: ValetSite) async throws {
|
||||
await Shell.quiet("valet unlink '\(site.name)'")
|
||||
}
|
||||
|
||||
public func proxy(domain: String, proxy: String, secure: Bool) async throws {
|
||||
let command = secure
|
||||
? "\(Paths.valet) proxy \(domain) \(proxy) --secure"
|
||||
: "\(Paths.valet) proxy \(domain) \(proxy)"
|
||||
|
||||
await Shell.quiet(command)
|
||||
await Actions.restartNginx()
|
||||
}
|
||||
|
||||
public func remove(proxy: ValetProxy) async throws {
|
||||
await Shell.quiet("valet unproxy '\(proxy.domain)'")
|
||||
}
|
||||
|
||||
// MARK: - Modifying Domains
|
||||
|
||||
public func toggleSecure(site: ValetSite) async throws {
|
||||
// Keep track of the original status (secure or not?)
|
||||
let originalSecureStatus = site.secured
|
||||
@@ -100,12 +125,4 @@ class ValetInteractor {
|
||||
throw ValetInteractionError(command: command)
|
||||
}
|
||||
}
|
||||
|
||||
public func unlink(site: ValetSite) async throws {
|
||||
await Shell.quiet("valet unlink '\(site.name)'")
|
||||
}
|
||||
|
||||
public func remove(proxy: ValetProxy) async throws {
|
||||
await Shell.quiet("valet unproxy '\(proxy.domain)'")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// FakeValetProxy.swift
|
||||
// PHP Monitor
|
||||
//
|
||||
// Created by Nico Verbruggen on 16/12/2022.
|
||||
// Copyright © 2022 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class FakeValetProxy: ValetProxy {
|
||||
override func determineSecured() {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
//
|
||||
// ValetProxy+Fake.swift
|
||||
// PHP Monitor
|
||||
//
|
||||
// Created by Nico Verbruggen on 02/04/2022.
|
||||
// Copyright © 2022 Nico Verbruggen. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension ValetProxy {
|
||||
|
||||
}
|
||||
@@ -14,10 +14,20 @@ class ValetProxy: ValetListable {
|
||||
var target: String
|
||||
var secured: Bool = false
|
||||
|
||||
init(_ configuration: NginxConfigurationFile) {
|
||||
self.domain = configuration.domain
|
||||
self.tld = configuration.tld
|
||||
self.target = configuration.proxy!
|
||||
init(domain: String, target: String, secure: Bool, tld: String) {
|
||||
self.domain = domain
|
||||
self.tld = tld
|
||||
self.target = target
|
||||
self.secured = false
|
||||
}
|
||||
|
||||
convenience init(_ configuration: NginxConfigurationFile) {
|
||||
self.init(
|
||||
domain: configuration.domain,
|
||||
target: configuration.proxy!,
|
||||
secure: false,
|
||||
tld: configuration.tld
|
||||
)
|
||||
self.determineSecured()
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ class FakeDomainScanner: DomainScanner {
|
||||
]
|
||||
|
||||
var proxies: [ValetProxy] = [
|
||||
// TODO: Add new proxy here
|
||||
FakeValetProxy(domain: "mailgun", target: "http://127.0.0.1:9999", secure: true, tld: "test")
|
||||
]
|
||||
|
||||
// MARK: - Sites
|
||||
|
||||
Reference in New Issue
Block a user