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

Commit 104de0f

Browse files
committed
Implement magic opt_output_auto
1 parent fc69069 commit 104de0f

4 files changed

Lines changed: 39 additions & 7 deletions

File tree

packages/jumpstarter-cli-admin/jumpstarter_cli_admin/create.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
opt_log_level,
1313
opt_namespace,
1414
opt_nointeractive,
15-
opt_output_all,
15+
opt_output_auto,
1616
)
1717
from jumpstarter_kubernetes import ClientsV1Alpha1Api, ExportersV1Alpha1Api, V1Alpha1Client, V1Alpha1Exporter
1818
from kubernetes_asyncio.client.exceptions import ApiException
@@ -71,7 +71,7 @@ def print_created_client(client: V1Alpha1Client, output: OutputType):
7171
@opt_context
7272
@opt_oidc_username
7373
@opt_nointeractive
74-
@opt_output_all
74+
@opt_output_auto(V1Alpha1Client)
7575
async def create_client(
7676
name: Optional[str],
7777
kubeconfig: Optional[str],
@@ -148,7 +148,7 @@ def print_created_exporter(exporter: V1Alpha1Exporter, output: OutputType):
148148
@opt_context
149149
@opt_oidc_username
150150
@opt_nointeractive
151-
@opt_output_all
151+
@opt_output_auto(V1Alpha1Exporter)
152152
async def create_exporter(
153153
name: Optional[str],
154154
kubeconfig: Optional[str],

packages/jumpstarter-cli-admin/jumpstarter_cli_admin/get.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
opt_kubeconfig,
1010
opt_log_level,
1111
opt_namespace,
12-
opt_output_all,
12+
opt_output_auto,
1313
)
1414
from jumpstarter_kubernetes import (
1515
ClientsV1Alpha1Api,
1616
ExportersV1Alpha1Api,
1717
LeasesV1Alpha1Api,
18+
V1Alpha1Client,
19+
V1Alpha1Exporter,
20+
V1Alpha1Lease,
1821
)
1922
from kubernetes_asyncio.client.exceptions import ApiException
2023
from kubernetes_asyncio.config.config_exception import ConfigException
@@ -41,7 +44,7 @@ def get(log_level: Optional[str]):
4144
@opt_namespace
4245
@opt_kubeconfig
4346
@opt_context
44-
@opt_output_all
47+
@opt_output_auto(V1Alpha1Client)
4548
async def get_client(
4649
name: Optional[str], kubeconfig: Optional[str], context: Optional[str], namespace: str, output: OutputType
4750
):
@@ -65,7 +68,7 @@ async def get_client(
6568
@opt_namespace
6669
@opt_kubeconfig
6770
@opt_context
68-
@opt_output_all
71+
@opt_output_auto(V1Alpha1Exporter)
6972
@click.option("-d", "--devices", is_flag=True, help="Display the devices hosted by the exporter(s)")
7073
async def get_exporter(
7174
name: Optional[str],
@@ -95,7 +98,7 @@ async def get_exporter(
9598
@opt_namespace
9699
@opt_kubeconfig
97100
@opt_context
98-
@opt_output_all
101+
@opt_output_auto(V1Alpha1Lease)
99102
async def get_lease(
100103
name: Optional[str], kubeconfig: Optional[str], context: Optional[str], namespace: str, output: OutputType
101104
):

packages/jumpstarter-cli-common/jumpstarter_cli_common/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
opt_namespace,
1414
opt_nointeractive,
1515
opt_output_all,
16+
opt_output_auto,
1617
opt_output_name_only,
1718
opt_output_path_only,
1819
)
@@ -34,6 +35,7 @@
3435
"opt_output_all",
3536
"opt_output_name_only",
3637
"opt_output_path_only",
38+
"opt_output_auto",
3739
"OutputMode",
3840
"OutputType",
3941
"NameOutputType",

packages/jumpstarter-cli-common/jumpstarter_cli_common/opt.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,30 @@
5555
opt_nointeractive = click.option(
5656
"--nointeractive", is_flag=True, default=False, help="Disable interactive prompts (for use in scripts)."
5757
)
58+
59+
60+
def opt_output_auto(cls):
61+
choices = []
62+
if hasattr(cls, "dump_json"):
63+
choices.append(OutputMode.JSON)
64+
if hasattr(cls, "dump_yaml"):
65+
choices.append(OutputMode.YAML)
66+
if hasattr(cls, "dump_name"):
67+
choices.append(OutputMode.NAME)
68+
if hasattr(cls, "dump_path"):
69+
choices.append(OutputMode.PATH)
70+
71+
if OutputMode.PATH in choices:
72+
help = 'Output mode. Use "-o path" for shorter output (file/path).'
73+
elif OutputMode.NAME in choices:
74+
help = 'Output mode. Use "-o name" for shorter output (resource/name).'
75+
else:
76+
help = "Output mode."
77+
78+
return click.option(
79+
"-o",
80+
"--output",
81+
type=click.Choice(choices),
82+
default=None,
83+
help=help,
84+
)

0 commit comments

Comments
 (0)