From 1ef0cf1dadade001622b080af154e9126f4b31c3 Mon Sep 17 00:00:00 2001 From: Manuel Holtgrewe Date: Thu, 2 Nov 2023 10:51:41 +0100 Subject: [PATCH] wip --- tests/cli/test_projects.py | 2 +- varfish_cli/api/project.py | 5 ++--- varfish_cli/cli/projects.py | 26 ++++++++++++++++++++++++++ varfish_cli/config.py | 2 +- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/tests/cli/test_projects.py b/tests/cli/test_projects.py index f63f27b..4616d63 100644 --- a/tests/cli/test_projects.py +++ b/tests/cli/test_projects.py @@ -140,7 +140,7 @@ def test_project_load_config( req_mocks.append( requests_mock.get( ( - f"{host}/project/api/retrieve/{project_uuid}?app_name=cases_import" + f"{host}/project/api/settings/retrieve/{project_uuid}?app_name=cases_import" f"&setting_name={setting_name}" ), request_headers={"Authorization": f"Token {token}"}, diff --git a/varfish_cli/api/project.py b/varfish_cli/api/project.py index e38a425..1c73b3f 100644 --- a/varfish_cli/api/project.py +++ b/varfish_cli/api/project.py @@ -18,7 +18,7 @@ #: End point for retrieving projects. ENDPOINT_PROJECT_RETRIEVE = "/project/api/retrieve/{project_uuid}" #: End point for retrieving projects settings. -ENDPOINT_PROJECT_RETRIEVE = "/project/api/settings/retrieve/{project_uuid}" +ENDPOINT_PROJECT_SETTING_RETRIEVE = "/project/api/settings/retrieve/{project_uuid}" def project_list( @@ -69,10 +69,9 @@ def project_settings_retrieve( query = "&".join(queries) if query: query = f"?{query}" - endpoint = f"{server_url}{ENDPOINT_PROJECT_RETRIEVE}{query}".format(project_uuid=project_uuid) + endpoint = f"{server_url}{ENDPOINT_PROJECT_SETTING_RETRIEVE}{query}".format(project_uuid=project_uuid) logger.debug("Sending GET request to end point %s", endpoint) headers = {"Authorization": "Token %s" % api_token} result = requests.get(endpoint, headers=headers, verify=verify_ssl) raise_for_status(result) - print(result.json()) return pydantic.TypeAdapter(SettingsEntry).validate_python(result.json()) diff --git a/varfish_cli/cli/projects.py b/varfish_cli/cli/projects.py index 5951756..58d4723 100644 --- a/varfish_cli/cli/projects.py +++ b/varfish_cli/cli/projects.py @@ -1,5 +1,14 @@ """Implementation of varfish-cli subcommand "projects *".""" +import os + +try: + import tomllib + from tomllib import TOMLDecodeError +except ImportError: + import toml as tomllib + from toml import TomlDecodeError as TOMLDecodeError + import typing import uuid @@ -7,6 +16,7 @@ import typer from varfish_cli import api, common +from varfish_cli.cli import DEFAULT_PATH_VARFISHRC from varfish_cli.cli.common import ListObjects, RetrieveObject from varfish_cli.common import OutputFormat from varfish_cli.config import CommonOptions @@ -86,6 +96,10 @@ def cli_project_load_config( project_uuid: typing.Annotated[ uuid.UUID, typer.Argument(..., help="UUID of the object to retrieve") ], + config_path: typing.Annotated[ + str, + typer.Option("--config-path", help="Path to configuration file", envvar="VARFISH_RC_PATH"), + ] = DEFAULT_PATH_VARFISHRC, output_file: typing.Annotated[ str, typer.Option("--output-file", help="Path to file to write to") ] = "-", @@ -121,4 +135,16 @@ def cli_project_load_config( logger.info("... all data retrieved, updating config...") + if not os.path.exists(config_path): + with open(config_path, "rt") as tomlf: + try: + config_toml = tomllib.loads(tomlf.read()) + except TOMLDecodeError as e: + logger.error("could not parse configuration file %s: %s", config_path, e) + raise typer.Exit(1) + else: + config_toml = {} + + config_toml.setdefault("paths", []) + print(kwargs) diff --git a/varfish_cli/config.py b/varfish_cli/config.py index 5b60a6b..53e5c09 100644 --- a/varfish_cli/config.py +++ b/varfish_cli/config.py @@ -60,7 +60,7 @@ def load_config(config_path: str) -> typing.Tuple[typing.Optional[str], typing.O logger.debug("global/varfish_server_url not set in %s", config_path) toml_varfish_api_token = config_toml.get("global", {}).get("varfish_api_token") if toml_varfish_api_token: - logger.debug("using global/varfish_server_url from %s", config_path) + logger.debug("using global/varfish_api_token from %s", config_path) else: logger.debug("global/varfish_api_token not set in %s", config_path)