mirror of
https://github.com/kopia/kopia.git
synced 2026-10-20 05:14:00 -04:00
refactored various show command into one
This commit is contained in:
1 parent
a48d54f00b
commit
e76f1f8a72
8 files changed
+92
-25
No files matched your search
@@ -16,6 +16,7 @@ var (
|
||||
metadataCommands = app.Command("metadata", "Low-level commands to manipulate metadata items.").Alias("md")
|
||||
objectCommands = app.Command("object", "Commands to manipulate objects in repository.").Alias("obj")
|
||||
blockCommands = app.Command("block", "Commands to manipulate blocks in repository.").Alias("blk")
|
||||
blockIndexCommands = blockCommands.Command("index", "Commands to manipulate block indexes.")
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
||||
)
|
||||
|
||||
var (
|
||||
blockIndexListCommand = blockIndexCommands.Command("list", "List block indexes").Alias("ls")
|
||||
)
|
||||
|
||||
func runListBlockIndexesAction(context *kingpin.ParseContext) error {
|
||||
rep := mustOpenRepository(nil)
|
||||
defer rep.Close()
|
||||
|
||||
ch, cancel := rep.Storage.ListBlocks("P")
|
||||
defer cancel()
|
||||
|
||||
for b := range ch {
|
||||
fmt.Printf("%-34v %10v %v\n", b.BlockID, b.Length, b.TimeStamp.Local().Format(timeFormat))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
blockIndexListCommand.Action(runListBlockIndexesAction)
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
optimizeCommand = blockCommands.Command("reindex", "Optimize block indexes.")
|
||||
optimizeCommand = blockIndexCommands.Command("optimize", "Optimize block indexes.")
|
||||
optimizeMinAge = optimizeCommand.Flag("min-age", "Minimum age of blocks to re-index").Default("24h").Duration()
|
||||
)
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
||||
)
|
||||
|
||||
var (
|
||||
blockIndexShowCommand = blockIndexCommands.Command("show", "List block indexes").Alias("cat")
|
||||
blockIndexShowIDs = blockIndexShowCommand.Arg("id", "IDs of index blocks to show").Required().Strings()
|
||||
)
|
||||
|
||||
func runShowBlockIndexesAction(context *kingpin.ParseContext) error {
|
||||
rep := mustOpenRepository(nil)
|
||||
defer rep.Close()
|
||||
|
||||
for _, blockID := range *blockIndexShowIDs {
|
||||
data, err := rep.Blocks.GetBlock(blockID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't read block %q: %v", blockID, err)
|
||||
}
|
||||
|
||||
if err := showContentWithFlags(bytes.NewReader(data), true, true); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
blockIndexShowCommand.Action(runShowBlockIndexesAction)
|
||||
}
|
||||
@@ -11,9 +11,7 @@ import (
|
||||
var (
|
||||
showBlockCommand = blockCommands.Command("show", "Show contents of a block.").Alias("cat")
|
||||
|
||||
showBlockIDs = showBlockCommand.Arg("id", "IDs of blocks to show").Required().Strings()
|
||||
showBlockJSON = showBlockCommand.Flag("json", "Pretty-print JSON content").Short('j').Bool()
|
||||
showBlockUnzip = showBlockCommand.Flag("unzip", "Transparently unzip the content").Short('z').Bool()
|
||||
showBlockIDs = showBlockCommand.Arg("id", "IDs of blocks to show").Required().Strings()
|
||||
)
|
||||
|
||||
func runShowBlockCommand(context *kingpin.ParseContext) error {
|
||||
@@ -35,9 +33,10 @@ func showBlock(r *repo.Repository, blockID string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return showContent(bytes.NewReader(data), *showBlockUnzip, *showBlockJSON)
|
||||
return showContent(bytes.NewReader(data))
|
||||
}
|
||||
|
||||
func init() {
|
||||
setupShowCommand(showBlockCommand)
|
||||
showBlockCommand.Action(runShowBlockCommand)
|
||||
}
|
||||
@@ -2,9 +2,6 @@ package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/kopia/kopia/repo"
|
||||
|
||||
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
||||
)
|
||||
@@ -18,19 +15,10 @@ func runListObjectsAction(context *kingpin.ParseContext) error {
|
||||
rep := mustOpenRepository(nil)
|
||||
defer rep.Close()
|
||||
|
||||
ch, cancel := rep.Storage.ListBlocks(*objectListPrefix)
|
||||
defer cancel()
|
||||
info := rep.Blocks.ListBlocks(*objectListPrefix, "all")
|
||||
|
||||
for b := range ch {
|
||||
if b.Error != nil {
|
||||
return b.Error
|
||||
}
|
||||
|
||||
if strings.HasPrefix(b.BlockID, repo.MetadataBlockPrefix) {
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("D%-34v %10v %v\n", b.BlockID, b.Length, b.TimeStamp.Local().Format(timeFormat))
|
||||
for _, b := range info {
|
||||
fmt.Printf("D%-34v %10v %v\n", b.BlockID, b.Length, b.Timestamp.Local().Format(timeFormat))
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -15,8 +15,6 @@ var (
|
||||
showCommand = objectCommands.Command("show", "Show contents of a repository object.").Alias("cat")
|
||||
|
||||
showObjectIDs = showCommand.Arg("id", "IDs of objects to show").Required().Strings()
|
||||
showJSON = showCommand.Flag("json", "Pretty-print JSON content").Short('j').Bool()
|
||||
showUnzip = showCommand.Flag("unzip", "Transparently unzip the content").Short('z').Bool()
|
||||
)
|
||||
|
||||
func runShowCommand(context *kingpin.ParseContext) error {
|
||||
@@ -48,9 +46,10 @@ func showObject(r *repo.Repository, oid object.ID) error {
|
||||
}
|
||||
defer rd.Close()
|
||||
|
||||
return showContent(rd, *showUnzip, *showJSON)
|
||||
return showContent(rd)
|
||||
}
|
||||
|
||||
func init() {
|
||||
setupShowCommand(showCommand)
|
||||
showCommand.Action(runShowCommand)
|
||||
}
|
||||
+18
-2
@@ -8,9 +8,25 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"gopkg.in/alecthomas/kingpin.v2"
|
||||
)
|
||||
|
||||
func showContent(rd io.Reader, unzip, formatJSON bool) error {
|
||||
var (
|
||||
commonIndentJSON bool
|
||||
commonUnzip bool
|
||||
)
|
||||
|
||||
func setupShowCommand(cmd *kingpin.CmdClause) {
|
||||
cmd.Flag("json", "Pretty-print JSON content").Short('j').BoolVar(&commonIndentJSON)
|
||||
cmd.Flag("unzip", "Transparently unzip the content").Short('z').BoolVar(&commonUnzip)
|
||||
}
|
||||
|
||||
func showContent(rd io.Reader) error {
|
||||
return showContentWithFlags(rd, commonUnzip, commonIndentJSON)
|
||||
}
|
||||
|
||||
func showContentWithFlags(rd io.Reader, unzip, indentJSON bool) error {
|
||||
if unzip {
|
||||
gz, err := gzip.NewReader(rd)
|
||||
if err != nil {
|
||||
@@ -21,7 +37,7 @@ func showContent(rd io.Reader, unzip, formatJSON bool) error {
|
||||
}
|
||||
|
||||
var buf1, buf2 bytes.Buffer
|
||||
if formatJSON {
|
||||
if indentJSON {
|
||||
if _, err := io.Copy(&buf1, rd); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user