From 7b3375b5ce6a0454f82b85593e5449f39c6431ab Mon Sep 17 00:00:00 2001 From: romekodexa Date: Fri, 22 Mar 2024 19:00:10 +0800 Subject: [PATCH 1/3] access-token --- kodexa_cli/cli.py | 64 +++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/kodexa_cli/cli.py b/kodexa_cli/cli.py index 0134633..ebe98cf 100644 --- a/kodexa_cli/cli.py +++ b/kodexa_cli/cli.py @@ -70,6 +70,20 @@ } +def get_current_kodexa_url(): + try: + return KodexaPlatform.get_url() + except: + return "" + + +def get_current_access_token(): + try: + return KodexaPlatform.get_access_token() + except: + return "" + + @contextmanager def set_directory(path: Path): """Sets the cwd within the context @@ -194,7 +208,7 @@ def safe_entry_point(): start_time = datetime.now().replace(microsecond=0) try: - print(f"Using profile {KodexaPlatform.get_current_profile()} @ {KodexaPlatform.get_url()}\n") + print(f"Using profile {KodexaPlatform.get_current_profile()} @ {get_current_kodexa_url()}\n") except: print("Unable to load profile") @@ -220,10 +234,10 @@ def safe_entry_point(): @click.argument("ref", required=True) @click.argument("paths", required=True, nargs=-1) @click.option( - "--url", default=KodexaPlatform.get_url(), help="The URL to the Kodexa server" + "--url", default=get_current_kodexa_url(), help="The URL to the Kodexa server" ) @click.option("--threads", default=5, help="Number of threads to use") -@click.option("--token", default=KodexaPlatform.get_access_token(), help="Access token") +@click.option("--token", default=get_current_access_token(), help="Access token") @pass_info def upload(_: Info, ref: str, paths: list[str], token: str, url: str, threads: int): """ @@ -287,9 +301,9 @@ def upload_file(path): default=False, ) @click.option( - "--url", default=KodexaPlatform.get_url(), help="The URL to the Kodexa server" + "--url", default=get_current_kodexa_url(), help="The URL to the Kodexa server" ) -@click.option("--token", default=KodexaPlatform.get_access_token(), help="Access token") +@click.option("--token", default=get_current_access_token(), help="Access token") @click.option( "--format", default=None, help="The format to input if from stdin (json, yaml)" ) @@ -418,9 +432,9 @@ def deploy_obj(obj): @cli.command() @click.argument("execution_id", required=True) @click.option( - "--url", default=KodexaPlatform.get_url(), help="The URL to the Kodexa server" + "--url", default=get_current_kodexa_url(), help="The URL to the Kodexa server" ) -@click.option("--token", default=KodexaPlatform.get_access_token(), help="Access token") +@click.option("--token", default=get_current_access_token(), help="Access token") @pass_info def logs(_: Info, execution_id: str, url: str, token: str): """ @@ -443,9 +457,9 @@ def logs(_: Info, execution_id: str, url: str, token: str): @click.argument("ref", required=True) @click.argument("output_file", required=False, default="model_implementation") @click.option( - "--url", default=KodexaPlatform.get_url(), help="The URL to the Kodexa server" + "--url", default=get_current_kodexa_url(), help="The URL to the Kodexa server" ) -@click.option("--token", default=KodexaPlatform.get_access_token(), help="Access token") +@click.option("--token", default=get_current_access_token(), help="Access token") @pass_info def download_implementation(_: Info, ref: str, output_file: str, url: str, token: str): """ @@ -461,9 +475,9 @@ def download_implementation(_: Info, ref: str, output_file: str, url: str, token @click.argument("object_type", required=True) @click.argument("ref", required=False) @click.option( - "--url", default=KodexaPlatform.get_url(), help="The URL to the Kodexa server" + "--url", default=get_current_kodexa_url(), help="The URL to the Kodexa server" ) -@click.option("--token", default=KodexaPlatform.get_access_token(), help="Access token") +@click.option("--token", default=get_current_access_token(), help="Access token") @click.option("--query", default="*", help="Limit the results using a query") @click.option("--format", default=None, help="The format to output (json, yaml)") @click.option("--page", default=1, help="Page number") @@ -615,9 +629,9 @@ def print_object_table(object_metadata, objects_endpoint, query, page, pagesize, @click.argument("ref", required=True) @click.argument("query", nargs=-1) @click.option( - "--url", default=KodexaPlatform.get_url(), help="The URL to the Kodexa server" + "--url", default=get_current_kodexa_url(), help="The URL to the Kodexa server" ) -@click.option("--token", default=KodexaPlatform.get_access_token(), help="Access token") +@click.option("--token", default=get_current_access_token(), help="Access token") @click.option( "--download/--no-download", default=False, @@ -823,9 +837,9 @@ def process_family(document_family): @cli.command() @click.argument("project_id", required=True) @click.option( - "--url", default=KodexaPlatform.get_url(), help="The URL to the Kodexa server" + "--url", default=get_current_kodexa_url(), help="The URL to the Kodexa server" ) -@click.option("--token", default=KodexaPlatform.get_access_token(), help="Access token") +@click.option("--token", default=get_current_access_token(), help="Access token") @click.option("--output", help="The path to export to") @pass_info def export_project(_: Info, project_id: str, url: str, token: str, output: str): @@ -843,9 +857,9 @@ def export_project(_: Info, project_id: str, url: str, token: str, output: str): @click.argument("org_slug", required=True) @click.argument("path", required=True) @click.option( - "--url", default=KodexaPlatform.get_url(), help="The URL to the Kodexa server" + "--url", default=get_current_kodexa_url(), help="The URL to the Kodexa server" ) -@click.option("--token", default=KodexaPlatform.get_access_token(), help="Access token") +@click.option("--token", default=get_current_access_token(), help="Access token") @pass_info def import_project(_: Info, org_slug: str, url: str, token: str, path: str): """ @@ -870,9 +884,9 @@ def import_project(_: Info, org_slug: str, url: str, token: str, path: str): @click.argument("project_id", required=True) @click.argument("assistant_id", required=True) @click.option( - "--url", default=KodexaPlatform.get_url(), help="The URL to the Kodexa server" + "--url", default=get_current_kodexa_url(), help="The URL to the Kodexa server" ) -@click.option("--token", default=KodexaPlatform.get_access_token(), help="Access token") +@click.option("--token", default=get_current_access_token(), help="Access token") @click.option("--file", help="The path to the file containing the event to send") @click.option( "--format", default=None, help="The format to use if from stdin (json, yaml)" @@ -940,13 +954,13 @@ def platform(_: Info, python: bool, show_token: bool): """ print(f"Profile: {KodexaPlatform.get_current_profile()}") - platform_url = KodexaPlatform.get_url() + platform_url = get_current_kodexa_url() if platform_url is not None: - print(f"URL: {KodexaPlatform.get_url()}") + print(f"URL: {get_current_kodexa_url()}") if show_token: - print(f"Access Token: {KodexaPlatform.get_access_token()}") + print(f"Access Token: {get_current_access_token()}") kodexa_version = KodexaPlatform.get_server_info() print(f"Environment: {kodexa_version['environment']}") print(f"Version: {kodexa_version['version']}") @@ -955,7 +969,7 @@ def platform(_: Info, python: bool, show_token: bool): print("\nPython example:\n\n") print(f"from kodexa import KodexaClient") print( - f"client = KodexaClient('{KodexaPlatform.get_url()}', '{KodexaPlatform.get_access_token()}')" + f"client = KodexaClient('{get_current_kodexa_url()}', '{get_current_access_token()}')" ) else: print("Kodexa is not logged in") @@ -965,9 +979,9 @@ def platform(_: Info, python: bool, show_token: bool): @click.argument("object_type") @click.argument("ref") @click.option( - "--url", default=KodexaPlatform.get_url(), help="The URL to the Kodexa server" + "--url", default=get_current_kodexa_url(), help="The URL to the Kodexa server" ) -@click.option("--token", default=KodexaPlatform.get_access_token(), help="Access token") +@click.option("--token", default=get_current_access_token(), help="Access token") @pass_info def delete(_: Info, object_type: str, ref: str, url: str, token: str): """ From 44563fbcd572234d9991e38e9c6c6e173f2223a0 Mon Sep 17 00:00:00 2001 From: romekodexa Date: Wed, 3 Apr 2024 15:48:05 +0800 Subject: [PATCH 2/3] Replicated try-except for kodexa-profile since it tends to fail at the safe entry point if profile does not exist --- kodexa_cli/cli.py | 13 ++++++++++--- tests/cli_runner_test.py | 8 ++++++++ tests/config_test.py | 11 +++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 tests/cli_runner_test.py create mode 100644 tests/config_test.py diff --git a/kodexa_cli/cli.py b/kodexa_cli/cli.py index ebe98cf..0d4a1b7 100644 --- a/kodexa_cli/cli.py +++ b/kodexa_cli/cli.py @@ -70,6 +70,13 @@ } +def get_current_kodexa_profile(): + try: + return KodexaPlatform.get_current_profile() + except: + return "" + + def get_current_kodexa_url(): try: return KodexaPlatform.get_url() @@ -208,7 +215,7 @@ def safe_entry_point(): start_time = datetime.now().replace(microsecond=0) try: - print(f"Using profile {KodexaPlatform.get_current_profile()} @ {get_current_kodexa_url()}\n") + print(f"Using profile {get_current_kodexa_profile()()} @ {get_current_kodexa_url()}\n") except: print("Unable to load profile") @@ -953,7 +960,7 @@ def platform(_: Info, python: bool, show_token: bool): Get the details for the Kodexa instance we are logged into """ - print(f"Profile: {KodexaPlatform.get_current_profile()}") + print(f"Profile: {get_current_kodexa_profile()}") platform_url = get_current_kodexa_url() if platform_url is not None: @@ -1049,7 +1056,7 @@ def profile(_: Info, profile: str, delete: bool, list: bool): print(f"Profiles: {','.join(KodexaPlatform.list_profiles())}") else: print( - f"Current profile: {KodexaPlatform.get_current_profile()} [{KodexaPlatform.get_url(KodexaPlatform.get_current_profile())}]") + f"Current profile: {get_current_kodexa_profile()} [{KodexaPlatform.get_url(get_current_kodexa_profile())}]") @cli.command() diff --git a/tests/cli_runner_test.py b/tests/cli_runner_test.py new file mode 100644 index 0000000..756fb8a --- /dev/null +++ b/tests/cli_runner_test.py @@ -0,0 +1,8 @@ +from click.testing import CliRunner +from kodexa_cli.cli import get # replace this with the name of your module where configure command is defined +import os + + +def test_configure_command(): + runner = CliRunner() + result = runner.invoke(get, ["org"]) \ No newline at end of file diff --git a/tests/config_test.py b/tests/config_test.py new file mode 100644 index 0000000..1cc0811 --- /dev/null +++ b/tests/config_test.py @@ -0,0 +1,11 @@ +import os, json + +from appdirs import AppDirs + +dirs = AppDirs("Kodexa", "Kodexa") + +path = os.path.join(dirs.user_config_dir, ".kodexa.json") +if os.path.exists(path): + with open(path, "r") as outfile: + kodexa_config = json.load(outfile) + print(kodexa_config) From 1179594d85a177ec6bc1df74df2e396c91f18154 Mon Sep 17 00:00:00 2001 From: romekodexa Date: Wed, 3 Apr 2024 15:49:58 +0800 Subject: [PATCH 3/3] minor changes --- kodexa_cli/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kodexa_cli/cli.py b/kodexa_cli/cli.py index 0d4a1b7..a08bbf7 100644 --- a/kodexa_cli/cli.py +++ b/kodexa_cli/cli.py @@ -215,7 +215,7 @@ def safe_entry_point(): start_time = datetime.now().replace(microsecond=0) try: - print(f"Using profile {get_current_kodexa_profile()()} @ {get_current_kodexa_url()}\n") + print(f"Using profile {get_current_kodexa_profile()} @ {get_current_kodexa_url()}\n") except: print("Unable to load profile")