Skip to content

Commit 4e7a872

Browse files
committed
refactor: support format choice
Signed-off-by: thxCode <[email protected]>
1 parent 9e3032f commit 4e7a872

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

gpustack_runtime/cmds/deployer.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ class GetWorkloadSubCommand(SubCommand):
396396
"""
397397

398398
name: str
399-
json: bool = False
399+
format: str = "table"
400400
watch: int = 0
401401

402402
@staticmethod
@@ -413,9 +413,11 @@ def register(parser: _SubParsersAction):
413413
)
414414

415415
get_parser.add_argument(
416-
"--json",
417-
action="store_true",
418-
help="output in JSON format",
416+
"--format",
417+
type=str,
418+
choices=["table", "json"],
419+
default="table",
420+
help="output format",
419421
)
420422

421423
get_parser.add_argument(
@@ -429,7 +431,7 @@ def register(parser: _SubParsersAction):
429431

430432
def __init__(self, args: Namespace):
431433
self.name = args.name
432-
self.json = args.json
434+
self.format = args.format
433435
self.watch = args.watch
434436

435437
if not self.name:
@@ -441,10 +443,11 @@ def run(self):
441443
while True:
442444
sts: list[WorkloadStatus] = [get_workload(self.name)]
443445
print("\033[2J\033[H", end="")
444-
if self.json:
445-
print(format_workloads_json(sts))
446-
else:
447-
print(format_workloads_table(sts))
446+
match self.format.lower():
447+
case "json":
448+
print(format_workloads_json(sts))
449+
case _:
450+
print(format_workloads_table(sts))
448451
if not self.watch:
449452
break
450453
time.sleep(self.watch)
@@ -458,7 +461,7 @@ class ListWorkloadsSubCommand(SubCommand):
458461
"""
459462

460463
labels: dict[str, str] | None = None
461-
json: bool = False
464+
format: str = "table"
462465
watch: int = 0
463466

464467
@staticmethod
@@ -476,9 +479,11 @@ def register(parser: _SubParsersAction):
476479
)
477480

478481
list_parser.add_argument(
479-
"--json",
480-
action="store_true",
481-
help="output in JSON format",
482+
"--format",
483+
type=str,
484+
choices=["table", "json"],
485+
default="table",
486+
help="output format",
482487
)
483488

484489
list_parser.add_argument(
@@ -492,18 +497,19 @@ def register(parser: _SubParsersAction):
492497

493498
def __init__(self, args: Namespace):
494499
self.labels = args.labels
495-
self.json = args.json
500+
self.format = args.format
496501
self.watch = args.watch
497502

498503
def run(self):
499504
try:
500505
while True:
501506
sts: list[WorkloadStatus] = list_workloads(self.labels)
502507
print("\033[2J\033[H", end="")
503-
if self.json:
504-
print(format_workloads_json(sts))
505-
else:
506-
print(format_workloads_table(sts))
508+
match self.format.lower():
509+
case "json":
510+
print(format_workloads_json(sts))
511+
case _:
512+
print(format_workloads_table(sts))
507513
if not self.watch:
508514
break
509515
time.sleep(self.watch)
@@ -570,7 +576,7 @@ def run(self):
570576

571577

572578
def format_workloads_json(sts: list[WorkloadStatus]) -> str:
573-
return json.dumps([st.to_dict() for st in sts], indent=2)
579+
return json.dumps([st.__dict__ for st in sts], indent=2)
574580

575581

576582
def format_workloads_table(sts: list[WorkloadStatus], width: int = 100) -> str:

gpustack_runtime/cmds/detector.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class DetectDevicesSubCommand(SubCommand):
1717
Command to detect GPUs and their properties.
1818
"""
1919

20-
json: bool = False
20+
format: str = "table"
2121
watch: int = 0
2222

2323
@staticmethod
@@ -28,10 +28,11 @@ def register(parser: _SubParsersAction):
2828
)
2929

3030
detect_parser.add_argument(
31-
"--json",
32-
"-j",
33-
action="store_true",
34-
help="output in JSON format",
31+
"--format",
32+
type=str,
33+
choices=["table", "json"],
34+
default="table",
35+
help="output format",
3536
)
3637

3738
detect_parser.add_argument(
@@ -44,18 +45,19 @@ def register(parser: _SubParsersAction):
4445
detect_parser.set_defaults(func=DetectDevicesSubCommand)
4546

4647
def __init__(self, args: Namespace):
47-
self.json = args.json
48+
self.format = args.format
4849
self.watch = args.watch
4950

5051
def run(self):
5152
try:
5253
while True:
5354
devs: Devices = detect_devices()
5455
print("\033[2J\033[H", end="")
55-
if self.json:
56-
print(format_devices_json(devs))
57-
else:
58-
print(format_devices_table(devs))
56+
match self.format.lower():
57+
case "json":
58+
print(format_devices_json(devs))
59+
case _:
60+
print(format_devices_table(devs))
5961
if not self.watch:
6062
break
6163
time.sleep(self.watch)

0 commit comments

Comments
 (0)