summaryrefslogtreecommitdiffstats
path: root/packages/meshbay-cli/src/meshbay_cli/commands/decline.py
blob: 23c7bb8a097542df37f89fb5660954a7eeb0928a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""`decline`: answer an invitation no. Asked first; `--yes` answers for a script."""

import json

from ..client.hub import HubError
from ..client.paths import handle
from ..errors import NOT_FOUND, CliError
from ..output import confirm, safe, sentence
from .context import Context
from .invitations import find_invitation

HELP = "decline an invitation"


def add_parser(sub, parent) -> None:
    p = sub.add_parser("decline", parents=[parent], help=HELP, description=sentence(HELP)
                       + ". Only a new invitation from the group's owner brings it back.")
    p.add_argument("invitation", metavar="INVITATION",
                   help="the group, as `invitations` lists it")
    p.add_argument("-y", "--yes", action="store_true", help="do not ask for confirmation")


async def run(ctx: Context) -> int:
    await ctx.hub.check_version()
    account = ctx.account()
    try:
        group = find_invitation(await account.call(account.hub.invitations),
                                ctx.args.invitation)
        name = handle(group)
        confirm(f"Decline the invitation to {safe(name)}?", ctx.args.yes)
        try:
            await account.call(account.hub.decline_invitation, group["id"])
        except HubError as e:
            if e.status == 404:
                raise CliError(NOT_FOUND, "That invitation is no longer waiting.") from e
            raise
    finally:
        await account.close()
    if ctx.args.json:
        print(json.dumps({"declined": True, "path": name, "id": group["id"]}))
    else:
        print(f"Declined the invitation to {safe(name)}.")
    return 0