tart ip: keep waiting for the /var/db/dhcpd_leases file to appear (#254)

This commit is contained in:
Nikolay Edigaryev 2022-09-22 18:24:14 +04:00 committed by GitHub
parent 0229138bd5
commit afbc2e0764
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 6 deletions

View File

@ -44,10 +44,10 @@ struct IP: AsyncParsableCommand {
let waitUntil = Calendar.current.date(byAdding: .second, value: Int(secondsToWait), to: Date.now)!
repeat {
if let ip = try Leases().resolveMACAddress(macAddress: vmMACAddress) {
if let leases = try Leases(), let ip = try leases.resolveMACAddress(macAddress: vmMACAddress) {
return ip
}
// wait a second
try await Task.sleep(nanoseconds: 1_000_000_000)
} while Date.now < waitUntil

View File

@ -19,14 +19,21 @@ enum LeasesError: Error {
class Leases {
private let leases: [MACAddress : Lease]
convenience init() throws {
convenience init?() throws {
try self.init(URL(fileURLWithPath: "/var/db/dhcpd_leases"))
}
convenience init(_ fromURL: URL) throws {
let fileContents = try String(contentsOf: fromURL, encoding: .utf8)
convenience init?(_ fromURL: URL) throws {
do {
let urlContents = try String(contentsOf: fromURL, encoding: .utf8)
try self.init(urlContents)
} catch {
if error.isFileNotFound() {
return nil
}
try self.init(fileContents)
throw error
}
}
init(_ fromString: String) throws {