Skip to content

Commit

Permalink
feat(project): adds yaspin implementation to project commands
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabpain committed Aug 3, 2023
1 parent 2f73774 commit 44a5a1c
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 46 deletions.
32 changes: 24 additions & 8 deletions riocli/project/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,33 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_spinner import spinner
from click_help_colors import HelpColorsCommand

from riocli.config import new_v2_client
from riocli.constants import Symbols, Colors
from riocli.project.util import name_to_organization_guid
from riocli.utils.spinner import with_spinner


@click.command('create')
@click.command(
'create',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.argument('project-name', type=str)
@click.option('--organization', 'organization_name',
help='Pass organization name for which project needs to be created. Default will be current organization')
@click.pass_context
@name_to_organization_guid
def create_project(ctx: click.Context, project_name: str,
organization_guid: str, organization_name: str) -> None:
@with_spinner(text="Creating project...")
def create_project(
ctx: click.Context,
project_name: str,
organization_guid: str,
organization_name: str,
spinner=None,
) -> None:
"""
Creates a new project
"""
Expand All @@ -45,9 +58,12 @@ def create_project(ctx: click.Context, project_name: str,

try:
client = new_v2_client(with_project=False)
with spinner():
client.create_project(payload)
click.secho('Project created successfully!', fg='green')
client.create_project(payload)
spinner.text = click.style(
'Project created successfully.', fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
click.secho('failed to create project: {}'.format(e), fg='red')
spinner.text = click.style(
'Failed to create project: {}'.format(e), Colors.RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1)
40 changes: 28 additions & 12 deletions riocli/project/delete.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,31 +12,47 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_spinner import spinner
from click_help_colors import HelpColorsCommand

from riocli.config import new_v2_client
from riocli.constants import Symbols, Colors
from riocli.project.util import name_to_guid
from riocli.utils.spinner import with_spinner


@click.command('delete')
@click.command(
'delete',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.option('--force', '-f', '--silent', 'force', is_flag=True,
help='Skip confirmation')
@click.argument('project-name', type=str)
@name_to_guid
def delete_project(force: bool, project_name: str, project_guid: str) -> None:
@with_spinner(text="Deleting project...")
def delete_project(
force: bool,
project_name: str,
project_guid: str,
spinner=None,
) -> None:
"""
Deletes the project from the Platform
Deletes a project
"""
if not force:
click.confirm(
'Deleting project {} ({})'.format(project_name, project_guid),
abort=True)
with spinner.hidden():
click.confirm('Deleting project {} ({})'.format(
project_name, project_guid), abort=True)

try:
client = new_v2_client()
with spinner():
client.delete_project(project_guid)
click.secho('Project deleted successfully!', fg='green')
client.delete_project(project_guid)
spinner.text = click.style(
'Project deleted successfully.', fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
click.secho('failed to delete project: {}'.format(e), fg='red')
spinner.text = click.style(
'Failed to delete project: {}'.format(e), Colors.RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1)
5 changes: 3 additions & 2 deletions riocli/project/features/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
import click
from click_help_colors import HelpColorsGroup

from riocli.constants import Colors
from riocli.project.features.vpn import vpn


@click.group(
invoke_without_command=False,
cls=HelpColorsGroup,
help_headers_color='yellow',
help_options_color='green',
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
def features():
"""
Expand Down
30 changes: 24 additions & 6 deletions riocli/project/features/vpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,30 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_help_colors import HelpColorsCommand

from riocli.config import new_v2_client
from riocli.constants import Colors, Symbols
from riocli.project.util import name_to_guid
from riocli.utils.spinner import with_spinner


@click.command('vpn')
@click.command(
'vpn',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.argument('project-name', type=str)
@click.argument('enable', type=bool)
@name_to_guid
def vpn(project_name: str, project_guid: str, enable: bool) -> None:
@with_spinner()
def vpn(
project_name: str,
project_guid: str,
enable: bool,
spinner=None,
) -> None:
"""
Enable or disable VPN on a project
Expand All @@ -40,10 +54,14 @@ def vpn(project_name: str, project_guid: str, enable: bool) -> None:
}
}

state = 'Enabling' if enable else 'Disabling'
spinner.text = click.style('{} VPN...'.format(state), fg=Colors.YELLOW)

try:
r = client.update_project(project_guid, body)
client.update_project(project_guid, body)
spinner.text = click.style('Done', fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
click.secho("❌ Failed: {}".format(e), fg='red')
spinner.text = click.style("Failed: {}".format(e), fg=Colors.RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1) from e

click.secho("✅ VPN has been {}".format("enabled" if enable else "disabled"), fg='green')
11 changes: 9 additions & 2 deletions riocli/project/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_help_colors import HelpColorsCommand
from munch import unmunchify

from riocli.config import new_v2_client
from riocli.constants import Colors
from riocli.project.util import name_to_guid
from riocli.utils import inspect_with_format


@click.command('inspect')
@click.command(
'inspect',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.option('--format', '-f', 'format_type', default='yaml',
type=click.Choice(['json', 'yaml'], case_sensitive=False))
@click.argument('project-name', type=str)
Expand All @@ -34,5 +41,5 @@ def inspect_project(format_type: str, project_name: str,
project = client.get_project(project_guid)
inspect_with_format(unmunchify(project), format_type)
except Exception as e:
click.secho(str(e), fg='red')
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)
19 changes: 13 additions & 6 deletions riocli/project/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@
import typing

import click
from click_help_colors import HelpColorsCommand
from munch import unmunchify
from rapyuta_io import Project

from riocli.config import new_v2_client
from riocli.constants import Colors
from riocli.project.util import name_to_organization_guid
from riocli.utils import tabulate_data


@click.command('list')
@click.command(
'list',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.option('--organization', 'organization_name',
help='Pass organization name for which project needs to be created. Default will be current organization')
@click.option('--wide', '-w', is_flag=True, default=False,
Expand All @@ -41,7 +48,7 @@ def list_project(ctx: click.Context = None, organization_guid: str = None,
current = ctx.obj.data.get('project_id', None)
_display_project_list(projects, current, show_header=True, wide=wide)
except Exception as e:
click.secho(str(e), fg='red')
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)


Expand All @@ -57,14 +64,14 @@ def _display_project_list(projects: typing.List[Project], current: str = None,
data = []
for project in projects:
metadata = project.metadata
fg = None
fg, bold = None, False
if metadata.guid == current:
fg = 'green'

fg = Colors.GREEN
bold = True
row = [metadata.guid, metadata.name, project.status.status]
if wide:
row.extend([metadata.createdAt, metadata.creatorGUID,
unmunchify(project.spec.features)])
data.append([click.style(v, fg=fg) for v in row])
data.append([click.style(v, fg=fg, bold=bold) for v in row])

tabulate_data(data, headers)
4 changes: 3 additions & 1 deletion riocli/project/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

PROJECT_READY_TIMEOUT = 150


class Project(Model):

def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -52,7 +53,8 @@ def create_object(self, client: Client) -> typing.Any:
r = client.create_project(project)

try:
wait(self.is_ready, timeout_seconds=PROJECT_READY_TIMEOUT, sleep_seconds=(1, 30, 2))
wait(self.is_ready, timeout_seconds=PROJECT_READY_TIMEOUT,
sleep_seconds=(1, 30, 2))
except TimeoutExpired as e:
raise e

Expand Down
28 changes: 21 additions & 7 deletions riocli/project/select.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,24 +12,38 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_help_colors import HelpColorsCommand

from riocli.constants import Colors, Symbols
from riocli.project.util import name_to_guid
from riocli.utils.context import get_root_context


@click.command('select')
@click.command(
'select',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.argument('project-name', type=str)
@name_to_guid
@click.pass_context
def select_project(ctx: click.Context, project_name: str,
project_guid: str) -> None:
def select_project(
ctx: click.Context,
project_name: str,
project_guid: str,
) -> None:
"""
Sets the given project in the CLI context. All other resources use this project to act upon.
"""
ctx = get_root_context(ctx)

ctx.obj.data['project_id'] = project_guid
ctx.obj.data['project_name'] = project_name
ctx.obj.save()
click.secho(
'Project {} ({}) is selected!'.format(project_name, project_guid),
fg='green')

click.secho('{} Project {} ({}) is selected!'.format(
Symbols.SUCCESS,
project_name,
project_guid),
fg=Colors.GREEN)
5 changes: 3 additions & 2 deletions riocli/project/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from rapyuta_io import Client

from riocli.config import new_client, new_v2_client
from riocli.constants import Colors
from riocli.utils.selector import show_selection
from riocli.v2client import Client as v2Client

Expand All @@ -40,7 +41,7 @@ def decorated(**kwargs: typing.Any):
try:
guid = find_project_guid(client, name)
except Exception as e:
click.secho(str(e), fg='red')
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)

kwargs['project_name'] = name
Expand Down Expand Up @@ -110,7 +111,7 @@ def decorated(*args: typing.Any, **kwargs: typing.Any):
else:
guid = find_organization_guid(client, name)
except Exception as e:
click.secho(str(e), fg='red')
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)
kwargs['organization_name'] = name
kwargs['organization_guid'] = guid
Expand Down

0 comments on commit 44a5a1c

Please sign in to comment.