Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

Replace the flag --json with the option --format json|text

Pedroeef1853f502c
diff --git a/src/Commands/Get.swift b/src/Commands/Get.swift
index 43137024e8..e3bcbbefaf 100644
--- a/src/Commands/Get.swift
+++ b/src/Commands/Get.swift
@@ -9,8 +9,8 @@ struct Get: AsyncParsableCommand {
@Argument(help: "Name of the virtual machine", completion: .custom(completeVMName))
var name: String
- @Flag(name: .long, help: "Outputs the images as a machine-readable JSON.")
- var json = false
+ @Option(name: [.long, .customShort("f")], help: "Output format (json|text)")
+ var format: FormatOption = .text
init() {
}
@@ -19,6 +19,6 @@ struct Get: AsyncParsableCommand {
func run() async throws {
let vmController = LumeController()
let vm = try vmController.get(name: name)
- try VMDetailsPrinter.printStatus([vm.details], json: self.json)
+ try VMDetailsPrinter.printStatus([vm.details], format: self.format)
}
}
diff --git a/src/Commands/List.swift b/src/Commands/List.swift
index 56b8e72bd9..6361f899d4 100644
--- a/src/Commands/List.swift
+++ b/src/Commands/List.swift
@@ -7,8 +7,8 @@ struct List: AsyncParsableCommand {
abstract: "List virtual machines"
)
- @Flag(name: .long, help: "Outputs the images as a machine-readable JSON.")
- var json = false
+ @Option(name: [.long, .customShort("f")], help: "Output format (json|text)")
+ var format: FormatOption = .text
init() {
}
@@ -17,10 +17,10 @@ struct List: AsyncParsableCommand {
func run() async throws {
let manager = LumeController()
let vms = try manager.list()
- if vms.isEmpty && !json {
+ if vms.isEmpty && self.format == .text {
print("No virtual machines found")
} else {
- try VMDetailsPrinter.printStatus(vms, json: self.json)
+ try VMDetailsPrinter.printStatus(vms, format: self.format)
}
}
}
diff --git a/src/Commands/Options/FormatOption.swift b/src/Commands/Options/FormatOption.swift
new file mode 100644
index 0000000000..0d362a2ed7
--- /dev/null
+++ b/src/Commands/Options/FormatOption.swift
@@ -0,0 +1,6 @@
+import ArgumentParser
+
+enum FormatOption: String, ExpressibleByArgument {
+ case json
+ case text
+}
diff --git a/src/VM/VMDetailsPrinter.swift b/src/VM/VMDetailsPrinter.swift
index 2c976a901f..4d62438f82 100644
--- a/src/VM/VMDetailsPrinter.swift
+++ b/src/VM/VMDetailsPrinter.swift
@@ -33,8 +33,8 @@ enum VMDetailsPrinter {
/// Prints the status of all VMs in a formatted table
/// - Parameter vms: Array of VM status objects to display
- static func printStatus(_ vms: [VMDetails], json: Bool, print: (String) -> () = { print($0) }) throws {
- if json {
+ static func printStatus(_ vms: [VMDetails], format: FormatOption, print: (String) -> () = { print($0) }) throws {
+ if format == .json {
let jsonEncoder = JSONEncoder()
jsonEncoder.outputFormatting = .prettyPrinted
let jsonData = try jsonEncoder.encode(vms)
diff --git a/tests/VM/VMDetailsPrinterTests.swift b/tests/VM/VMDetailsPrinterTests.swift
index 3f73e65a07..fad026c2f4 100644
--- a/tests/VM/VMDetailsPrinterTests.swift
+++ b/tests/VM/VMDetailsPrinterTests.swift
@@ -20,7 +20,7 @@ struct VMDetailsPrinterTests {
// When
var printedStatus: String?
- try VMDetailsPrinter.printStatus(vms, json: true, print: { printedStatus = $0 })
+ try VMDetailsPrinter.printStatus(vms, format: .json, print: { printedStatus = $0 })
// Then
#expect(printedStatus == expectedOutput)
@@ -39,7 +39,7 @@ struct VMDetailsPrinterTests {
// When
var printedLines: [String] = []
- try VMDetailsPrinter.printStatus(vms, json: false, print: { printedLines.append($0) })
+ try VMDetailsPrinter.printStatus(vms, format: .text, print: { printedLines.append($0) })
// Then
#expect(printedLines.count == 2)
@@ -49,6 +49,6 @@ struct VMDetailsPrinterTests {
#expect(headerParts == ["name", "os", "cpu", "memory", "disk", "status", "ip", "vnc"])
let vmParts = printedLines[1].split(whereSeparator: \.isWhitespace)
- #expect(vmParts == ["name", "os", "2", "0.00G", "24.0B/30.0B", "status", "0.0.0.0", "vncUrl"])
+ #expect(vmParts == ["name", "os", "2", "0.00G", "24.0B/30.0B", "status", "0.0.0.0", "vncUrl"])
}
}