Skip to content

Commit

Permalink
tart ip: keep waiting for the /var/db/dhcpd_leases file to appear (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
edigaryev authored Sep 22, 2022
1 parent 0229138 commit afbc2e0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Sources/tart/Commands/IP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions Sources/tart/MACAddressResolver/Leases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit afbc2e0

Please sign in to comment.