mirror of
https://github.com/kopia/kopia.git
synced 2026-01-25 23:08:01 -05:00
* cli: added standard --json flags to several commands Fixes #272 * Update flag description Co-authored-by: Julio López <julio+gh@kasten.io>
65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kopia/kopia/repo"
|
|
)
|
|
|
|
var (
|
|
blockIndexListCommand = indexCommands.Command("list", "List content indexes").Alias("ls").Default()
|
|
blockIndexListSummary = blockIndexListCommand.Flag("summary", "Display index blob summary").Bool()
|
|
blockIndexListIncludeSuperseded = blockIndexListCommand.Flag("superseded", "Include inactive index files superseded by compaction").Bool()
|
|
blockIndexListSort = blockIndexListCommand.Flag("sort", "Index blob sort order").Default("time").Enum("time", "size", "name")
|
|
)
|
|
|
|
func runListBlockIndexesAction(ctx context.Context, rep repo.DirectRepository) error {
|
|
var jl jsonList
|
|
|
|
jl.begin()
|
|
defer jl.end()
|
|
|
|
blks, err := rep.IndexBlobReader().IndexBlobs(ctx, *blockIndexListIncludeSuperseded)
|
|
if err != nil {
|
|
return errors.Wrap(err, "error listing index blobs")
|
|
}
|
|
|
|
switch *blockIndexListSort {
|
|
case "time":
|
|
sort.Slice(blks, func(i, j int) bool {
|
|
return blks[i].Timestamp.Before(blks[j].Timestamp)
|
|
})
|
|
case "size":
|
|
sort.Slice(blks, func(i, j int) bool {
|
|
return blks[i].Length < blks[j].Length
|
|
})
|
|
case "name":
|
|
sort.Slice(blks, func(i, j int) bool {
|
|
return blks[i].BlobID < blks[j].BlobID
|
|
})
|
|
}
|
|
|
|
for _, b := range blks {
|
|
if jsonOutput {
|
|
jl.emit(b)
|
|
} else {
|
|
fmt.Printf("%-40v %10v %v %v\n", b.BlobID, b.Length, formatTimestampPrecise(b.Timestamp), b.Superseded)
|
|
}
|
|
}
|
|
|
|
if *blockIndexListSummary && !jsonOutput {
|
|
fmt.Printf("total %v indexes\n", len(blks))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
registerJSONOutputFlags(blockIndexListCommand)
|
|
blockIndexListCommand.Action(directRepositoryReadAction(runListBlockIndexesAction))
|
|
}
|