From c9caeab098bd4551735b441032cc40a332df1f02 Mon Sep 17 00:00:00 2001 From: marc-48k <61566036+marc-48k@users.noreply.github.com> Date: Wed, 14 Dec 2022 18:08:35 +0000 Subject: [PATCH] Filter tart list by source (#349) * Adds Option to filter VMs by 'source' * Tidy up columns for table on some Terminals * Don't show headers if there's an error. * Fixes linting errors. * Update Sources/tart/Commands/List.swift - removing the short name for now, as we don't know if we may need -s in the future - removing the capitalization for the word "source" and adding an example instead Co-authored-by: Nikolay Edigaryev * source should be an Optional flag. Reverts header change. Co-authored-by: Nikolay Edigaryev --- Sources/tart/Commands/List.swift | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) 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)") } }