|
| 1 | +import json |
| 2 | +import os |
| 3 | +import time |
| 4 | +from logging import getLogger |
| 5 | +from typing import List |
| 6 | +from uuid import UUID |
| 7 | + |
| 8 | +import rich |
| 9 | +import typer |
| 10 | +from click import Context |
| 11 | +from pydantic import BaseModel |
| 12 | +from rich.console import Console |
| 13 | +from rich.progress import Progress |
| 14 | +from rich.table import Table |
| 15 | +from typer.core import TyperGroup |
| 16 | +from typing_extensions import Annotated |
| 17 | + |
| 18 | +from naas_python.domains.secret.adaptors.primary.utils import PydanticTableModel |
| 19 | +from naas_python.domains.secret.SecretSchema import ( |
| 20 | + ISecretDomain, |
| 21 | + ISecretInvoker, |
| 22 | + SecretConflictError, |
| 23 | +) |
| 24 | +# from naas_python.domains.secret.SecretSchema import SecrettryConflictError |
| 25 | +from naas_python.utils.cicd import Pipeline |
| 26 | + |
| 27 | +logger = getLogger(__name__) |
| 28 | + |
| 29 | + |
| 30 | +class OrderCommands(TyperGroup): |
| 31 | + def list_commands(self, ctx: Context): |
| 32 | + """Return list of commands in the order appear.""" |
| 33 | + return list(self.commands) |
| 34 | + |
| 35 | + |
| 36 | +class TyperSecretAdaptor(ISecretInvoker): |
| 37 | + def __init__(self, domain: ISecretDomain): |
| 38 | + super().__init__() |
| 39 | + |
| 40 | + self.domain = domain |
| 41 | + self.console = Console() |
| 42 | + |
| 43 | + self.app = typer.Typer( |
| 44 | + cls=OrderCommands, |
| 45 | + help="Naas Secret CLI", |
| 46 | + add_completion=False, |
| 47 | + no_args_is_help=True, |
| 48 | + pretty_exceptions_enable=False, |
| 49 | + rich_markup_mode="rich", |
| 50 | + context_settings={"help_option_names": ["-h", "--help"]}, |
| 51 | + ) |
| 52 | + |
| 53 | + # Include all commands |
| 54 | + self.app.command()(self.list) |
| 55 | + self.app.command()(self.create) |
| 56 | + self.app.command()(self.get) |
| 57 | + self.app.command()(self.delete) |
| 58 | + |
| 59 | + def _list_preview(self, data: List[dict], headers: list): |
| 60 | + if not isinstance(data, list): |
| 61 | + raise TypeError("Data must be a list of dicts, not {}".format(type(data))) |
| 62 | + |
| 63 | + # Determine column widths based on the longest values |
| 64 | + column_widths = [max(len(str(item)) for item in col) for col in zip(*data)] |
| 65 | + |
| 66 | + # Print the headers |
| 67 | + header_format = " ".join( |
| 68 | + f"{header:<{width}}" for header, width in zip(headers, column_widths) |
| 69 | + ) |
| 70 | + print(header_format) |
| 71 | + |
| 72 | + # Print the data |
| 73 | + for row in data: |
| 74 | + row_format = " ".join( |
| 75 | + f"{str(item):<{width}}" for item, width in zip(row, column_widths) |
| 76 | + ) |
| 77 | + print(row_format) |
| 78 | + |
| 79 | + def create( |
| 80 | + self, |
| 81 | + name: str = typer.Option(..., "--name", "-n", help="Name of the secret"), |
| 82 | + value: str = typer.Option(..., "--value", "-v", help="Value of the secret"), |
| 83 | + |
| 84 | + rich_preview: bool = typer.Option( |
| 85 | + False, |
| 86 | + "--rich-preview", |
| 87 | + "-rp", |
| 88 | + help="Rich preview of the Secret information as a table", |
| 89 | + ), |
| 90 | + ): |
| 91 | + """Create a Secret with the given specifications""" |
| 92 | + secret = self.domain.create( |
| 93 | + name=name, |
| 94 | + value=value |
| 95 | + ) |
| 96 | + |
| 97 | + if secret is None: |
| 98 | + print('Secret Successfully created') |
| 99 | + |
| 100 | + def get( |
| 101 | + self, |
| 102 | + name: str = typer.Option(..., "--name", "-n", help="Name of the secret"), |
| 103 | + rich_preview: bool = typer.Option( |
| 104 | + os.environ.get("NAAS_CLI_RICH_PREVIEW", False), |
| 105 | + "--rich-preview", |
| 106 | + "-rp", |
| 107 | + help="Rich preview of the secret information as a table", |
| 108 | + ), |
| 109 | + ): |
| 110 | + """Get a secret with the given name""" |
| 111 | + secret = self.domain.get(name=name) |
| 112 | + |
| 113 | + if rich_preview: |
| 114 | + self.console.print(PydanticTableModel([secret]).table) |
| 115 | + |
| 116 | + else: |
| 117 | + print(secret.value) |
| 118 | + |
| 119 | + def delete( |
| 120 | + self, |
| 121 | + name: str = typer.Option(..., "--name", "-n", help="Name of the secret"), |
| 122 | + ): |
| 123 | + self.domain.delete(name=name) |
| 124 | + |
| 125 | + print(f"Secret '{name}' deleted successfully") |
| 126 | + |
| 127 | + def list( |
| 128 | + self, |
| 129 | + page_size: int = typer.Option(0, help="Size of each page of results"), |
| 130 | + page_number: int = typer.Option(0, help="Target page number of results"), |
| 131 | + rich_preview: bool = typer.Option( |
| 132 | + False, |
| 133 | + "--rich-preview", |
| 134 | + "-rp", |
| 135 | + help="Rich preview of the secret information as a table", |
| 136 | + ), |
| 137 | + ): |
| 138 | + """List all secrets for the current user""" |
| 139 | + secret_list = self.domain.list(page_size=page_size, page_number=page_number) |
| 140 | + |
| 141 | + data = [] |
| 142 | + headers = [] |
| 143 | + |
| 144 | + # Extract the data and headers |
| 145 | + for secret in secret_list: |
| 146 | + _secret_dict = secret.dict() |
| 147 | + |
| 148 | + |
| 149 | + data.append(list(_secret_dict.values())) # Append a list of values to data |
| 150 | + |
| 151 | + headers = [key.upper() for key in _secret_dict.keys()] |
| 152 | + |
| 153 | + if len(data) == 0: |
| 154 | + print("No matching results found.") |
| 155 | + return |
| 156 | + |
| 157 | + headers = [key.upper() for key in _secret_dict.keys()] |
| 158 | + |
| 159 | + if rich_preview: |
| 160 | + # Create a Rich Table |
| 161 | + table = Table(show_header=True, header_style="bold") |
| 162 | + # Add columns to the table |
| 163 | + for header in headers: |
| 164 | + table.add_column(header, justify="center") |
| 165 | + |
| 166 | + # Add data rows to the table |
| 167 | + for row in data: |
| 168 | + table.add_row(*row) |
| 169 | + |
| 170 | + # Print the table |
| 171 | + rich.print(table) |
| 172 | + |
| 173 | + else: |
| 174 | + self._list_preview(data, headers) |
0 commit comments