diff --git a/Sources/tart/Commands/List.swift b/Sources/tart/Commands/List.swift index f799271..bbccf4a 100644 --- a/Sources/tart/Commands/List.swift +++ b/Sources/tart/Commands/List.swift @@ -5,17 +5,35 @@ import SwiftUI struct List: AsyncParsableCommand { static var configuration = CommandConfiguration(abstract: "List created VMs") - @Flag(name: [.short, .long], help: ArgumentHelp("Only display VM names")) + @Flag(name: [.short, .long], help: ArgumentHelp("Only display VM names.")) var quiet: Bool = false + @Option(help: ArgumentHelp("Only display VMs from the specified source (e.g. --source local, --source oci).")) + var source: String? + + func validate() throws { + guard let source = source else { + return + } + + if !["local", "oci"].contains(source) { + throw ValidationError("'\(source)' is not a valid ") + } + } + func run() async throws { do { if !quiet { print("Source\tName") } - displayTable("local", try VMStorageLocal().list()) - displayTable("oci", try VMStorageOCI().list().map { (name, vmDir, _) in (name, vmDir) }) + if source == nil || source == "local" { + displayTable("local", try VMStorageLocal().list()) + } + + if source == nil || source == "oci" { + displayTable("oci", try VMStorageOCI().list().map { (name, vmDir, _) in (name, vmDir) }) + } Foundation.exit(0) } catch { @@ -30,6 +48,7 @@ struct List: AsyncParsableCommand { if quiet { print(name) } else { + let source = source.padding(toLength: "Source".count, withPad: " ", startingAt: 0) print("\(source)\t\(name)") } }