Skip to content

gitea.cli.config.add

add

Add command for config CLI.

Functions:

gitea.cli.config.add.add_command

add_command(
    ctx: Context,
    name: Annotated[
        str,
        Option(
            "--name",
            help="Name of the account. It does not need to be the same as the Gitea account name.",
        ),
    ],
    token: Annotated[
        str,
        Option("--token", help="Token for authentication."),
    ],
    base_url: Annotated[
        str,
        Option(
            "--base-url", help="Base URL of the platform."
        ),
    ] = "https://gitea.com",
    is_default: Annotated[
        bool,
        Option("--default", help="Set as default account."),
    ] = False,
) -> None

Add a new account to the configuration.

Parameters:

Name Type Description Default
ctx Context

Typer context.

required
name Annotated[str, Option('--name', help='Name of the account. It does not need to be the same as the Gitea account name.')]

Name of the account. It does not need to be the same as the Gitea account name.

required
token Annotated[str, Option('--token', help='Token for authentication.')]

Token for authentication.

required
base_url Annotated[str, Option('--base-url', help='Base URL of the platform.')]

Base URL of the platform.

'https://gitea.com'
is_default Annotated[bool, Option('--default', help='Set as default account.')]

Set as default account.

False
Source code in src/gitea/cli/config/add.py
def add_command(
    ctx: typer.Context,
    name: Annotated[
        str,
        typer.Option("--name", help="Name of the account. It does not need to be the same as the Gitea account name."),
    ],
    token: Annotated[str, typer.Option("--token", help="Token for authentication.")],
    base_url: Annotated[str, typer.Option("--base-url", help="Base URL of the platform.")] = "https://gitea.com",
    is_default: Annotated[bool, typer.Option("--default", help="Set as default account.")] = False,
) -> None:
    """Add a new account to the configuration.

    Args:
        ctx: Typer context.
        name: Name of the account. It does not need to be the same as the Gitea account name.
        token: Token for authentication.
        base_url: Base URL of the platform.
        is_default: Set as default account.

    """
    import logging  # noqa: PLC0415

    from gitea.cli.output import emit  # noqa: PLC0415
    from gitea.config.manager import ConfigManager  # noqa: PLC0415

    logger = logging.getLogger("gitea")

    config_manager = ConfigManager(filename=ctx.obj["config_path"])

    logger.info("Configuration path: %s", config_manager.config_path)

    config_manager.load_config()

    try:
        config_manager.add_account(name=name, token=token, base_url=base_url, is_default=is_default)
        config_manager.save_config()
        logger.info("Account '%s' added successfully.", name)
    except ValueError as e:
        logger.error("Error adding account: %s", e)
        raise typer.Exit(code=1) from e

    emit(
        ctx,
        data={
            "name": name,
            "base_url": config_manager.config.accounts[name].base_url,
            "is_default": config_manager.config.default_account == name,
            "status": "added",
        },
        metadata={"config_path": str(config_manager.config_path)},
    )