~gpanders/gandi-cli unlisted

eb7bf910e87addac3dd5df856c36efaab3416c53 — Greg Anders 6 months ago a7b0dac
Add mbox subcommand
4 files changed, 72 insertions(+), 1 deletions(-)

M README.md
M src/gandi/commands/__init__.py
A src/gandi/commands/mbox.py
M src/gandi/console.py
M README.md => README.md +10 -0
@@ 29,7 29,11 @@ Create a file at `$XDG_CONFIG_HOME/gandi/config` with the following format
```ini
[gandi]
api_key = <your Gandi API key>

; Optional: can be given on command line if not in config file
domain = <your Gandi domain>

; Optional: can be given on command line if not in config file
mailbox_id = <your Gandi mailbox ID>
```



@@ 54,6 58,12 @@ Remove an existing alias

    gandi alias --remove ALIAS

**mbox**: Manage email mailboxes

List mailboxes

    gandi mbox --list

Contributing
------------


M src/gandi/commands/__init__.py => src/gandi/commands/__init__.py +2 -1
@@ 1,3 1,4 @@
__all__ = ["alias"]
__all__ = ["alias", "mbox"]

import gandi.commands.alias
import gandi.commands.mbox

A src/gandi/commands/mbox.py => src/gandi/commands/mbox.py +59 -0
@@ 0,0 1,59 @@
import argparse
import json
from typing import Dict, List, Optional

import requests

from gandi.auth import GandiAuth


def create_command(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
    subparser = parser.add_parser("mbox", help="manage email mailboxes")
    subparser.add_argument("-d", "--domain", type=str)

    actions = subparser.add_mutually_exclusive_group(required=True)
    actions.add_argument("-l", "--list", action="store_true")

    subparser.set_defaults(func=mbox)
    return subparser


def mbox(config: Dict[str, str], auth: GandiAuth, args: argparse.Namespace) -> bool:
    try:
        domain = args.domain if args.domain is not None else config["domain"]
        url = f"https://api.gandi.net/v5/email/mailboxes/{domain}"
    except KeyError as e:
        print("Missing required config parameter: {}".format(*e.args))
        return False

    mboxes = get_mailboxes(url, auth)
    if mboxes is None:
        return False

    if args.list:
        fmt = "{:<30} {:<16} {:<8} {:>12}   {}"
        header = fmt.format("Address", "Domain", "Type", "Quota Used", "Mailbox ID")
        rows = [
            fmt.format(
                mbox["address"],
                mbox["domain"],
                mbox["mailbox_type"],
                mbox["quota_used"],
                mbox["id"],
            )
            for mbox in mboxes
        ]

        print(header)
        print("\n".join(rows))

    return True


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

    data = json.loads(res.text)
    return data

M src/gandi/console.py => src/gandi/console.py +1 -0
@@ 35,6 35,7 @@ def parse_args() -> argparse.Namespace:

    subparsers = parser.add_subparsers(title="available commands")
    commands.alias.create_command(subparsers)
    commands.mbox.create_command(subparsers)

    return parser.parse_args()