Files
kopia/cli/command_content_list.go
Jarek Kowalski 70d4c8764a cli: improvements to content selection for list/rewrite/stats/verify (#409)
They now uniformly support 3 flags:

--prefix=P       selects contents with the specified prefix
--prefixed       selects contents with ANY prefix
--non-prefixed   selects non-prefixed contents

Also changed content manager iteration API to support ranges.

cli: add --prefix to 'blob gc' and 'blob stats'
2020-04-06 18:43:41 -07:00

76 lines
2.0 KiB
Go

package cli
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/stats"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/content"
)
var (
contentListCommand = contentCommands.Command("list", "List contents").Alias("ls")
contentListLong = contentListCommand.Flag("long", "Long output").Short('l').Bool()
contentListIncludeDeleted = contentListCommand.Flag("deleted", "Include deleted content").Bool()
contentListDeletedOnly = contentListCommand.Flag("deleted-only", "Only show deleted content").Bool()
contentListSummary = contentListCommand.Flag("summary", "Summarize the list").Short('s').Bool()
contentListHuman = contentListCommand.Flag("human", "Human-readable output").Short('h').Bool()
)
func runContentListCommand(ctx context.Context, rep *repo.DirectRepository) error {
var totalSize stats.CountSum
err := rep.Content.IterateContents(
ctx,
content.IterateOptions{
Range: contentIDRange(),
IncludeDeleted: *contentListIncludeDeleted || *contentListDeletedOnly,
},
func(b content.Info) error {
if *contentListDeletedOnly && !b.Deleted {
return nil
}
totalSize.Add(int64(b.Length))
if *contentListLong {
optionalDeleted := ""
if b.Deleted {
optionalDeleted = " (deleted)"
}
fmt.Printf("%v %v %v %v+%v%v\n",
b.ID,
formatTimestamp(b.Timestamp()),
b.PackBlobID,
b.PackOffset,
maybeHumanReadableBytes(*contentListHuman, int64(b.Length)),
optionalDeleted)
} else {
fmt.Printf("%v\n", b.ID)
}
return nil
})
if err != nil {
return errors.Wrap(err, "error iterating")
}
if *contentListSummary {
count, sz := totalSize.Approximate()
fmt.Printf("Total: %v contents, %v total size\n",
maybeHumanReadableCount(*contentListHuman, int64(count)),
maybeHumanReadableBytes(*contentListHuman, sz))
}
return nil
}
func init() {
contentListCommand.Action(directRepositoryAction(runContentListCommand))
setupContentIDRangeFlags(contentListCommand)
}