mirror of
https://github.com/kopia/kopia.git
synced 2026-03-15 21:01:37 -04:00
* logging: added logger wrappers for Broadcast and Prefix * nit: moved max hash size to a named constant * content: added internal logger * content: replaced context-based logging with explicit Loggers This will capture the logger.Logger associated with the context when the repository is opened and will reuse it for all logs instead of creating new logger for each log message. The new logger will also write logs to the internal logger in addition to writing to a log file/console. * cli: allow decrypting all blobs whose names start with _ * maintenance: added logs cleanup * cli: commands to view logs * cli: log selected command on each write session
92 lines
1.9 KiB
Go
92 lines
1.9 KiB
Go
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/kopia/kopia/internal/iocopy"
|
|
"github.com/kopia/kopia/repo"
|
|
"github.com/kopia/kopia/repo/blob"
|
|
)
|
|
|
|
type commandBlobShow struct {
|
|
blobShowDecrypt bool
|
|
blobShowIDs []string
|
|
|
|
out textOutput
|
|
}
|
|
|
|
func (c *commandBlobShow) setup(svc appServices, parent commandParent) {
|
|
cmd := parent.Command("show", "Show contents of BLOBs").Alias("cat")
|
|
cmd.Flag("decrypt", "Decrypt blob if possible").BoolVar(&c.blobShowDecrypt)
|
|
cmd.Arg("blobID", "Blob IDs").Required().StringsVar(&c.blobShowIDs)
|
|
cmd.Action(svc.directRepositoryReadAction(c.run))
|
|
|
|
c.out.setup(svc)
|
|
}
|
|
|
|
func (c *commandBlobShow) run(ctx context.Context, rep repo.DirectRepository) error {
|
|
for _, blobID := range c.blobShowIDs {
|
|
if err := c.maybeDecryptBlob(ctx, c.out.stdout(), rep, blob.ID(blobID)); err != nil {
|
|
return errors.Wrap(err, "error presenting blob")
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *commandBlobShow) maybeDecryptBlob(ctx context.Context, w io.Writer, rep repo.DirectRepository, blobID blob.ID) error {
|
|
var (
|
|
d []byte
|
|
err error
|
|
)
|
|
|
|
d, err = rep.BlobReader().GetBlob(ctx, blobID, 0, -1)
|
|
|
|
if c.blobShowDecrypt && canDecryptBlob(blobID) {
|
|
d, err = rep.Crypter().DecryptBLOB(d, blobID)
|
|
|
|
if isJSONBlob(blobID) && err == nil {
|
|
var b bytes.Buffer
|
|
|
|
if err = json.Indent(&b, d, "", " "); err != nil {
|
|
return errors.Wrap(err, "invalid JSON")
|
|
}
|
|
|
|
d = b.Bytes()
|
|
}
|
|
}
|
|
|
|
if err != nil {
|
|
return errors.Wrapf(err, "error getting %v", blobID)
|
|
}
|
|
|
|
if _, err := iocopy.Copy(w, bytes.NewReader(d)); err != nil {
|
|
return errors.Wrap(err, "error copying data")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func canDecryptBlob(b blob.ID) bool {
|
|
switch b[0] {
|
|
case '_', 'n', 'm', 'l':
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func isJSONBlob(b blob.ID) bool {
|
|
switch b[0] {
|
|
case 'm', 'l':
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|