~gpanders/gandi-cli

e2e7b1a4b769cf6cc254924c09b315c3c66ffce6 — Greg Anders 2 months ago 125ccad
Fix output from alias command
1 files changed, 46 insertions(+), 22 deletions(-)

M src/gandi/commands/alias.py
M src/gandi/commands/alias.py => src/gandi/commands/alias.py +46 -22
@@ 1,6 1,6 @@
import argparse
import json
from typing import Dict, List, Optional
from typing import Dict, Optional, Set

import requests



@@ 25,41 25,65 @@ def alias(config: Dict[str, str], auth: GandiAuth, args: argparse.Namespace) -> 
    domain = args.domain if args.domain is not None else config["domain"]
    mailbox_id = args.id if args.id is not None else config["mailbox_id"]
    url = f"https://api.gandi.net/v5/email/mailboxes/{domain}/{mailbox_id}"

    aliases = get_aliases(url, auth)
    if aliases is None:
        return False

    if args.list:
        print("\n".join(aliases))
    else:
        if args.add:
            if args.add in aliases:
                print(f"Alias '{args.add}' already exists")
                return False

            aliases.append(args.add)
        elif args.remove:
            if args.remove not in aliases:
                print(f"Alias '{args.remove}' does not exist")
                return False
        print("\n".join(sorted(aliases)))
        return True

            aliases.remove(args.remove)
    if args.add:
        return add_alias(url, auth, args.add, aliases)

        if not update_aliases(url, auth, aliases):
            return False
    if args.remove:
        return remove_alias(url, auth, args.remove, aliases)

    return True
    # Unreachable
    raise AssertionError


def get_aliases(url: str, auth: GandiAuth) -> Optional[List[str]]:
def get_aliases(url: str, auth: GandiAuth) -> Optional[Set[str]]:
    res = requests.get(url, auth=auth)
    if res.status_code != 200:
        return None

    data = json.loads(res.text)
    return data["aliases"]
    return set(data["aliases"])


def add_alias(url: str, auth: GandiAuth, alias: str, aliases: Set[str]) -> bool:
    if alias in aliases:
        print(f"Alias '{alias}' already exists")
        return False

    if not update_aliases(url, auth, aliases | {alias}):
        return False

    print(f"Created alias '{alias}'")
    return True


def update_aliases(url: str, auth: GandiAuth, aliases: List[str]) -> bool:
    res = requests.patch(url, json={"aliases": aliases}, auth=auth)
    return res.status_code == 200
def remove_alias(url: str, auth: GandiAuth, alias: str, aliases: Set[str]) -> bool:
    if alias not in aliases:
        print(f"Alias '{alias}' does not exist")
        return False

    if not update_aliases(url, auth, aliases - {alias}):
        return False

    print(f"Removed alias '{alias}'")
    return True


def update_aliases(url: str, auth: GandiAuth, aliases: Set[str]) -> bool:
    res = requests.patch(url, json={"aliases": list(aliases)}, auth=auth)
    try:
        res.raise_for_status()
    except requests.HTTPError as e:
        print("Error occurred while updating aliases")
        print(e.args)
        return False

    return True