Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Commit c6a696a

Browse files
committed
Use output_mode_auto in more places
1 parent cf17e0c commit c6a696a

4 files changed

Lines changed: 78 additions & 79 deletions

File tree

packages/jumpstarter-cli/jumpstarter_cli/create.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
import asyncclick as click
44
from jumpstarter_cli_common import (
5-
OutputMode,
65
OutputType,
6+
echo,
77
make_table,
88
opt_config,
9-
opt_output_all,
9+
opt_output_auto,
1010
)
1111
from jumpstarter_cli_common.exceptions import handle_exceptions
1212

1313
from .common import opt_duration_partial, opt_selector
14+
from jumpstarter.client.grpc import Lease
1415

1516

1617
@click.group()
@@ -24,7 +25,7 @@ def create():
2425
@opt_config(exporter=False)
2526
@opt_selector
2627
@opt_duration_partial(required=True)
27-
@opt_output_all
28+
@opt_output_auto(Lease)
2829
@handle_exceptions
2930
async def create_lease(config, selector: str, duration: timedelta, output: OutputType):
3031
"""
@@ -54,20 +55,17 @@ async def create_lease(config, selector: str, duration: timedelta, output: Outpu
5455

5556
lease = config.create_lease(selector=selector, duration=duration)
5657

57-
match output:
58-
case OutputMode.JSON | OutputMode.YAML:
59-
click.echo(lease.dump(output))
60-
case OutputMode.NAME:
61-
click.echo(lease.name)
62-
case _:
63-
columns = ["NAME", "SELECTOR", "DURATION", "CLIENT", "EXPORTER"]
64-
rows = [
65-
{
66-
"NAME": lease.name,
67-
"SELECTOR": lease.selector,
68-
"DURATION": str(lease.duration.total_seconds()),
69-
"CLIENT": lease.client,
70-
"EXPORTER": lease.exporter,
71-
}
72-
]
73-
click.echo(make_table(columns, rows))
58+
if output:
59+
echo(lease.dump(output))
60+
else:
61+
columns = ["NAME", "SELECTOR", "DURATION", "CLIENT", "EXPORTER"]
62+
rows = [
63+
{
64+
"NAME": lease.name,
65+
"SELECTOR": lease.selector,
66+
"DURATION": str(lease.duration.total_seconds()),
67+
"CLIENT": lease.client,
68+
"EXPORTER": lease.exporter,
69+
}
70+
]
71+
click.echo(make_table(columns, rows))
Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import asyncclick as click
2-
from jumpstarter_cli_common import OutputMode, OutputType, make_table, opt_config, opt_output_all
2+
from jumpstarter_cli_common import OutputType, make_table, opt_config, opt_output_auto
33
from jumpstarter_cli_common.exceptions import handle_exceptions
44

55
from .common import opt_selector
6+
from jumpstarter.client.grpc import ExporterList, LeaseList
67

78

89
@click.group()
@@ -15,7 +16,7 @@ def get():
1516
@get.command(name="exporters")
1617
@opt_config(exporter=False)
1718
@opt_selector
18-
@opt_output_all
19+
@opt_output_auto(ExporterList)
1920
@handle_exceptions
2021
def get_exporters(config, selector: str | None, output: OutputType):
2122
"""
@@ -24,28 +25,24 @@ def get_exporters(config, selector: str | None, output: OutputType):
2425

2526
exporters = config.list_exporters(filter=selector)
2627

27-
match output:
28-
case OutputMode.JSON | OutputMode.YAML:
29-
click.echo(exporters.dump(output))
30-
case OutputMode.NAME:
31-
for exporter in exporters.exporters:
32-
click.echo(exporter.name)
33-
case _:
34-
columns = ["NAME", "LABELS"]
35-
rows = [
36-
{
37-
"NAME": exporter.name,
38-
"LABELS": ",".join(("{}={}".format(i[0], i[1]) for i in exporter.labels.items())),
39-
}
40-
for exporter in exporters.exporters
41-
]
42-
click.echo(make_table(columns, rows))
28+
if output:
29+
click.echo(exporters.dump(output))
30+
else:
31+
columns = ["NAME", "LABELS"]
32+
rows = [
33+
{
34+
"NAME": exporter.name,
35+
"LABELS": ",".join(("{}={}".format(i[0], i[1]) for i in exporter.labels.items())),
36+
}
37+
for exporter in exporters.exporters
38+
]
39+
click.echo(make_table(columns, rows))
4340

4441

4542
@get.command(name="leases")
4643
@opt_config(exporter=False)
4744
@opt_selector
48-
@opt_output_all
45+
@opt_output_auto(LeaseList)
4946
@handle_exceptions
5047
def get_leases(config, selector: str | None, output: OutputType):
5148
"""
@@ -54,20 +51,16 @@ def get_leases(config, selector: str | None, output: OutputType):
5451

5552
leases = config.list_leases(filter=selector)
5653

57-
match output:
58-
case OutputMode.JSON | OutputMode.YAML:
59-
click.echo(leases.dump(output))
60-
case OutputMode.NAME:
61-
for lease in leases.leases:
62-
click.echo(lease.name)
63-
case _:
64-
columns = ["NAME", "CLIENT", "EXPORTER"]
65-
rows = [
66-
{
67-
"NAME": lease.name,
68-
"CLIENT": lease.client,
69-
"EXPORTER": lease.exporter,
70-
}
71-
for lease in leases.leases
72-
]
73-
click.echo(make_table(columns, rows))
54+
if output:
55+
click.echo(leases.dump(output))
56+
else:
57+
columns = ["NAME", "CLIENT", "EXPORTER"]
58+
rows = [
59+
{
60+
"NAME": lease.name,
61+
"CLIENT": lease.client,
62+
"EXPORTER": lease.exporter,
63+
}
64+
for lease in leases.leases
65+
]
66+
click.echo(make_table(columns, rows))
Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from datetime import timedelta
22

33
import asyncclick as click
4-
from jumpstarter_cli_common import OutputMode, OutputType, make_table, opt_config, opt_output_all
4+
from jumpstarter_cli_common import OutputType, echo, make_table, opt_config, opt_output_auto
55
from jumpstarter_cli_common.exceptions import handle_exceptions
66

77
from .common import opt_duration_partial
8+
from jumpstarter.client.grpc import Lease
89

910

1011
@click.group()
@@ -18,7 +19,7 @@ def update():
1819
@opt_config(exporter=False)
1920
@click.argument("name")
2021
@opt_duration_partial(required=True)
21-
@opt_output_all
22+
@opt_output_auto(Lease)
2223
@handle_exceptions
2324
async def update_lease(config, name: str, duration: timedelta, output: OutputType):
2425
"""
@@ -27,20 +28,17 @@ async def update_lease(config, name: str, duration: timedelta, output: OutputTyp
2728

2829
lease = config.update_lease(name, duration)
2930

30-
match output:
31-
case OutputMode.JSON | OutputMode.YAML:
32-
click.echo(lease.dump(output))
33-
case OutputMode.NAME:
34-
click.echo(lease.name)
35-
case _:
36-
columns = ["NAME", "SELECTOR", "DURATION", "CLIENT", "EXPORTER"]
37-
rows = [
38-
{
39-
"NAME": lease.name,
40-
"SELECTOR": lease.selector,
41-
"DURATION": str(lease.duration.total_seconds()),
42-
"CLIENT": lease.client,
43-
"EXPORTER": lease.exporter,
44-
}
45-
]
46-
click.echo(make_table(columns, rows))
31+
if output:
32+
echo(lease.dump(output))
33+
else:
34+
columns = ["NAME", "SELECTOR", "DURATION", "CLIENT", "EXPORTER"]
35+
rows = [
36+
{
37+
"NAME": lease.name,
38+
"SELECTOR": lease.selector,
39+
"DURATION": str(lease.duration.total_seconds()),
40+
"CLIENT": lease.client,
41+
"EXPORTER": lease.exporter,
42+
}
43+
]
44+
click.echo(make_table(columns, rows))

packages/jumpstarter/jumpstarter/client/grpc.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ def parse_lease_identifier(identifier: str) -> (str, str):
3535
return parse_identifier(identifier, "leases")
3636

3737

38-
class Exporter(SerializableBaseModel):
38+
class GrpcObject(SerializableBaseModel):
3939
namespace: str
4040
name: str
41+
42+
def dump_name(self) -> str:
43+
return self.name + "\n"
44+
45+
46+
class Exporter(GrpcObject):
4147
labels: dict[str, str]
4248

4349
@classmethod
@@ -46,9 +52,7 @@ def from_protobuf(cls, data: client_pb2.Exporter) -> Exporter:
4652
return cls(namespace=namespace, name=name, labels=data.labels)
4753

4854

49-
class Lease(SerializableBaseModel):
50-
namespace: str
51-
name: str
55+
class Lease(GrpcObject):
5256
selector: str
5357
duration: timedelta
5458
client: str
@@ -97,6 +101,9 @@ class ExporterList(SerializableBaseModel):
97101
exporters: list[Exporter]
98102
next_page_token: str | None = Field(exclude=True)
99103

104+
def dump_name(self) -> str:
105+
return "".join(exporter.dump_name() for exporter in self.exporters)
106+
100107
@classmethod
101108
def from_protobuf(cls, data: client_pb2.ListExportersResponse) -> ExporterList:
102109
return cls(
@@ -109,6 +116,9 @@ class LeaseList(SerializableBaseModel):
109116
leases: list[Lease]
110117
next_page_token: str | None = Field(exclude=True)
111118

119+
def dump_name(self) -> str:
120+
return "".join(lease.dump_name() for lease in self.leases)
121+
112122
@classmethod
113123
def from_protobuf(cls, data: client_pb2.ListLeasesResponse) -> LeaseList:
114124
return cls(

0 commit comments

Comments
 (0)