~gpanders/gandi-cli

5300603bfffa5d3815cd024875143e58a5f6bfa0 — Greg Anders 2 months ago a09e14d
Allow config params to be defined in environment

Priority of configuration parameters is

  1. Commandline arguments
  2. Environment variables
  3. Config file

Also remove underscores from keys in config file (e.g. "api_key" =>
"apikey").
3 files changed, 38 insertions(+), 13 deletions(-)

M src/gandi/commands/alias.py
M src/gandi/commands/mbox.py
M src/gandi/commands/setup.py
M src/gandi/commands/alias.py => src/gandi/commands/alias.py +22 -3
@@ 1,4 1,5 @@
import json
import os

import requests



@@ 17,8 18,26 @@ def create_command(parser):


def alias(config, auth, args):
    domain = args.domain if args.domain is not None else config["domain"]
    mailbox_id = args.mailbox if args.mailbox is not None else config["mailboxid"]
    domain = (
        args.domain
        if args.domain is not None
        else os.environ.get("GANDI_DOMAIN", config.get("domain"))
    )

    if not domain:
        print("Missing domain")
        return False

    mailbox_id = (
        args.mailbox
        if args.mailbox is not None
        else os.environ.get("GANDI_MAILBOX_ID", config.get("mailboxid"))
    )

    if not mailbox_id:
        print("Missing mailbox ID")
        return False

    url = f"https://api.gandi.net/v5/email/mailboxes/{domain}/{mailbox_id}"

    aliases = get_aliases(url, auth)


@@ 26,7 45,7 @@ def alias(config, auth, args):
        return False

    if args.list:
        print("\n".join(sorted(aliases)))
        print(os.linesep.join(sorted(aliases)))
        return True

    if args.add:

M src/gandi/commands/mbox.py => src/gandi/commands/mbox.py +12 -6
@@ 1,4 1,5 @@
import json
import os

import requests



@@ 14,13 15,18 @@ def create_command(parser):


def mbox(config, auth, args):
    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))
    domain = (
        args.domain
        if args.domain is not None
        else os.environ.get("GANDI_DOMAIN", config.get("domain"))
    )

    if not domain:
        print("Missing domain")
        return False

    url = f"https://api.gandi.net/v5/email/mailboxes/{domain}"

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


@@ 40,7 46,7 @@ def mbox(config, auth, args):
        ]

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

    return True


M src/gandi/commands/setup.py => src/gandi/commands/setup.py +4 -4
@@ 8,7 8,7 @@ def create_command(parser):


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


@@ 17,7 17,7 @@ def setup(config, *args):
    domain = config.get("domain", "")
    domain = input("Default domain [{}]: ".format(domain)) or domain

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

    xdg_config_home = os.environ.get("XDG_CONFIG_HOME", "~/.config")


@@ 25,12 25,12 @@ def setup(config, *args):
    conf_file.parent.mkdir(parents=True, exist_ok=True)

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

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

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