diff --git a/Sources/tart/Commands/Clone.swift b/Sources/tart/Commands/Clone.swift index 73a8493..67068b3 100644 --- a/Sources/tart/Commands/Clone.swift +++ b/Sources/tart/Commands/Clone.swift @@ -63,7 +63,7 @@ struct Clone: AsyncParsableCommand { try lock.lock() let generateMAC = try localStorage.hasVMsWithMACAddress(macAddress: sourceVM.macAddress()) - && sourceVM.state() != "suspended" + && sourceVM.state() != .Suspended try sourceVM.clone(to: tmpVMDir, generateMAC: generateMAC) try localStorage.move(newName, from: tmpVMDir) diff --git a/Sources/tart/Commands/Get.swift b/Sources/tart/Commands/Get.swift index 89f57f8..733d8ff 100644 --- a/Sources/tart/Commands/Get.swift +++ b/Sources/tart/Commands/Get.swift @@ -26,8 +26,7 @@ struct Get: AsyncParsableCommand { let vmConfig = try VMConfig(fromURL: vmDir.configURL) let memorySizeInMb = vmConfig.memorySize / 1024 / 1024 - let info = VMInfo(OS: vmConfig.os, CPU: vmConfig.cpuCount, Memory: memorySizeInMb, Disk: try vmDir.sizeGB(), Size: String(format: "%.3f", Float(try vmDir.allocatedSizeBytes()) / 1000 / 1000 / 1000), - Display: vmConfig.display.description, Running: try vmDir.running(), State: try vmDir.state()) + let info = VMInfo(OS: vmConfig.os, CPU: vmConfig.cpuCount, Memory: memorySizeInMb, Disk: try vmDir.sizeGB(), Size: String(format: "%.3f", Float(try vmDir.allocatedSizeBytes()) / 1000 / 1000 / 1000), Display: vmConfig.display.description, Running: try vmDir.running(), State: try vmDir.state().rawValue) print(format.renderSingle(info)) } } diff --git a/Sources/tart/Commands/List.swift b/Sources/tart/Commands/List.swift index 8a1af18..c958007 100644 --- a/Sources/tart/Commands/List.swift +++ b/Sources/tart/Commands/List.swift @@ -38,13 +38,13 @@ struct List: AsyncParsableCommand { if source == nil || source == "local" { infos += sortedInfos(try VMStorageLocal().list().map { (name, vmDir) in - try VMInfo(Source: "local", Name: name, Disk: vmDir.sizeGB(), Size: vmDir.allocatedSizeGB(), Running: vmDir.running(), State: vmDir.state()) + try VMInfo(Source: "local", Name: name, Disk: vmDir.sizeGB(), Size: vmDir.allocatedSizeGB(), Running: vmDir.running(), State: vmDir.state().rawValue) }) } if source == nil || source == "oci" { infos += sortedInfos(try VMStorageOCI().list().map { (name, vmDir, _) in - try VMInfo(Source: "oci", Name: name, Disk: vmDir.sizeGB(), Size: vmDir.allocatedSizeGB(), Running: vmDir.running(), State: vmDir.state()) + try VMInfo(Source: "oci", Name: name, Disk: vmDir.sizeGB(), Size: vmDir.allocatedSizeGB(), Running: vmDir.running(), State: vmDir.state().rawValue) }) } diff --git a/Sources/tart/Commands/Run.swift b/Sources/tart/Commands/Run.swift index 84e3e28..a8ecc78 100644 --- a/Sources/tart/Commands/Run.swift +++ b/Sources/tart/Commands/Run.swift @@ -159,7 +159,7 @@ struct Run: AsyncParsableCommand { let localStorage = VMStorageLocal() let vmDir = try localStorage.open(name) - if try vmDir.state() == "suspended" { + if try vmDir.state() == .Suspended { suspendable = true } @@ -182,7 +182,7 @@ struct Run: AsyncParsableCommand { let vmDir = try localStorage.open(name) let storageLock = try FileLock(lockURL: Config().tartHomeDir) - if try vmDir.state() == "suspended" { + if try vmDir.state() == .Suspended { try storageLock.lock() // lock before checking let needToGenerateNewMac = try localStorage.list().contains { // check if there is a running VM with the same MAC but different name diff --git a/Sources/tart/Commands/Stop.swift b/Sources/tart/Commands/Stop.swift index 85f5d2c..631bee9 100644 --- a/Sources/tart/Commands/Stop.swift +++ b/Sources/tart/Commands/Stop.swift @@ -15,12 +15,12 @@ struct Stop: AsyncParsableCommand { func run() async throws { let vmDir = try VMStorageLocal().open(name) switch try vmDir.state() { - case "suspended": + case .Suspended: try stopSuspended(vmDir) - case "running": + case .Running: try await stopRunning(vmDir) - default: - return + case .Stopped: + throw RuntimeError.VMNotRunning(name) } } diff --git a/Sources/tart/VMDirectory.swift b/Sources/tart/VMDirectory.swift index 14473de..650d8d2 100644 --- a/Sources/tart/VMDirectory.swift +++ b/Sources/tart/VMDirectory.swift @@ -3,6 +3,12 @@ import Virtualization import CryptoKit struct VMDirectory: Prunable { + enum State: String { + case Running = "running" + case Suspended = "suspended" + case Stopped = "stopped" + } + var baseURL: URL var configURL: URL { @@ -47,13 +53,13 @@ struct VMDirectory: Prunable { return try lock.pid() != 0 } - func state() throws -> String { + func state() throws -> State { if try running() { - return "running" + return State.Running } else if FileManager.default.fileExists(atPath: stateURL.path) { - return "suspended" + return State.Suspended } else { - return "stopped" + return State.Stopped } }