fix(cli): prevent duplicate snapshots when --all and a source path are given (#3067)

Adds a check for snapshot create when --all and a source path are
given simultaneously. Returns an error in this case since it would
otherwise create a  duplicate snapshot for the specified source path.

---

Co-authored-by: Julio Lopez <1953782+julio-lopez@users.noreply.github.com>
This commit is contained in:
NickIAm
2023-08-04 03:36:10 +10:00
committed by GitHub
parent e98eb371ed
commit a9c5a993c8
2 changed files with 21 additions and 1 deletions

View File

@@ -55,7 +55,7 @@ func (c *commandSnapshotCreate) setup(svc appServices, parent commandParent) {
cmd := parent.Command("create", "Creates a snapshot of local directory or file.")
cmd.Arg("source", "Files or directories to create snapshot(s) of.").StringsVar(&c.snapshotCreateSources)
cmd.Flag("all", "Create snapshots for files or directories previously backed up by this user on this computer").BoolVar(&c.snapshotCreateAll)
cmd.Flag("all", "Create snapshots for files or directories previously backed up by this user on this computer. Cannot be used when a source path argument is also specified.").BoolVar(&c.snapshotCreateAll)
cmd.Flag("upload-limit-mb", "Stop the backup process after the specified amount of data (in MB) has been uploaded.").PlaceHolder("MB").Default("0").Int64Var(&c.snapshotCreateCheckpointUploadLimitMB)
cmd.Flag("checkpoint-interval", "Interval between periodic checkpoints (must be <= 45 minutes).").Hidden().DurationVar(&c.snapshotCreateCheckpointInterval)
cmd.Flag("description", "Free-form snapshot description.").StringVar(&c.snapshotCreateDescription)
@@ -89,6 +89,10 @@ func (c *commandSnapshotCreate) setup(svc appServices, parent commandParent) {
func (c *commandSnapshotCreate) run(ctx context.Context, rep repo.RepositoryWriter) error {
sources := c.snapshotCreateSources
if c.snapshotCreateAll && len(sources) > 0 {
return errors.New("cannot use --all when a source path argument is specified")
}
if err := maybeAutoUpgradeRepository(ctx, rep); err != nil {
return errors.Wrap(err, "error upgrading repository")
}

View File

@@ -783,3 +783,19 @@ func TestSnapshotCreateAllSnapshotPath(t *testing.T) {
require.Equal(t, "/foo/bar", si[2].Path)
}
}
func TestSnapshotCreateWithAllAndPath(t *testing.T) {
t.Parallel()
runner := testenv.NewInProcRunner(t)
e := testenv.NewCLITest(t, testenv.RepoFormatNotImportant, runner)
defer e.RunAndExpectSuccess(t, "repo", "disconnect")
e.RunAndExpectSuccess(t, "repo", "create", "filesystem", "--path", e.RepoDir)
// creating a snapshot with a directory and --all should fail
e.RunAndExpectSuccess(t, "snapshot", "create", sharedTestDataDir1)
e.RunAndExpectSuccess(t, "snapshot", "create", sharedTestDataDir2)
e.RunAndExpectFailure(t, "snapshot", "create", sharedTestDataDir1, "--all")
}