Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to limit snapshots list #3167

Merged
merged 1 commit into from
May 13, 2021

Conversation

renard
Copy link

@renard renard commented Dec 16, 2020

This patch adds a --limit option to limit snapshots list to the n
last snapshots. It is very similar to the --last one but does not
limit to one entry.

Output example:

$ restic snapshots --limit 2
repository 0d3eb989 opened successfully, password is correct
ID        Time                 Host        Tags        Paths
------------------------------------------------------------
5a33bdcc  2020-12-14 12:30:00  local                   /home
73887d8e  2020-12-15 12:30:00  local                   /home
------------------------------------------------------------
2 snapshots

To keep compatibility with previous versions a new function has been
added: FilterSnapshotsWithLimit. This function that filters a list
of snapshots to only return the N last entries for each hostname and
path. FilterLastSnapshots calls FilterSnapshotsWithLimit(list, 1).

Signed-off-by: Sébastien Gross <seb•ɑƬ•chezwam•ɖɵʈ•org>

What does this PR change? What problem does it solve?

--last option limit to the very last snapshot for each hosts. In
some case you want to show the n last snapshots. For example if you
want a weekly report of daily snapshots you cannot have this but by
using shell tools and wont report the correct number of snapshots.

This PR adds a --limit to limit display of the n last snapshots (see
above).

Was the change discussed in an issue or in the forum before?

No.

Checklist

  • I have read the Contribution Guidelines
  • I have enabled maintainer edits for this PR
  • I have added tests for all changes in this PR
  • I have added documentation for the changes (in the manual)
  • There's a new file in changelog/unreleased/ that describes the changes for our users (template here)
  • I have run gofmt on the code in all commits
  • All commit messages are formatted in the same style as the other commits in the repo
  • I'm done, this Pull Request is ready for review

@renard renard force-pushed the limit-snapshots-list branch 2 times, most recently from e44134d to 7995786 Compare December 16, 2020 10:01
@renard
Copy link
Author

renard commented Dec 16, 2020

I didn't want to do deep changes with this patch. I am not sure that cobra allows to have flags with optional value.

Ideally it would be better to reuse the --last with following behavior:

  • not set: list all snapshosts
  • --last: only list last snapshot (current behavor)
  • --last 10: only list last 10 snapshots (what `--limit actually does).

Not sure about the test, it seems that snapshots does not have any check but for a regression bug.

@rawtaz
Copy link
Contributor

rawtaz commented Dec 16, 2020

I agree that reusing the --last option is better than introducing a --limit option.

@renard
Copy link
Author

renard commented Dec 16, 2020

What would be the best option then?

I can convert --last to take an integer argument. I am not sure I can define it to act like I suggested above.
Or I can make it require an argument but this may break existing scripts.

OTOH is it a problem to change FilterLastSnapshots to accept a limit argument? or would it be better to introduce the FilterSnapshotsWithLimit like I did?

Thanks for your feedback.

Copy link
Member

@MichaelEischer MichaelEischer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be possible to get --last=n as parameter type. However, that looks like it could cause a lot of confusion, as we only use that variant for --verbose.

After discussing with @rawtaz we arrived at the following suggestion: Name the parameter --latest n instead of limit and deprecate --last, but make sure that it still works. Cobra supports this via something like f.MarkDeprecated("last", "use --latest 1").

cmd/restic/cmd_snapshots.go Outdated Show resolved Hide resolved
doc/bash-completion.sh Outdated Show resolved Hide resolved
changelog/unreleased/pull-3167 Outdated Show resolved Hide resolved
changelog/unreleased/pull-3167 Outdated Show resolved Hide resolved
@renard renard force-pushed the limit-snapshots-list branch 3 times, most recently from fdfdc3c to ff6558e Compare March 29, 2021 14:14
This patch adds a `--latest` option to limit snapshots list to the n
last snapshots. It is very similar to the `--last` one but does not
limit to one entry. It also deprecates the `--last` flag usage in
favor of `--latest 1`

Output example:

    $ restic snapshots --latest 2
    repository 0d3eb989 opened successfully, password is correct
    ID        Time                 Host        Tags        Paths
    ------------------------------------------------------------
    5a33bdcc  2020-12-14 12:30:00  local                   /home
    73887d8e  2020-12-15 12:30:00  local                   /home
    ------------------------------------------------------------
    2 snapshots

Signed-off-by: Sébastien Gross <seb•ɑƬ•chezwam•ɖɵʈ•org>
Copy link
Member

@MichaelEischer MichaelEischer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I've removed the man page modification and rebased the commit to the current master branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants