diff --git a/Sources/tart/Commands/Get.swift b/Sources/tart/Commands/Get.swift index dbbb7bb..a061e88 100644 --- a/Sources/tart/Commands/Get.swift +++ b/Sources/tart/Commands/Get.swift @@ -4,20 +4,49 @@ import Foundation struct Get: AsyncParsableCommand { static var configuration = CommandConfiguration(commandName: "get", abstract: "Get a VM's configuration") - @Argument(help: "VM name") + @Argument(help: "VM name.") var name: String + @Flag(help: "Number of VM CPUs.") + var cpu: Bool = false + + @Flag(help: "VM memory size in megabytes.") + var memory: Bool = false + + @Flag(help: "Disk size in gigabytes.") + var diskSize: Bool = false + + @Flag(help: "VM display resolution in a format of x. For example, 1200x800.") + var display: Bool = false + + func validate() throws { + if [cpu, memory, diskSize, display].filter({$0}).count > 1 { + throw ValidationError("Options --cpu, --memory, --disk-size and --display are mutually exclusive") + } + } + func run() async throws { let vmDir = try VMStorageLocal().open(name) let vmConfig = try VMConfig(fromURL: vmDir.configURL) - let diskSize = try vmDir.sizeBytes() / 1000 / 1000 / 1000 + let diskSizeInGb = try vmDir.sizeBytes() / 1000 / 1000 / 1000 + let memorySizeInMb = vmConfig.memorySize / 1024 / 1024 - print("CPU\tMemory\tDisk\tDisplay") - - var s = "\(vmConfig.cpuCount)\t" - s += "\(vmConfig.memorySize / 1024 / 1024) MB\t" - s += "\(diskSize) GB\t" - s += "\(vmConfig.display.width)x\(vmConfig.display.height)" - print(s) + if cpu { + print(vmConfig.cpuCount) + } else if memory { + print(memorySizeInMb) + } else if diskSize { + print(diskSizeInGb) + } else if display { + print("\(vmConfig.display.width)x\(vmConfig.display.height)") + } else { + print( + "CPU\tMemory\tDisk\tDisplay\n" + + "\(vmConfig.cpuCount)\t" + + "\(memorySizeInMb) MB\t" + + "\(diskSizeInGb) GB\t" + + "\(vmConfig.display)" + ) + } } } diff --git a/Sources/tart/VMConfig.swift b/Sources/tart/VMConfig.swift index 8f52736..67d1227 100644 --- a/Sources/tart/VMConfig.swift +++ b/Sources/tart/VMConfig.swift @@ -35,6 +35,12 @@ struct VMDisplayConfig: Codable { var height: Int = 768 } +extension VMDisplayConfig: CustomStringConvertible { + var description: String { + "\(width)x\(height)" + } +} + struct VMConfig: Codable { var version: Int = 1 var os: OS