a09e14d3865c693cd98e1bce5ecfd0cb207686b2 — Greg Anders 2 months ago 6c61afa
Add setup subcommand
4 files changed, 51 insertions(+), 26 deletions(-)

M src/gandi/commands/__init__.py
A src/gandi/commands/setup.py
M src/gandi/console.py
M README.md => README.md +11 -25
@@ 23,19 23,13 @@ Install using [pipx](https://pipxproject.github.io/pipx/):

Create a file at `$XDG_CONFIG_HOME/gandi/config` with the following format
(`$XDG_CONFIG_HOME` is set to `$HOME/.config` by default):
Run `gandi setup` to create a configuration file at
`$XDG_CONFIG_HOME/gandi/config`. This will ask for your Gandi API key as well
as an (optional) default domain name and an (optional) default mailbox ID.

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>
If you specify a domain name and mailbox ID, subcommands that require these
parameters will use the values supplied in your config file instead of
requiring them as commandline flags.


@@ 44,25 38,17 @@ Usage

### Subcommands

**alias**: Manage email aliases

List existing email aliases
**setup**: Setup the Gandi CLI config file

    gandi alias --list
    gandi setup

Add a new email alias

    gandi alias --add ALIAS

Remove an existing alias
**alias**: Manage email aliases

    gandi alias --remove ALIAS
    gandi alias [-d DOMAIN] [-m MAILBOXID] (-l|--list | -a|--add ALIAS | -r|--remove ALIAS)

**mbox**: Manage email mailboxes

List mailboxes

    gandi mbox --list
    gandi alias [-d DOMAIN] --list


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

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

A src/gandi/commands/setup.py => src/gandi/commands/setup.py +37 -0
@@ 0,0 1,37 @@
import os
from pathlib import Path

def create_command(parser):
    subparser = parser.add_parser("setup", help="setup your Gandi account")

def setup(config, *args):
    api_key = config.get("api_key", "")
    api_key = input("Gandi v5 API key [{}]: ".format(api_key)) or api_key
    if not api_key:
        print("API key is required")
        return False

    domain = config.get("domain", "")
    domain = input("Default domain [{}]: ".format(domain)) or domain

    mailbox_id = config.get("mailbox_id", "")
    mailbox_id = input("Default mailbox ID [{}]: ".format(mailbox_id)) or mailbox_id

    xdg_config_home = os.environ.get("XDG_CONFIG_HOME", "~/.config")
    conf_file = Path(xdg_config_home).expanduser() / "gandi" / "config"
    conf_file.parent.mkdir(parents=True, exist_ok=True)

    with conf_file.open("w") as fil:
        lines = ["[gandi]", "api_key = {}".format(api_key)]
        if domain:
            lines += ["domain = {}".format(domain)]

        if mailbox_id:
            lines += ["mailbox_id = {}".format(mailbox_id)]

        fil.write(os.linesep.join(lines) + os.linesep)

    return True

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

    subparsers = parser.add_subparsers(title="available commands")