Skip to content

Commit

Permalink
reintroduce find subcommand
Browse files Browse the repository at this point in the history
the `last` symlink no longer exists, so we need another way to easily
get the latest revision in external scripts
  • Loading branch information
Johann Bahl committed Dec 7, 2023
1 parent 4bc6844 commit fed1563
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelog.d/20231207_235814_jb_reintroduce_find.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.. A new scriv changelog fragment.
- reintroduce `find` subcommand
26 changes: 26 additions & 0 deletions src/backy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ def restore(self, revision, target):
b = backy.backup.Backup(self.path, self.log)
b.restore(revision, target)

def find(self, revision, uuid):
b = backy.backup.Backup(self.path, self.log)
if uuid:
print(b.find(revision).uuid)
else:
print(b.find(revision).filename)

def forget(self, revision):
b = backy.backup.Backup(self.path, self.log)
b.forget_revision(revision)
Expand Down Expand Up @@ -305,6 +312,25 @@ def setup_argparser():
)
p.set_defaults(func="purge")

# FIND
p = subparsers.add_parser(
"find",
help="Print full path to a given revision's image file",
)
p.add_argument(
"--uuid",
action="store_true",
help="Print uuid instead of full path",
)
p.add_argument(
"-r",
"--revision",
metavar="SPEC",
default="latest",
help="use revision SPEC to find",
)
p.set_defaults(func="find")

# STATUS
p = subparsers.add_parser(
"status",
Expand Down
34 changes: 32 additions & 2 deletions src/backy/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_display_usage(capsys, argv):
assert (
"""\
usage: pytest [-h] [-v] [-l LOGFILE] [-b BACKUPDIR]
{client,backup,restore,purge,status,\
{client,backup,restore,purge,find,status,\
upgrade,scheduler,distrust,verify,forget}
...
"""
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_display_help(capsys, argv):
Ellipsis(
"""\
usage: pytest [-h] [-v] [-l LOGFILE] [-b BACKUPDIR]
{client,backup,restore,purge,status,\
{client,backup,restore,purge,find,status,\
upgrade,scheduler,distrust,verify,forget}
...
Expand Down Expand Up @@ -198,6 +198,36 @@ def test_call_backup(tmpdir, capsys, argv, monkeypatch):
assert exit.value.code == 0


def test_call_find(capsys, backup, argv, monkeypatch):
monkeypatch.setattr(backy.main.Command, "find", print_args)
argv.extend(["-v", "-b", backup.path, "find", "-r", "1"])
utils.log_data = ""
with pytest.raises(SystemExit) as exit:
backy.main.main()
assert exit.value.code == 0
out, err = capsys.readouterr()
assert (
Ellipsis(
"""\
(<backy.main.Command object at ...>,)
{'revision': '1', 'uuid': False}
"""
)
== out
)
assert (
Ellipsis(
"""\
... D command/invoked args='... -v -b ... find -r 1'
... D command/parsed func='find' func_args={'uuid': False, 'revision': '1'}
... D command/successful \n\
"""
)
== utils.log_data
)
assert exit.value.code == 0


@pytest.mark.parametrize(
["action", "args"],
[
Expand Down

0 comments on commit fed1563

Please sign in to comment.