Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions emborg/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -1516,22 +1516,42 @@ class ListCommand(Command):
Options:
-e, --include-external list all archives in repository, not just
those associated with this configuration
-f <N>, --first <N> only show the first <N> archives
-l <N>, --last <N> only show the last <N> 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,
)
Expand Down
30 changes: 19 additions & 11 deletions emborg/emborg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion emborg/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
emborg [options] [<command> [<args>...]]

Options:
-c <cfgname>, --config <cfgname> Specifies the configuration to use.
-c <cfgname>, --config <cfgname> 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.
Expand Down
75 changes: 75 additions & 0 deletions tests/test-cases.nt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down