Skip to content

Commit

Permalink
refactor(cli): new cluster and workspaces (#166)
Browse files Browse the repository at this point in the history
* refactor: newcluster and workspace command

* chore: mypy

* fix: restore UsageError

* fix: combine click.echo
  • Loading branch information
dtdang authored Nov 1, 2024
1 parent 8fc21d8 commit 0c43363
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
41 changes: 20 additions & 21 deletions silverback/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,39 +220,38 @@ def workspace_info(platform: PlatformClient, workspace: str):
"-n",
"--name",
"workspace_name",
required=True,
help="Name for new workspace",
)
@click.option(
"-s",
"--slug",
"workspace_slug",
required=True,
help="Slug for new workspace",
)
@platform_client
def new_workspace(
platform: PlatformClient,
workspace_name: str,
workspace_slug: str,
workspace_name: str | None,
workspace_slug: str | None,
):
"""Create a new workspace"""

if workspace_name:
click.echo(f"name: {workspace_name}")
click.echo(f"slug: {workspace_slug or workspace_name.lower().replace(' ', '-')}")

elif workspace_slug:
click.echo(f"slug: {workspace_slug}")
workspace_name = workspace_name or workspace_slug
workspace_slug = workspace_slug or (
workspace_name.lower().replace(" ", "-") if workspace_name else None
)

else:
if not workspace_name:
raise click.UsageError("Must provide a name or a slug/name combo")

platform.create_workspace(
workspace = platform.create_workspace(
workspace_name=workspace_name,
workspace_slug=workspace_slug,
)
click.echo(f"{click.style('SUCCESS', fg='green')}: Created '{workspace_name}'")
click.echo(
f"{click.style('SUCCESS', fg='green')}: "
f"Created '{workspace.name}' (slug: '{workspace.slug}')"
)


@workspaces.command(name="update", section="Platform Commands (https://silverback.apeworx.io)")
Expand Down Expand Up @@ -356,21 +355,21 @@ def new_cluster(
if not (workspace_client := platform.workspaces.get(workspace)):
raise click.BadOptionUsage("workspace", f"Unknown workspace '{workspace}'")

if cluster_name:
click.echo(f"name: {cluster_name}")
click.echo(f"slug: {cluster_slug or cluster_name.lower().replace(' ', '-')}")

elif cluster_slug:
click.echo(f"slug: {cluster_slug}")
cluster_name = cluster_name or cluster_slug
cluster_slug = cluster_slug or (
cluster_name.lower().replace(" ", "-") if cluster_name else None
)

else:
if not cluster_name:
raise click.UsageError("Must provide a name or a slug/name combo")

cluster = workspace_client.create_cluster(
cluster_name=cluster_name,
cluster_slug=cluster_slug,
)
click.echo(f"{click.style('SUCCESS', fg='green')}: Created '{cluster.name}'")
click.echo(
f"{click.style('SUCCESS', fg='green')}: Created '{cluster.name}' (slug: '{cluster.slug}')"
)

if cluster.status == ResourceStatus.CREATED:
click.echo(
Expand Down
4 changes: 2 additions & 2 deletions silverback/cluster/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,8 @@ def workspaces(self) -> dict[str, Workspace]:

def create_workspace(
self,
workspace_slug: str = "",
workspace_name: str = "",
workspace_slug: str | None = None,
workspace_name: str | None = None,
) -> Workspace:
response = self.post(
"/workspaces",
Expand Down

0 comments on commit 0c43363

Please sign in to comment.