Skip to content

gitea.cli.utils

utils

Utility functions for Gitea CLI.

Functions:

gitea.cli.utils.execute_api_command

execute_api_command(
    api_call: Callable[
        [],
        tuple[
            dict[str, Any] | list[dict[str, Any]],
            dict[str, Any],
        ],
    ],
    command_name: str = "Command",
    base_url: str | None = None,
) -> None

Execute an API command and print the result as the JSON envelope.

The result is always written as the {"data": ..., "metadata": ...} JSON envelope, so these commands already satisfy --output json and are unaffected by --output text. A command with a human-readable rendering of its own calls execute_api_call instead, and reports the result itself.

Parameters:

Name Type Description Default
api_call Callable[[], tuple[dict[str, Any] | list[dict[str, Any]], dict[str, Any]]]

Callable that executes the API call and returns the result.

required
command_name str

Name of the command for error messages.

'Command'
base_url str | None

The base URL the call is made against, so an unreachable instance is reported by the host the command tried to reach. The callable holds the client, so the host is not recoverable from here and every command is expected to pass it.

None
Source code in src/gitea/cli/utils/api.py
def execute_api_command(
    api_call: Callable[[], tuple[dict[str, Any] | list[dict[str, Any]], dict[str, Any]]],
    command_name: str = "Command",
    base_url: str | None = None,
) -> None:
    """Execute an API command and print the result as the JSON envelope.

    The result is always written as the `{"data": ..., "metadata": ...}` JSON
    envelope, so these commands already satisfy `--output json` and are
    unaffected by `--output text`. A command with a human-readable rendering of
    its own calls `execute_api_call` instead, and reports the result itself.

    Args:
        api_call: Callable that executes the API call and returns the result.
        command_name: Name of the command for error messages.
        base_url: The base URL the call is made against, so an unreachable
            instance is reported by the host the command tried to reach. The
            callable holds the client, so the host is not recoverable from here
            and every command is expected to pass it.

    """
    execute_api_call(
        api_call=api_call,
        report=lambda data, metadata: print_envelope(data=data, metadata=metadata),
        command_name=command_name,
        base_url=base_url,
    )

gitea.cli.utils.get_auth_params

get_auth_params(
    config_path: Path | str,
    account_name: str | None,
    token: str | None,
    base_url: str | None,
) -> tuple[str, str]

Get authentication parameters from CLI context.

Parameters:

Name Type Description Default
config_path Path | str

Path to the configuration file.

required
account_name str | None

Name of the account to use for authentication.

required
token str | None

Token for authentication.

required
base_url str | None

Base URL of the Gitea platform.

required

Returns:

Type Description
tuple[str, str]

A tuple containing the token and base URL for authentication.

Source code in src/gitea/cli/utils/auth.py
def get_auth_params(
    config_path: Path | str,
    account_name: str | None,
    token: str | None,
    base_url: str | None,
) -> tuple[str, str]:
    """Get authentication parameters from CLI context.

    Args:
        config_path: Path to the configuration file.
        account_name: Name of the account to use for authentication.
        token: Token for authentication.
        base_url: Base URL of the Gitea platform.

    Returns:
        A tuple containing the token and base URL for authentication.

    """
    if account_name is not None:
        if token is not None or base_url is not None:
            logger.warning(
                "Both account name and token/base_url provided. The token and base_url from the account '%s' will be used.",
                account_name,
            )

        config_manager = ConfigManager(filename=config_path)
        config_manager.load_config()
        account_config = config_manager.get_config(name=account_name)
        token = account_config.token
        base_url = account_config.base_url
        return token, base_url
    if token is None and base_url is None:
        config_manager = ConfigManager(filename=config_path)
        config_manager.load_config()

        if config_manager.has_default_account():
            account_config = config_manager.get_config(name=None)
            token = account_config.token
            base_url = account_config.base_url
            return token, base_url
        else:
            raise ValueError(
                "No default account available for authentication. Please provide an account name or token/base_url."
            )
    if token is None or base_url is None:
        missing_params = []
        if token is None:
            missing_params.append("token")
        if base_url is None:
            missing_params.append("base_url")
        raise ValueError(
            f"Insufficient authentication parameters. Missing: {', '.join(missing_params)}. "
            f"Please provide both token and base_url, or use an account name."
        )
    return token, base_url

Modules