Skip to content

Commit

Permalink
feat(device): adds or updates spinner in device commands
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabpain committed Aug 3, 2023
1 parent 458b44e commit c14ebf4
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 152 deletions.
102 changes: 71 additions & 31 deletions riocli/device/config.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,21 +14,23 @@
import typing

import click
from click_help_colors import HelpColorsCommand
from click_help_colors import HelpColorsGroup
from click_spinner import spinner
from rapyuta_io import DeviceConfig

from riocli.config import new_client
from riocli.constants import Colors, Symbols
from riocli.device.util import name_to_guid
from riocli.utils import tabulate_data
from riocli.utils.spinner import with_spinner


@click.group(
'config',
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 device_config() -> None:
"""
Expand All @@ -37,7 +39,12 @@ def device_config() -> None:
pass


@device_config.command('list')
@device_config.command(
'list',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.argument('device-name', type=str)
@name_to_guid
def list_config(device_name: str, device_guid: str) -> None:
Expand All @@ -54,59 +61,92 @@ def list_config(device_name: str, device_guid: str) -> None:
raise SystemExit(1)


@device_config.command('create')
@device_config.command(
'create',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.argument('device-name', type=str)
@click.argument('key', type=str)
@click.argument('value', type=str)
@name_to_guid
def create_config(device_name: str, device_guid: str, key: str, value: str) -> None:
@with_spinner(text='Creating new config variable...')
def create_config(
device_name: str,
device_guid: str,
key: str,
value: str,
spinner=None,
) -> None:
"""
Create a new config variable on the Device
Create a new config variable on the device
"""
try:
with spinner():
client = new_client()
device = client.get_device(device_id=device_guid)
device.add_config_variable(key, value)
click.secho('Config Variable added successfully!', fg='green')
client = new_client()
device = client.get_device(device_id=device_guid)
device.add_config_variable(key, value)
spinner.text = click.style('Config variable added successfully.', fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
click.secho(str(e), fg='red')
raise SystemExit(1)
spinner.text = click.style('Failed to add config variable: {}'.format(e), fg=Colors.RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1) from e


@device_config.command('update')
@device_config.command(
'update',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.argument('device-name', type=str)
@click.argument('key', type=str)
@click.argument('value', type=str)
@name_to_guid
def update_config(device_name: str, device_guid: str, key: str, value: str) -> None:
@with_spinner(text='Updating config variable...')
def update_config(
device_name: str,
device_guid: str,
key: str,
value: str,
spinner=None,
) -> None:
"""
Update the config variable on the Device
Update the config variable on the device
"""
try:
with spinner():
_update_config_variable(device_guid, key, value)
click.secho('Config variable updated successfully!', fg='green')
_update_config_variable(device_guid, key, value)
spinner.text = click.style('Config variable updated successfully.', fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
click.secho(str(e), fg='red')
raise SystemExit(1)
spinner.text = click.style('Failed to update config variable: {}'.format(e), fg=Colors.RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1) from e


@device_config.command('delete')
@device_config.command(
'delete',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.argument('device-name', type=str)
@click.argument('key', type=str)
@name_to_guid
def delete_config(device_name: str, device_guid: str, key: str) -> None:
@with_spinner(text='Deleting config variable...')
def delete_config(device_name: str, device_guid: str, key: str, spinner=None) -> None:
"""
Delete the config variable on the Device
Delete the config variable on the device
"""
try:
with spinner():
_delete_config_variable(device_guid, key)
click.secho('Config variable deleted successfully!', fg='green')
_delete_config_variable(device_guid, key)
spinner.text = click.style('Config variable deleted successfully.', fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
click.secho(str(e), fg='red')
raise SystemExit(1)
spinner.text = click.style('Failed to delete config variable: {}'.format(e), fg=Colors.RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1) from e


def _display_config_list(config_variables: typing.List[DeviceConfig], show_header: bool = True) -> None:
Expand Down
36 changes: 24 additions & 12 deletions riocli/device/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,28 +12,40 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
import click_spinner
from click_help_colors import HelpColorsCommand

from riocli.config import new_client
from riocli.constants import Colors, Symbols
from riocli.device.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', 'force', is_flag=True, help='Skip confirmation')
@click.argument('device-name', type=str)
@name_to_guid
def delete_device(device_name: str, device_guid: str, force: bool):
@with_spinner(text='Deleting device...')
def delete_device(device_name: str, device_guid: str, force: bool, spinner=None):
"""
Deletes the device from the Platform
Deletes a device
"""
if not force:
click.confirm('Deleting device {} ({})'.format(device_name, device_guid), abort=True)
with spinner.hidden():
if not force:
click.confirm(
'Deleting device {} ({})'.format(
device_name, device_guid), abort=True)

try:
client = new_client(with_project=True)
with click_spinner.spinner():
client.delete_device(device_id=device_guid)
click.secho('Device deleted successfully!', fg='green')
client.delete_device(device_id=device_guid)
spinner.text = click.style('Device deleted successfully', fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
click.secho(str(e), fg='red')
raise SystemExit(1)
spinner.text = click.style('Failed to delete device: {}'.format(e), fg=Colors.RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1) from e
15 changes: 11 additions & 4 deletions riocli/device/deployment.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,13 +12,20 @@
# 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.deployment.list import display_deployment_list
from riocli.device.util import name_to_guid


@click.command('deployments')
@click.command(
'deployments',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.argument('device-name', type=str)
@name_to_guid
def list_deployments(device_name: str, device_guid: str) -> None:
Expand All @@ -38,5 +45,5 @@ def list_deployments(device_name: str, device_guid: str) -> None:
deployments.append(deployment)
display_deployment_list(deployments, show_header=True)
except Exception as e:
click.secho(str(e), fg='red')
raise SystemExit(1)
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1) from e
32 changes: 26 additions & 6 deletions riocli/device/execute.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,24 +14,44 @@
import typing

import click
from click_help_colors import HelpColorsCommand

from riocli.constants import Colors
from riocli.device.util import name_to_guid
from riocli.utils.execute import run_on_device


@click.command('execute')
@click.command(
'execute',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.option('--user', default='root')
@click.option('--shell', default='/bin/bash')
@click.argument('device-name', type=str)
@click.argument('command', nargs=-1)
@name_to_guid
def execute_command(device_name: str, device_guid: str, user: str, shell: str, command: typing.List[str]):
def execute_command(
device_name: str,
device_guid: str,
user: str,
shell: str,
command: typing.List[str]
) -> None:
"""
Execute commands on the Device
"""
try:
response = run_on_device(device_guid=device_guid, user=user, shell=shell, command=command, background=False)
response = run_on_device(
device_guid=device_guid,
user=user,
shell=shell,
command=command,
background=False,
)

click.secho(response)
except Exception as e:
click.secho(str(e), fg='red')
raise SystemExit(1)
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1) from e
Loading

0 comments on commit c14ebf4

Please sign in to comment.