Files
kopia/cli/command_content_show.go
Jarek Kowalski d2288c443f cli: major refactoring (#1046)
cli: major refactoring of how CLI commands are registered

The goal is to eliminate flags as global variables to allow for better
testing. Each command and subcommand and most sets of flags are now
their own struct with 'setup()' methods that attached the flags or
subcommand to the provided parent.

This change is 94.3% mechanical, but is fully organic and hand-made.

* introduced cli.appServices interface which provides the environment in which commands run
* remove auto-maintenance global flag
* removed globals in memory_tracking.go
* removed globals from cli_progress.go
* removed globals from the update_check.go
* moved configPath into TheApp
* removed remaining globals from config.go
* refactored logfile to get rid of global variables
* removed 'app' global variable
* linter fixes
* fixed password_*.go build
* fixed BSD build
2021-05-03 10:28:00 -07:00

46 lines
1.2 KiB
Go

package cli
import (
"bytes"
"context"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/content"
)
type commandContentShow struct {
ids []string
indentJSON bool
decompress bool
}
func (c *commandContentShow) setup(svc appServices, parent commandParent) {
cmd := parent.Command("show", "Show contents by ID.").Alias("cat")
cmd.Arg("id", "IDs of contents to show").Required().StringsVar(&c.ids)
cmd.Flag("json", "Pretty-print JSON content").Short('j').BoolVar(&c.indentJSON)
cmd.Flag("unzip", "Transparently decompress the content").Short('z').BoolVar(&c.decompress)
cmd.Action(svc.directRepositoryReadAction(c.run))
}
func (c *commandContentShow) run(ctx context.Context, rep repo.DirectRepository) error {
for _, contentID := range toContentIDs(c.ids) {
if err := c.contentShow(ctx, rep, contentID); err != nil {
return err
}
}
return nil
}
func (c *commandContentShow) contentShow(ctx context.Context, r repo.DirectRepository, contentID content.ID) error {
data, err := r.ContentReader().GetContent(ctx, contentID)
if err != nil {
return errors.Wrapf(err, "error getting content %v", contentID)
}
return showContentWithFlags(bytes.NewReader(data), c.decompress, c.indentJSON)
}