Gitlab Community Edition Instance

rma.py 1.45 KB
Newer Older
1
2
3
4
5
6
7
8
"""
Remove one or more archives.
"""
from contextlib import ExitStack
from pycdstar3 import ApiError


def register(subparsers):
9
10
11
12
13
14
15
16
    parser = subparsers.add_parser(
        "rma", help=__doc__.strip().splitlines()[0], description=__doc__
    )

    parser.add_argument(
        "-f",
        "--force",
        action="store_true",
Marcel Hellkamp's avatar
Marcel Hellkamp committed
17
        help="Do not prompt and ignore missing archives or access errors.",
18
    )
19
    parser.add_argument("-y", "--yes", action="store_true", help="Do not prompt")
20
21
22
    parser.add_argument(
        "ARCHIVE", nargs="+", help="Archive IDs. Repeat to delete multiple archives."
    )
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    parser.set_defaults(main=remove_files)


def remove_files(ctx, args):
    client = ctx.client
    vault = ctx.vault
    archives = args.ARCHIVE
    force = args.force
    yes = args.yes

    with ExitStack() as stack:
        if len(archives) > 1:
            stack.enter_context(client.begin(autocommit=True))

mhellka's avatar
mhellka committed
37
38
39
        prompt = (
            "Do you really want to delete {} archives? "
            "This cannot be undone!".format(len(archives))
40
        )
41
42
43
44
45
46
47
48
49
50
51
52
        if not (yes or force or ctx.ask_yes(prompt)):
            return

        for archive in archives:
            try:
                client.delete_archive(vault, archive)
                ctx.print("Deleting: {}".format(archive))
            except ApiError as e:
                if force:
                    ctx.print("  Ignoring error (--force) {}", e)
                    continue
                raise