Skip to content

Commit

Permalink
feat(cli): allow setting default workspaces and clusters per profile
Browse files Browse the repository at this point in the history
  • Loading branch information
fubuloubu committed Aug 2, 2024
1 parent c3f62f9 commit 4d29fea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
28 changes: 26 additions & 2 deletions silverback/_click_ext.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from functools import update_wrapper
from pathlib import Path

import click
from fief_client import Fief
Expand Down Expand Up @@ -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.<profile-name>.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.<profile-name>.default-cluster.<workspace-name>`"
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`",
Expand Down
2 changes: 2 additions & 0 deletions silverback/cluster/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 4d29fea

Please sign in to comment.