Skip to content

Commit

Permalink
Add kopia snapshot verify command builder helper (#3141)
Browse files Browse the repository at this point in the history
  • Loading branch information
redgoat650 authored Sep 27, 2024
1 parent 232f2f5 commit dac52b2
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/kopia/command/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ const (
showSubCommand = "show"
snapshotSubCommand = "snapshot"
statsSubCommand = "stats"
verifySubCommand = "verify"

allFlag = "--all"
configFileFlag = "--config-file"
deleteFlag = "--delete"
deltaFlag = "--delta"
directoryIDFlag = "--directory-id"
fileIDFlag = "--file-id"
fileParallelismFlag = "--file-parallelism"
fileQueueLengthFlag = "--file-queue-length"
filterFlag = "--filter"
globalFlag = "--global"
jsonFlag = "--json"
Expand All @@ -44,15 +49,18 @@ const (
fileLogLevelFlag = "--file-log-level"
LogLevelError = "error"
LogLevelInfo = "info"
maxErrorsFlag = "--max-errors"
parallelFlag = "--parallel"
passwordFlag = "--password"
progressUpdateIntervalFlag = "--progress-update-interval"
rawFlag = "--raw"
showIdenticalFlag = "--show-identical"
sourcesFlag = "--sources"
tagsFlag = "--tags"
unsafeIgnoreSourceFlag = "--unsafe-ignore-source"
ownerFlag = "--owner"
sparseFlag = "--write-sparse-files"
verifyFilesPercentFlag = "--verify-files-percent"
ignorePermissionsError = "--ignore-permission-errors"
noIgnorePermissionsError = "--no-ignore-permission-errors"

Expand Down
55 changes: 55 additions & 0 deletions pkg/kopia/command/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package command

import (
"fmt"
"strconv"
"time"

Expand Down Expand Up @@ -169,3 +170,57 @@ func SnapListByTags(cmdArgs SnapListByTagsCommandArgs) []string {
args = addTags(cmdArgs.Tags, args)
return stringSliceCommand(args)
}

type SnapshotVerifyCommandArgs struct {
*CommandArgs
DirectoryID []string
FileID []string
Sources []string
SnapshotIDs []string
VerifyFilesPercent *float64
Parallelism *int
FileQueueLength *int
FileParallelism *int
MaxErrors *int
}

// SnapshotVerify returns kopia command verifying snapshots with given snapshot IDs.
func SnapshotVerify(cmdArgs SnapshotVerifyCommandArgs) []string {
args := commonArgs(cmdArgs.CommandArgs)
args = args.AppendLoggable(snapshotSubCommand, verifySubCommand)

if cmdArgs.VerifyFilesPercent != nil {
args = args.AppendLoggableKV(verifyFilesPercentFlag, fmt.Sprintf("%v", *cmdArgs.VerifyFilesPercent))
}

if cmdArgs.Parallelism != nil {
parallelismStr := strconv.Itoa(*cmdArgs.Parallelism)
args = args.AppendLoggableKV(parallelFlag, parallelismStr)
}

if cmdArgs.FileQueueLength != nil {
args = args.AppendLoggableKV(fileQueueLengthFlag, strconv.Itoa(*cmdArgs.FileQueueLength))
}

if cmdArgs.FileParallelism != nil {
args = args.AppendLoggableKV(fileParallelismFlag, strconv.Itoa(*cmdArgs.FileParallelism))
}

for _, dirID := range cmdArgs.DirectoryID {
args = args.AppendLoggableKV(directoryIDFlag, dirID)
}

for _, fileID := range cmdArgs.FileID {
args = args.AppendLoggableKV(fileIDFlag, fileID)
}

for _, source := range cmdArgs.Sources {
args = args.AppendLoggableKV(sourcesFlag, source)
}

for _, snapID := range cmdArgs.SnapshotIDs {
args = args.AppendLoggable(snapID)
}

return stringSliceCommand(args)
}
30 changes: 30 additions & 0 deletions pkg/kopia/command/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,36 @@ func (kSnapshot *KopiaSnapshotTestSuite) TestSnapshotCommands(c *check.C) {
},
expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key snapshot list --all --delta --show-identical --json --tags tag1:val1 --tags tag2:val2",
},
{
f: func() []string {
args := SnapshotVerifyCommandArgs{
CommandArgs: commandArgs,
}
return SnapshotVerify(args)
},
expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key snapshot verify",
},
{
f: func() []string {
vfp := 12.345
p := 123
fql := 456
fp := 890
args := SnapshotVerifyCommandArgs{
CommandArgs: commandArgs,
VerifyFilesPercent: &vfp,
Parallelism: &p,
FileQueueLength: &fql,
FileParallelism: &fp,
DirectoryID: []string{"d1", "d2"},
FileID: []string{"f1", "f2"},
Sources: []string{"s1", "s2"},
SnapshotIDs: []string{"id1", "id2"},
}
return SnapshotVerify(args)
},
expectedLog: "kopia --log-level=error --config-file=path/kopia.config --log-dir=cache/log --password=encr-key snapshot verify --verify-files-percent=12.345 --parallel=123 --file-queue-length=456 --file-parallelism=890 --directory-id=d1 --directory-id=d2 --file-id=f1 --file-id=f2 --sources=s1 --sources=s2 id1 id2",
},
} {
cmd := strings.Join(tc.f(), " ")
c.Check(cmd, check.Equals, tc.expectedLog)
Expand Down

0 comments on commit dac52b2

Please sign in to comment.