Skip to content

Commit

Permalink
feat(device): updates spinner in device commands
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabpain committed Oct 3, 2023
1 parent bdbb17e commit b02e0ad
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
25 changes: 17 additions & 8 deletions riocli/device/tools/scp.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
from riocli.config import new_client
from riocli.device.tools.util import copy_from_device, copy_to_device
from riocli.device.util import is_remote_path
from riocli.utils.spinner import with_spinner
from yaspin.api import Yaspin
from riocli.constants import Symbols, Colors


@click.command()
@click.argument('source', nargs=1)
@click.argument('destination', nargs=1)
def scp(source, destination) -> None:
@with_spinner(text='Copying files...')
def scp(source: str, destination: str, spinner: Yaspin = None) -> None:
"""
scp like interface to copy files to and from the device
An scp like interface to copy files to and from the device
"""
try:
client = new_client()
Expand All @@ -32,15 +36,20 @@ def scp(source, destination) -> None:
dest_device_guid, dest = is_remote_path(destination, devices=devices)

if src_device_guid is None and dest_device_guid is None:
click.secho('One of source or destination paths should be a remote path of format '
'<device-id|device-name>:path', fg='red')
raise SystemExit(1)
raise Exception('One of source or destination paths should be a remote '
'path of the format <device-id|device-name>:path')

if src_device_guid is not None:
copy_from_device(src_device_guid, src, dest)
with spinner.hidden():
copy_from_device(src_device_guid, src, dest)

if dest_device_guid is not None:
copy_to_device(dest_device_guid, src, dest)
with spinner.hidden():
copy_to_device(dest_device_guid, src, dest)

spinner.text = click.style('Files copied 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 copy files: {}'.format(e), fg=Colors.RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1)
14 changes: 5 additions & 9 deletions riocli/device/tools/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,19 @@ def ssh_authorize_key(device_name: str, device_guid: str, user: str,
temp_path = "/tmp/{}".format(random_string(8, 5))

spinner.write("> Uploading public SSH key to device")
copy_to_device(device_guid, str(public_key_file), temp_path,
spinner=spinner)
with spinner.hidden():
copy_to_device(device_guid, str(public_key_file), temp_path)

if user != 'root':
command = ['cat', temp_path, '>>',
'/home/' + user + '/.ssh/authorized_keys']
command = ['cat', temp_path, '>>', '/home/' + user + '/.ssh/authorized_keys']
else:
command = ['cat', temp_path, '>>', '/root/.ssh/authorized_keys']

run_on_device(device_guid=device_guid, command=command, user=user)

spinner.text = click.style(
'Public key {} added successfully'.format(public_key_file),
fg=Colors.GREEN)
spinner.text = click.style('Public key {} added successfully'.format(public_key_file), fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
spinner.text = click.style("Failed to add keys".format(e),
fg=Colors.RED)
spinner.text = click.style("Failed to add keys".format(e), fg=Colors.RED)
spinner.red.ok(Symbols.ERROR)
raise SystemExit(1)
9 changes: 4 additions & 5 deletions riocli/device/tools/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ def copy_from_device(device_guid: str, src: str, dest: str) -> None:
run_bash('curl -o "{}" "{}"'.format(dest, url))


def copy_to_device(device_guid: str, src: str, dest: str, spinner=None) -> None:
def copy_to_device(device_guid: str, src: str, dest: str) -> None:
config = Configuration()
path = random_string(8, 5)
run_bash('curl -sT {} {}/{}'.format(src, config.piping_server, path), bg=True)
with spinner.hidden():
run_on_device(
device_guid=device_guid,
command=['curl', '-s', '-o', dest, '{}/{}'.format(config.piping_server, path)])
run_on_device(
device_guid=device_guid,
command=['curl', '-s', '-o', dest, '{}/{}'.format(config.piping_server, path)])

0 comments on commit b02e0ad

Please sign in to comment.