mirror of
https://github.com/trycua/computer.git
synced 2026-02-23 06:50:14 -06:00
Replace the flag --json with the option --format json|text
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
6
src/Commands/Options/FormatOption.swift
Normal file
6
src/Commands/Options/FormatOption.swift
Normal file
@@ -0,0 +1,6 @@
|
||||
import ArgumentParser
|
||||
|
||||
enum FormatOption: String, ExpressibleByArgument {
|
||||
case json
|
||||
case text
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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"])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user