Files
kopia/cli/command_block_verify.go
2019-05-11 12:34:14 -07:00

63 lines
1.3 KiB
Go

package cli
import (
"context"
"fmt"
"github.com/kopia/repo"
"github.com/pkg/errors"
)
var (
verifyBlockCommand = blockCommands.Command("verify", "Verify contents of a block.")
verifyBlockIDs = verifyBlockCommand.Arg("id", "IDs of blocks to show (or 'all')").Required().Strings()
)
func runVerifyBlockCommand(ctx context.Context, rep *repo.Repository) error {
for _, blockID := range *verifyBlockIDs {
if blockID == "all" {
return verifyAllBlocks(ctx, rep)
}
if err := verifyBlock(ctx, rep, blockID); err != nil {
return err
}
}
return nil
}
func verifyAllBlocks(ctx context.Context, rep *repo.Repository) error {
blockIDs, err := rep.Blocks.ListBlocks("")
if err != nil {
return errors.Wrap(err, "unable to list blocks")
}
var errorCount int
for _, blockID := range blockIDs {
if err := verifyBlock(ctx, rep, blockID); err != nil {
errorCount++
}
}
if errorCount == 0 {
return nil
}
return fmt.Errorf("encountered %v errors", errorCount)
}
func verifyBlock(ctx context.Context, r *repo.Repository, blockID string) error {
if _, err := r.Blocks.GetBlock(ctx, blockID); err != nil {
log.Warningf("block %v is invalid: %v", blockID, err)
return err
}
log.Infof("block %v is ok", blockID)
return nil
}
func init() {
verifyBlockCommand.Action(repositoryAction(runVerifyBlockCommand))
}