Get command (#353)

* (wip) First pass at Get command.

* Adds validation in case multiple options are supplied.
This commit is contained in:
marc-48k 2022-12-15 18:52:22 +00:00 committed by GitHub
parent d9f1c37cdd
commit fdeaf979c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 9 deletions

View File

@ -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 <width>x<height>. 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)"
)
}
}
}

View File

@ -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