Coverage for harbor_cli/harbor/client.py: 22%
24 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-09 12:09 +0100
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-09 12:09 +0100
1from __future__ import annotations
3from typing import TYPE_CHECKING
5from harborapi import HarborAsyncClient
7from ..logs import logger
8from ..output.prompts import str_prompt
10if TYPE_CHECKING:
11 from ..config import HarborCLIConfig
13# It's unlikely that we will authenticate with multiple
14# Harbor instances, so keeping it in a dict is probably overkill
16_CLIENTS = {} # type: dict[str, HarborAsyncClient]
19def get_client(config: HarborCLIConfig) -> HarborAsyncClient:
20 if _CLIENTS.get(config.harbor.url) is None:
21 # Instantiate harbor client with credentials dict from config
22 _CLIENTS[config.harbor.url] = HarborAsyncClient(**(config.harbor.credentials))
23 return _CLIENTS[config.harbor.url]
26def setup_client(config: HarborCLIConfig) -> HarborAsyncClient:
27 """Setup the HarborAsyncClient for the application."""
28 # We want to log all problems before prompting for input,
29 # so we do these checks pre-emptively
30 has_url = True if config.harbor.url else False
31 has_auth = True if config.harbor.has_auth_method else False
32 if not has_url:
33 logger.warning("Harbor API URL missing from configuration file.")
34 if not has_auth:
35 logger.warning("Harbor authentication method missing from configuration file.")
37 if not has_url:
38 config.harbor.url = str_prompt("Harbor API URL")
40 # We need one of the available auth methods to be specified
41 # If not, prompt for username and password
42 if not has_auth:
43 # TODO: refactor this so we can re-use username and password
44 # prompts from commands.cli.init!
45 if not config.harbor.username:
46 config.harbor.username = str_prompt("Username")
47 if not config.harbor.secret:
48 config.harbor.secret = str_prompt("Password", password=True) # type: ignore # pydantic.SecretStr
50 return get_client(config)