1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-07 12:00:09 +02:00

👌 Improved nginx file parsing

This commit is contained in:
2022-05-13 00:24:54 +02:00
parent c040ac3200
commit bd34c2b255
5 changed files with 29 additions and 22 deletions

View File

@ -1520,7 +1520,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 785;
CURRENT_PROJECT_VERSION = 786;
DEBUG = YES;
DEVELOPMENT_TEAM = 8M54J5J787;
ENABLE_HARDENED_RUNTIME = YES;
@ -1546,7 +1546,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 785;
CURRENT_PROJECT_VERSION = 786;
DEBUG = NO;
DEVELOPMENT_TEAM = 8M54J5J787;
ENABLE_HARDENED_RUNTIME = YES;

View File

@ -37,43 +37,43 @@ class NginxConfigurationTest: XCTestCase {
func testCanDetermineSiteNameAndTld() throws {
XCTAssertEqual(
"nginx-site",
NginxConfiguration(filePath: NginxConfigurationTest.regularUrl.path).domain
NginxConfiguration.from(filePath: NginxConfigurationTest.regularUrl.path)?.domain
)
XCTAssertEqual(
"test",
NginxConfiguration(filePath: NginxConfigurationTest.regularUrl.path).tld
NginxConfiguration.from(filePath: NginxConfigurationTest.regularUrl.path)?.tld
)
}
func testCanDetermineIsolation() throws {
XCTAssertNil(
NginxConfiguration(filePath: NginxConfigurationTest.regularUrl.path).isolatedVersion
NginxConfiguration.from(filePath: NginxConfigurationTest.regularUrl.path)?.isolatedVersion
)
XCTAssertEqual(
"8.1",
NginxConfiguration(filePath: NginxConfigurationTest.isolatedUrl.path).isolatedVersion
NginxConfiguration.from(filePath: NginxConfigurationTest.isolatedUrl.path)?.isolatedVersion
)
}
func testCanDetermineProxy() throws {
let proxied = NginxConfiguration(filePath: NginxConfigurationTest.proxyUrl.path)
let proxied = NginxConfiguration.from(filePath: NginxConfigurationTest.proxyUrl.path)!
XCTAssertTrue(proxied.contents.contains("# valet stub: proxy.valet.conf"))
XCTAssertEqual("http://127.0.0.1:90", proxied.proxy)
let normal = NginxConfiguration(filePath: NginxConfigurationTest.regularUrl.path)
let normal = NginxConfiguration.from(filePath: NginxConfigurationTest.regularUrl.path)!
XCTAssertFalse(normal.contents.contains("# valet stub: proxy.valet.conf"))
XCTAssertEqual(nil, normal.proxy)
}
func testCanDetermineSecuredProxy() throws {
let proxied = NginxConfiguration(filePath: NginxConfigurationTest.secureProxyUrl.path)
let proxied = NginxConfiguration.from(filePath: NginxConfigurationTest.secureProxyUrl.path)!
XCTAssertTrue(proxied.contents.contains("# valet stub: secure.proxy.valet.conf"))
XCTAssertEqual("http://127.0.0.1:90", proxied.proxy)
}
func testCanDetermineProxyWithCustomTld() throws {
let proxied = NginxConfiguration(filePath: NginxConfigurationTest.customTldProxyUrl.path)
let proxied = NginxConfiguration.from(filePath: NginxConfigurationTest.customTldProxyUrl.path)!
XCTAssertTrue(proxied.contents.contains("# valet stub: secure.proxy.valet.conf"))
XCTAssertEqual("http://localhost:8080", proxied.proxy)
}

View File

@ -19,19 +19,30 @@ class NginxConfiguration {
/** The TLD of the domain, usually derived from the name of the file. */
var tld: String
init(filePath: String) {
static func from(filePath: String) -> NginxConfiguration? {
let path = filePath.replacingOccurrences(
of: "~",
with: "/Users/\(Paths.whoami)"
)
self.contents = try! String(contentsOfFile: path)
do {
let fileContents = try String(contentsOfFile: path)
return NginxConfiguration.init(
path: path,
contents: fileContents
)
} catch {
Log.warn("Could not read the nginx configuration file at: `\(filePath)`")
return nil
}
}
init(path: String, contents: String) {
let domain = String(path.split(separator: "/").last!)
let tld = String(domain.split(separator: ".").last!)
self.domain = domain
.replacingOccurrences(of: ".\(tld)", with: "")
self.contents = contents
self.domain = domain.replacingOccurrences(of: ".\(tld)", with: "")
self.tld = tld
}

View File

@ -13,12 +13,8 @@ class ValetProxyScanner: ProxyScanner {
return try! FileManager
.default
.contentsOfDirectory(atPath: directoryPath)
.filter {
// Skip .DS_Store files
return $0 != ".DS_Store"
}
.map {
return NginxConfiguration.init(filePath: "\(directoryPath)/\($0)")
.compactMap {
return NginxConfiguration.from(filePath: "\(directoryPath)/\($0)")
}
.filter {
return $0.proxy != nil

View File

@ -226,8 +226,8 @@ class ValetSite: DomainListable {
public static func isolatedVersion(_ filePath: String) -> String? {
if Filesystem.fileExists(filePath) {
return NginxConfiguration
.init(filePath: filePath)
.isolatedVersion
.from(filePath: filePath)?
.isolatedVersion ?? nil
}
return nil