Skip to content

Commit

Permalink
✨ feat(static-route): updates spinner and other implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabpain committed Jul 20, 2023
1 parent d7bafdf commit 9a7a0b2
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 63 deletions.
5 changes: 3 additions & 2 deletions riocli/static_route/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import click
from click_help_colors import HelpColorsGroup

from riocli.constants import Colors
from riocli.static_route.create import create_static_route
from riocli.static_route.delete import delete_static_route
from riocli.static_route.inspect import inspect_static_route
Expand All @@ -24,8 +25,8 @@
@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 static_route() -> None:
"""
Expand Down
28 changes: 19 additions & 9 deletions riocli/static_route/create.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,22 +12,32 @@
# 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_client
from riocli.constants import Colors, Symbols
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('prefix', type=str)
def create_static_route(prefix: str) -> None:
@with_spinner(text="Creating static route...")
def create_static_route(prefix: str, spinner=None) -> None:
"""
Creates a new instance of static route
Creates a new static route
"""
try:
client = new_client()
with spinner():
route = client.create_static_route(prefix)
click.secho("Static Route created successfully for URL {}".format(route.urlString), fg='green')
route = client.create_static_route(prefix)
spinner.text = click.style(
'Static Route created successfully for URL {}'.format(route.urlString), fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
click.secho(str(e), fg='red')
spinner.text = click.style('Failed to create static route: {}'.format(e), fg=Colors.RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1)
42 changes: 29 additions & 13 deletions riocli/static_route/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,30 +12,46 @@
# 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_client
from riocli.constants import Colors, Symbols
from riocli.static_route.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', is_flag=True, default=False, help='Skip confirmation')
@click.argument('static-route', type=str)
@name_to_guid
def delete_static_route(static_route: str, static_route_guid: str, force: bool) -> None:
@with_spinner(text="Deleting static route...")
def delete_static_route(
static_route: str,
static_route_guid: str,
force: bool,
spinner=None,
) -> None:
"""
Deletes the static route resource from the Platform
Deletes a static route
"""

if not force:
click.confirm('Deleting static route {} ({})'.format(static_route, static_route_guid),
abort=True)
with spinner.hidden():
if not force:
click.confirm(
'Deleting static route {} ({})'.format(
static_route, static_route_guid), abort=True)

try:
client = new_client()
with spinner():
client.delete_static_route(static_route_guid)
click.secho('Static Route deleted successfully!', fg='green')
client.delete_static_route(static_route_guid)
spinner.text = click.style(
'Static Route deleted successfully ', fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
click.secho(str(e), fg='red')
spinner.text = click.style('Failed to delete static route: {}'.format(e), fg=Colors.RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1)
24 changes: 18 additions & 6 deletions riocli/static_route/inspect.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,29 +12,41 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_help_colors import HelpColorsCommand
from rapyuta_io.clients.static_route import StaticRoute

from riocli.config import new_client
from riocli.constants import Colors
from riocli.static_route.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',
type=click.Choice(['json', 'yaml'], case_sensitive=True), default='yaml')
type=click.Choice(['json', 'yaml'], case_sensitive=True),
default='yaml')
@click.argument('static-route', type=str)
@name_to_guid
def inspect_static_route(format_type: str, static_route: str, static_route_guid: str) -> None:
def inspect_static_route(
format_type: str,
static_route: str,
static_route_guid: str
) -> None:
"""
Inspect the static route resource
Inspect a static route
"""
try:
client = new_client()
route = client.get_static_route(static_route_guid)
data = make_static_route_inspectable(route)
inspect_with_format(data, format_type)
except Exception as e:
click.secho(str(e), fg='red')
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)


Expand Down
36 changes: 31 additions & 5 deletions riocli/static_route/list.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 @@ -11,21 +11,47 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import List

import click
from click_help_colors import HelpColorsCommand
from rapyuta_io.clients.static_route import StaticRoute

from riocli.config import new_client
from riocli.static_route.util import repr_static_routes
from riocli.constants import Colors
from riocli.utils import tabulate_data


@click.command('list')
@click.command(
'list',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
def list_static_routes() -> None:
"""
List the static routes in the selected project
"""
try:
client = new_client()
routes = client.get_all_static_routes()
repr_static_routes(routes)
_display_routes_list(routes)
except Exception as e:
click.secho(str(e), fg='red')
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)


def _display_routes_list(routes: List[StaticRoute]) -> None:
headers = ['Route ID', 'Name', 'URL', 'Creator', 'CreatedAt']

data = []
for route in routes:
data.append([
route.guid,
route.urlPrefix,
route.urlString,
route.creator,
route.CreatedAt,
])

tabulate_data(data, headers)
9 changes: 6 additions & 3 deletions riocli/static_route/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 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 @@ -25,8 +25,11 @@ def __init__(self, *args, **kwargs):
self.update(*args, **kwargs)

def find_object(self, client: Client) -> bool:
_, static_route = self.rc.find_depends({'kind': 'staticroute',
'nameOrGUID': self.metadata.name})
_, static_route = self.rc.find_depends({
'kind': 'staticroute',
'nameOrGUID': self.metadata.name
})

if not static_route:
return False

Expand Down
13 changes: 10 additions & 3 deletions riocli/static_route/open.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,12 +12,19 @@
# 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_client
from riocli.constants import Colors
from riocli.static_route.util import name_to_guid


@click.command('open')
@click.command(
'open',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.argument('static-route', type=str)
@name_to_guid
def open_static_route(static_route, static_route_guid) -> None:
Expand All @@ -29,5 +36,5 @@ def open_static_route(static_route, static_route_guid) -> None:
route = client.get_static_route(static_route_guid)
click.launch(url='https://{}'.format(route.urlString), wait=False)
except Exception as e:
click.secho(str(e), fg='red')
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)
25 changes: 3 additions & 22 deletions riocli/static_route/util.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 @@ -14,9 +14,7 @@
import functools
import typing

import click
from rapyuta_io import Client
from rapyuta_io.clients.static_route import StaticRoute

from riocli.config import new_client

Expand Down Expand Up @@ -59,23 +57,6 @@ def find_static_route_guid(client: Client, name: str) -> str:
raise StaticRouteNotFound()


def repr_static_routes(routes: typing.List[StaticRoute]) -> None:
header = '{:<36} {:<25} {:36} {:36} {:32}'.format(
'Static Route ID',
'Name',
'Full URL',
'Creator',
'Created At',
)
click.echo(click.style(header, fg='yellow'))
for route in routes:
click.secho(
'{:<36} {:<25} {:36} {:36} {:32}'.
format(route.guid, route.urlPrefix, route.urlString, route.creator,
route.CreatedAt))


class StaticRouteNotFound(Exception):
def __init__(self, message='secret not found'):
self.message = message
super().__init__(self.message)
def __init__(self):
super().__init__('static route not found')

0 comments on commit 9a7a0b2

Please sign in to comment.