diff --git a/riocli/secret/__init__.py b/riocli/secret/__init__.py index 5bde0c8c..94623ed9 100644 --- a/riocli/secret/__init__.py +++ b/riocli/secret/__init__.py @@ -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. @@ -14,6 +14,7 @@ import click from click_help_colors import HelpColorsGroup +from riocli.constants import Colors from riocli.secret.create import create_secret from riocli.secret.delete import delete_secret from riocli.secret.import_secret import import_secret @@ -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 secret() -> None: """ diff --git a/riocli/secret/delete.py b/riocli/secret/delete.py index 06222c88..d9d74e5b 100644 --- a/riocli/secret/delete.py +++ b/riocli/secret/delete.py @@ -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. @@ -12,29 +12,40 @@ # See the License for the specific language governing permissions and # limitations under the License. import click -from click_spinner import spinner from riocli.config import new_client +from riocli.constants import Colors, Symbols from riocli.secret.util import name_to_guid +from riocli.utils.spinner import with_spinner +from click_help_colors import HelpColorsCommand - -@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, default=False, help='Skip confirmation') @click.argument('secret-name', type=str) @name_to_guid -def delete_secret(force: str, secret_name: str, secret_guid: str) -> None: +@with_spinner(text='Deleting secret...') +def delete_secret(force: str, secret_name: str, secret_guid: str, spinner=None) -> None: """ - Deletes the secret resource from the Platform + Deletes a secret """ - if not force: - click.confirm('Deleting secret {} ({})'.format(secret_name, secret_guid), abort=True) + with spinner.hidden(): + if not force: + click.confirm( + 'Deleting secret {} ({})'.format(secret_name, secret_guid), + abort=True) try: client = new_client() - with spinner(): - client.delete_secret(secret_guid) - click.secho('Secret deleted successfully!', fg='green') + client.delete_secret(secret_guid) + spinner.text = click.style('Secret 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 secret: {}'.format(e), fg=Colors.RED) + spinner.red.fail(Symbols.ERROR) + raise SystemExit(1) from e diff --git a/riocli/secret/inspect.py b/riocli/secret/inspect.py index ee02ac4b..daf4ff0a 100644 --- a/riocli/secret/inspect.py +++ b/riocli/secret/inspect.py @@ -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. @@ -12,21 +12,28 @@ # 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 import Secret from riocli.config import new_client +from riocli.constants import Colors from riocli.secret.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('secret-name', type=str) @name_to_guid def inspect_secret(format_type: str, secret_name: str, secret_guid: str) -> None: """ - Inspect the secret resource + Inspect a secret """ try: client = new_client() @@ -34,7 +41,7 @@ def inspect_secret(format_type: str, secret_name: str, secret_guid: str) -> None data = make_secret_inspectable(secret) 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) diff --git a/riocli/secret/list.py b/riocli/secret/list.py index abddd557..c2820fdf 100644 --- a/riocli/secret/list.py +++ b/riocli/secret/list.py @@ -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. @@ -18,9 +18,15 @@ from riocli.config import new_client from riocli.utils import tabulate_data - - -@click.command('list') +from riocli.constants import Colors +from click_help_colors import HelpColorsCommand + +@click.command( + 'list', + cls=HelpColorsCommand, + help_headers_color=Colors.YELLOW, + help_options_color=Colors.GREEN, +) @click.option('--secret-type', '-t', default=['docker', 'source'], multiple=True, help='Types to filter the list of Secret [default: docker,source]') def list_secrets(secret_type: typing.Union[str, typing.Tuple[str]]) -> None: @@ -33,7 +39,7 @@ def list_secrets(secret_type: typing.Union[str, typing.Tuple[str]]) -> None: secrets = sorted(secrets, key=lambda s: s.name.lower()) _display_secret_list(secrets, secret_type, show_header=True) except Exception as e: - click.secho(str(e), fg='red') + click.secho(str(e), fg=Colors.RED) raise SystemExit(1) diff --git a/riocli/secret/model.py b/riocli/secret/model.py index 917f40f4..59ad3ef2 100644 --- a/riocli/secret/model.py +++ b/riocli/secret/model.py @@ -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. @@ -13,22 +13,28 @@ # limitations under the License. import typing -from rapyuta_io import Secret as v1Secret, SecretConfigDocker, \ - SecretConfigSourceBasicAuth, \ - SecretConfigSourceSSHAuth, Client +from rapyuta_io import ( + Client, + Secret as v1Secret, + SecretConfigDocker, + SecretConfigSourceBasicAuth, + SecretConfigSourceSSHAuth, +) from riocli.jsonschema.validate import load_schema from riocli.model import Model class Secret(Model): - def __init__(self, *args, **kwargs): self.update(*args, **kwargs) def find_object(self, client: Client) -> bool: - _, secret = self.rc.find_depends( - {'kind': 'secret', 'nameOrGUID': self.metadata.name}) + _, secret = self.rc.find_depends({ + 'kind': 'secret', + 'nameOrGUID': self.metadata.name + }) + if not secret: return False @@ -51,8 +57,12 @@ def to_v1(self) -> v1Secret: return self._git_secret_to_v1() def _docker_secret_to_v1(self) -> v1Secret: - config = SecretConfigDocker(self.spec.docker.username, self.spec.docker.password, self.spec.docker.email, - self.spec.docker.registry) + config = SecretConfigDocker( + self.spec.docker.username, + self.spec.docker.password, + self.spec.docker.email, + self.spec.docker.registry, + ) return v1Secret(self.metadata.name, config) def _git_secret_to_v1(self) -> v1Secret: @@ -60,7 +70,11 @@ def _git_secret_to_v1(self) -> v1Secret: config = SecretConfigSourceSSHAuth(self.spec.git.privateKey) elif self.spec.git.authMethod == 'HTTP/S Basic Auth': ca_cert = self.spec.git.get('ca_cert', None) - config = SecretConfigSourceBasicAuth(self.spec.git.username, self.spec.git.password, ca_cert=ca_cert) + config = SecretConfigSourceBasicAuth( + self.spec.git.username, + self.spec.git.password, + ca_cert=ca_cert + ) elif self.spec.git.authMethod == 'HTTP/S Token Auth': # TODO(ankit): Implement it once SDK has support for it. raise Exception('token-based secret is not supported yet!')