diff --git a/emborg/command.py b/emborg/command.py index db593bb..c7725c1 100644 --- a/emborg/command.py +++ b/emborg/command.py @@ -1516,22 +1516,42 @@ class ListCommand(Command): Options: -e, --include-external list all archives in repository, not just those associated with this configuration + -f , --first only show the first archives + -l , --last only show the last archives """ ).strip() REQUIRES_EXCLUSIVITY = True - COMPOSITE_CONFIGS = "first" + COMPOSITE_CONFIGS = "all" LOG_COMMAND = True + @classmethod + def run_early(cls, command, args, settings, options): + # validate the command line once, before any configuration is processed + cmdline = docopt(cls.USAGE, argv=[command] + args) + if cmdline["--first"] and cmdline["--last"]: + # borg treats these as mutually exclusive + raise Error("--first and --last cannot be used together.") + @classmethod def run(cls, command, args, settings, options): # read command line cmdline = docopt(cls.USAGE, argv=[command] + args) include_external_archives = cmdline["--include-external"] + first = cmdline["--first"] + last = cmdline["--last"] + + # build borg command line arguments + borg_args = ["--short"] + if first: + borg_args.extend(["--first", first]) + if last: + borg_args.extend(["--last", last]) + borg_args.append(settings.destination()) # run borg borg = settings.run_borg( cmd = "list", - args = ["--short", settings.destination()], + args = borg_args, emborg_opts = options, strip_prefix = include_external_archives, ) diff --git a/emborg/emborg.py b/emborg/emborg.py index bbb3cb5..b08c051 100644 --- a/emborg/emborg.py +++ b/emborg/emborg.py @@ -146,24 +146,32 @@ def initialize(self, name, settings): if not name: name = default if name: - if name not in config_groups: - raise Error( - "unknown configuration.", - culprit = name, - codicil = ( - "Perhaps you forgot to add it to the ‘configurations’ setting?.", - f"Choose from: {conjoin(all_configs)}.", + # a comma-separated list of configs may be given, in which case an + # ad-hoc composite config is formed from the listed configs/groups + requested = [n.strip() for n in name.split(",") if n.strip()] + for n in requested: + if n not in config_groups: + raise Error( + "unknown configuration.", + culprit = n, + codicil = ( + "Perhaps you forgot to add it to the ‘configurations’ setting?.", + f"Choose from: {conjoin(all_configs)}.", + ) ) - ) else: if len(config_groups) > 0: - name = list(config_groups.keys())[0] + requested = [list(config_groups.keys())[0]] else: raise Error("no known configurations.", culprit=CONFIGS_SETTING) # set the config queue - # convert configs to list while preserving order and eliminating dupes - configs = list(dict.fromkeys(config_groups[name])) + # expand each requested config/group into its subconfigs, then convert + # to a list while preserving order and eliminating dupes + configs = [] + for n in requested: + configs.extend(config_groups[n]) + configs = list(dict.fromkeys(configs)) self.configs = configs[:] num_configs = len(configs) if num_configs > 1: diff --git a/emborg/main.py b/emborg/main.py index a098f13..895556d 100644 --- a/emborg/main.py +++ b/emborg/main.py @@ -9,7 +9,8 @@ emborg [options] [ [...]] Options: - -c , --config Specifies the configuration to use. + -c , --config Specifies the configuration to use. May be + a comma-separated list of configurations. -d, --dry-run Run Borg in dry run mode. -h, --help Output basic usage information. -m, --mute Suppress all output. diff --git a/tests/test-cases.nt b/tests/test-cases.nt index 4f0d727..669e4a0 100644 --- a/tests/test-cases.nt +++ b/tests/test-cases.nt @@ -428,6 +428,56 @@ emborg with configs: > run_after_last_backup on test3 > run_after_borg on test3 + # list on a composite config must run on every subconfig, not just the first + roster: + args: --quiet --config tests list + expected: + > run_before_borg on test0 + > test0-\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d + > run_after_borg on test0 + > run_before_borg on test1 + > test1-\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d + > run_after_borg on test1 + > run_before_borg on test2 + > test2-\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d + > run_after_borg on test2 + > run_before_borg on test3 + > test3-\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d + > run_after_borg on test3 + expected_type: regex + + # a comma-separated --config forms an ad-hoc composite of the listed configs + tandem: + args: --quiet --config test0,test1 list + expected: + > run_before_borg on test0 + > test0-\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d + > run_after_borg on test0 + > run_before_borg on test1 + > test1-\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d + > run_after_borg on test1 + expected_type: regex + + # a comma-separated --config may mix a scalar config and a composite group; + # the scalar (test0) is also a member of the group (tests), so it is only + # listed once (duplicates are removed while order is preserved) + medley: + args: --quiet --config test0,tests list + expected: + > run_before_borg on test0 + > test0-\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d + > run_after_borg on test0 + > run_before_borg on test1 + > test1-\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d + > run_after_borg on test1 + > run_before_borg on test2 + > test2-\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d + > run_after_borg on test2 + > run_before_borg on test3 + > test3-\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d + > run_after_borg on test3 + expected_type: regex + motif: args: --quiet --config test0 list expected: @@ -549,6 +599,31 @@ emborg with configs: > run_after_borg on test0 expected_type: regex + # test0 has two archives at this point (created by gravy and whiplash); the + # --first and --last options are forwarded to borg to limit the listing + nibble: + args: --quiet --config test0 list --first 1 + expected: + > run_before_borg on test0 + > test0-\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d + > run_after_borg on test0 + expected_type: regex + + quibble: + args: --quiet --config test0 list --last 1 + expected: + > run_before_borg on test0 + > test0-\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d + > run_after_borg on test0 + expected_type: regex + + # borg does not allow --first and --last together, so emborg rejects the + # combination up front, before any configuration (or hook) is run + squabble: + args: --quiet --config test0 list --first 1 --last 1 + expected: emborg error: --first and --last cannot be used together. + expected_type: error + spoof: args: --quiet --config test0 prune expected: