def _implement_config(obj: ContextInfo, key: str, value: str,
                      unset: bool) -> None:
    _check_args_and_options(key, value, unset)

    config_parser = obj.config_parser

    if not config_parser.has_section("config"):
        config_parser.add_section("config")
    config_section = config_parser["config"]

    if not key:
        for config_key, config_value in config_section.items():
            click.echo(f"{config_key} = {config_value}\n")
        return

    if not value:
        if key not in config_section:
            error(f"{key} has not been configured yet")
        if unset:
            del config_section[key]
            obj.write_config(show_message=False)
            click.echo(f'Successfully unset "{key}"')
            return

        click.echo(f"{key} = {config_section[key]}\n")
    else:
        _check_key_and_value(key, value)
        config_section[key] = value
        obj.write_config()
def context_object(mocker: MockerFixture, tmp_path: Path) -> ContextInfo:
    """Get a ContextInfo instance containing the command context.

    Arguments:
        mocker: The mocker fixture.
        tmp_path: An instance of TempPathFactory.

    Returns:
        The ContextInfo instance.

    """
    path = tmp_path / "test_home"
    path.mkdir()
    mocker.patch(f"{os.__name__}.path.expanduser", return_value=path)

    access_key = "Accesskey-********************************"
    url = "https://gas.graviti.cn/"
    context = ContextInfo(access_key, url, "test")

    config_parser = context.config_parser
    config_parser.add_section("profiles")
    profiles = config_parser["profiles"]
    profiles["test"] = form_profile_value(access_key, "")
    profiles["test_01"] = form_profile_value(access_key, "")
    profiles["test_02"] = form_profile_value(access_key, url)
    context.write_config(False)

    return context
Exemple #3
0
def _unset_auth(obj: ContextInfo, is_all: bool) -> None:
    config_parser = obj.config_parser
    if is_all:
        config_parser.remove_section("profiles")
    else:
        try:
            removed = config_parser.remove_option("profiles", obj.profile_name)
        except NoSectionError:
            removed = False
        if not removed:
            error(f'Profile "{obj.profile_name}" does not exist.')
    obj.write_config(show_message=False)
    hint = "all" if is_all else f'"{obj.profile_name}"'
    click.echo(f"Successfully unset {hint} auth info")
Exemple #4
0
def _update_profile(obj: ContextInfo, arg1: str, arg2: str) -> None:
    access_key, url = (arg2, arg1) if arg2 else (arg1, arg2)
    profile_name, config_parser = obj.profile_name, obj.config_parser
    gas_client = obj.get_gas(access_key, url)
    try:
        user_info = gas_client.get_user()
    except UnauthorizedError:
        error(f"{access_key} is not a valid AccessKey")

    if not config_parser.has_section("profiles"):
        config_parser.add_section("profiles")
    config_parser["profiles"][profile_name] = form_profile_value(access_key, url)
    obj.write_config(show_message=False)

    messages = [
        f'Successfully set authentication info of "{click.style(user_info.name, bold=True)}"'
    ]
    if user_info.team:
        messages.append(f' in "{click.style(user_info.team.name, bold=True)}" team')
    if profile_name != "default":
        messages.append(f' into profile "{click.style(profile_name, bold=True)}"')
    click.echo("".join(messages))