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

Commit 10d53b6

Browse files
committed
Dedup client config handling
1 parent d193383 commit 10d53b6

6 files changed

Lines changed: 34 additions & 38 deletions

File tree

packages/jumpstarter-cli-client/jumpstarter_cli_client/client_shell.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@
33
import asyncclick as click
44
from jumpstarter_cli_common.exceptions import handle_exceptions
55

6-
from .common import load_context, opt_context, opt_selector_simple, selector_to_labels
6+
from .common import opt_config, opt_selector_simple, selector_to_labels
77
from jumpstarter.common import MetadataFilter
88
from jumpstarter.common.utils import launch_shell
99

1010

1111
@click.command("shell", short_help="Spawns a shell connecting to a leased remote exporter")
1212
@click.option("-n", "--lease", "lease_name", type=str)
13-
@opt_context
13+
@opt_config
1414
@opt_selector_simple
1515
@handle_exceptions
16-
def client_shell(context: str | None, selector: str, lease_name):
16+
def client_shell(config, selector: str, lease_name):
1717
"""Spawns a shell connecting to a leased remote exporter"""
18-
config = load_context(context)
1918

2019
exit_code = 0
2120

packages/jumpstarter-cli-client/jumpstarter_cli_client/common.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
UserConfigV1Alpha1,
99
)
1010

11-
opt_context = click.option("--client", "--context", "context", help="Name of client config")
12-
1311
opt_selector_simple = click.option(
1412
"-l",
1513
"--selector",
@@ -24,17 +22,20 @@ def selector_to_labels(selector: str):
2422
return dict([term.split("=") for term in selector.split(",")])
2523

2624

27-
def load_context(context: str | None) -> ClientConfigV1Alpha1:
28-
if context:
29-
config = ClientConfigV1Alpha1.load(context)
30-
else:
31-
config = UserConfigV1Alpha1.load_or_create().config.current_client
32-
if not config:
33-
raise click.BadOptionUsage(
34-
"--context",
35-
"no client context specified, and no default client context set",
36-
)
37-
return config
25+
class ClientParamType(click.ParamType):
26+
name = "client"
27+
28+
def convert(self, value, param, ctx):
29+
if isinstance(value, ClientConfigV1Alpha1):
30+
return value
31+
32+
if isinstance(value, bool): # hack to allow loading the default config
33+
config = UserConfigV1Alpha1.load_or_create().config.current_client
34+
if config is None:
35+
self.fail("no client config specified, and no default client config set", param, ctx)
36+
return config
37+
else:
38+
return ClientConfigV1Alpha1.load(value)
3839

3940

4041
class DurationParamType(click.ParamType):
@@ -51,3 +52,6 @@ def convert(self, value, param, ctx):
5152

5253

5354
DURATION = DurationParamType()
55+
CLIENT = ClientParamType()
56+
57+
opt_config = click.option("--client", "config", type=CLIENT, default=False, help="Name of client config")

packages/jumpstarter-cli-client/jumpstarter_cli_client/create.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
)
1010
from jumpstarter_cli_common.exceptions import handle_exceptions
1111

12-
from .common import DURATION, load_context, opt_context, opt_selector_simple
12+
from .common import DURATION, opt_config, opt_selector_simple
1313

1414

1515
@click.group()
@@ -20,12 +20,12 @@ def create():
2020

2121

2222
@create.command(name="lease")
23-
@opt_context
23+
@opt_config
2424
@opt_selector_simple
2525
@click.option("--duration", "duration", type=DURATION, required=True)
2626
@opt_output_all
2727
@handle_exceptions
28-
async def create_lease(context: str | None, selector: str, duration: timedelta, output: OutputType):
28+
async def create_lease(config, selector: str, duration: timedelta, output: OutputType):
2929
"""
3030
Create a lease
3131
@@ -50,7 +50,6 @@ async def create_lease(context: str | None, selector: str, duration: timedelta,
5050
$ jmp client delete lease "${JMP_LEASE}"
5151
5252
"""
53-
config = load_context(context)
5453

5554
lease = config.create_lease(selector=selector, duration=duration)
5655

packages/jumpstarter-cli-client/jumpstarter_cli_client/delete.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from jumpstarter_cli_common import OutputMode, OutputType, opt_output_name_only
33
from jumpstarter_cli_common.exceptions import handle_exceptions
44

5-
from .common import load_context, opt_context
5+
from .common import opt_config
66

77

88
@click.group()
@@ -13,18 +13,16 @@ def delete():
1313

1414

1515
@delete.command(name="leases")
16-
@opt_context
16+
@opt_config
1717
@click.argument("name", required=False)
1818
@click.option("--all", "all", is_flag=True)
1919
@opt_output_name_only
2020
@handle_exceptions
21-
def delete_leases(context: str | None, name: str, all: bool, output: OutputType):
21+
def delete_leases(config, name: str, all: bool, output: OutputType):
2222
"""
2323
Delete leases
2424
"""
2525

26-
config = load_context(context)
27-
2826
names = []
2927

3028
if name is not None:

packages/jumpstarter-cli-client/jumpstarter_cli_client/get.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from jumpstarter_cli_common import OutputMode, OutputType, make_table, opt_output_all
33
from jumpstarter_cli_common.exceptions import handle_exceptions
44

5-
from .common import load_context, opt_context
5+
from .common import opt_config
66

77
opt_selector = click.option(
88
"-l",
@@ -20,15 +20,14 @@ def get():
2020

2121

2222
@get.command(name="exporters")
23-
@opt_context
23+
@opt_config
2424
@opt_selector
2525
@opt_output_all
2626
@handle_exceptions
27-
def get_exporters(context: str | None, selector: str | None, output: OutputType):
27+
def get_exporters(config, selector: str | None, output: OutputType):
2828
"""
2929
Display one or many exporters
3030
"""
31-
config = load_context(context)
3231

3332
exporters = config.list_exporters(filter=selector)
3433

@@ -53,15 +52,14 @@ def get_exporters(context: str | None, selector: str | None, output: OutputType)
5352

5453

5554
@get.command(name="leases")
56-
@opt_context
55+
@opt_config
5756
@opt_selector
5857
@opt_output_all
5958
@handle_exceptions
60-
def get_leases(context: str | None, selector: str | None, output: OutputType):
59+
def get_leases(config, selector: str | None, output: OutputType):
6160
"""
6261
Display one or many leases
6362
"""
64-
config = load_context(context)
6563

6664
leases = config.list_leases(filter=selector)
6765

packages/jumpstarter-cli-client/jumpstarter_cli_client/update.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from jumpstarter_cli_common import OutputMode, OutputType, make_table, opt_output_all
55
from jumpstarter_cli_common.exceptions import handle_exceptions
66

7-
from .common import DURATION, load_context, opt_context
7+
from .common import DURATION, opt_config
88

99

1010
@click.group()
@@ -15,18 +15,16 @@ def update():
1515

1616

1717
@update.command(name="lease")
18-
@opt_context
18+
@opt_config
1919
@click.argument("name")
2020
@click.option("--duration", "duration", type=DURATION, required=True)
2121
@opt_output_all
2222
@handle_exceptions
23-
async def update_lease(context: str | None, name: str, duration: timedelta, output: OutputType):
23+
async def update_lease(config, name: str, duration: timedelta, output: OutputType):
2424
"""
2525
Update a lease
2626
"""
2727

28-
config = load_context(context)
29-
3028
lease = config.update_lease(name, duration)
3129

3230
match output:

0 commit comments

Comments
 (0)