From 4d29feaca03655e806d629e0e20250a5b52e3734 Mon Sep 17 00:00:00 2001 From: fubuloubu <3859395+fubuloubu@users.noreply.github.com> Date: Fri, 2 Aug 2024 16:54:51 -0400 Subject: [PATCH] feat(cli): allow setting default workspaces and clusters per profile --- silverback/_click_ext.py | 28 ++++++++++++++++++++++++++-- silverback/cluster/settings.py | 2 ++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/silverback/_click_ext.py b/silverback/_click_ext.py index 61dce124..f0fc7c02 100644 --- a/silverback/_click_ext.py +++ b/silverback/_click_ext.py @@ -1,4 +1,5 @@ from functools import update_wrapper +from pathlib import Path import click from fief_client import Fief @@ -177,10 +178,33 @@ def cluster_client(f): def inject_cluster(ctx, param, value: str | None): ctx.obj = ctx.obj or {} - if isinstance(ctx.obj.get("profile"), ClusterProfile): + if isinstance(profile := ctx.obj.get("profile"), ClusterProfile): return value # Ignore processing this for cluster clients - elif value is None or "/" not in value or len(parts := value.split("/")) > 2: + elif profile is None: + raise AssertionError("Shouldn't happen, fix cli") + + elif value is None or "/" not in value: + if profile.default_workspace is None: + raise click.UsageError( + "Must provide `-c CLUSTER`, or set `profile..default-workspace`" + f"in your `~/{PROFILE_PATH.relative_to(Path.home())}`" + ) + + if value is None and profile.default_workspace not in profile.default_cluster: + raise click.UsageError( + "Must provide `-c CLUSTER`, or set " + "`profile..default-cluster.`" + f"in your `~/{PROFILE_PATH.relative_to(Path.home())}`" + ) + + parts = [ + profile.default_workspace, + # NOTE: `value` works as cluster selector, if set + value or profile.default_cluster[profile.default_workspace], + ] + + elif len(parts := value.split("/")) > 2: raise click.BadParameter( param=param, message="CLUSTER should be in format `WORKSPACE/NAME`", diff --git a/silverback/cluster/settings.py b/silverback/cluster/settings.py index 3e4fa21e..e92c53a7 100644 --- a/silverback/cluster/settings.py +++ b/silverback/cluster/settings.py @@ -27,6 +27,8 @@ class ClusterProfile(BaseProfile): class PlatformProfile(BaseProfile): auth: str # key of `AuthenticationConfig` in authentication section + default_workspace: str | None = Field(alias="default-workspace", default=None) + default_cluster: dict[str, str] = Field(alias="default-cluster", default_factory=dict) class ProfileSettings(BaseModel):