restic/cmd/restic/cmd_cat.go

215 lines
4.5 KiB
Go
Raw Normal View History

2014-10-05 14:44:59 +02:00
package main
import (
"context"
2014-10-05 14:44:59 +02:00
"encoding/json"
"strings"
2014-10-05 14:44:59 +02:00
2016-09-17 12:36:05 +02:00
"github.com/spf13/cobra"
2017-07-23 14:21:03 +02:00
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/repository"
2017-07-24 17:42:25 +02:00
"github.com/restic/restic/internal/restic"
2014-10-05 14:44:59 +02:00
)
2016-09-17 12:36:05 +02:00
var cmdCat = &cobra.Command{
Use: "cat [flags] [masterkey|config|pack ID|blob ID|snapshot ID|index ID|key ID|lock ID|tree snapshot:subfolder]",
Short: "Print internal objects to stdout",
2016-09-17 12:36:05 +02:00
Long: `
The "cat" command is used to print internal objects to stdout.
EXIT STATUS
===========
Exit status is 0 if the command was successful, and non-zero if there was any error.
2016-09-17 12:36:05 +02:00
`,
DisableAutoGenTag: true,
2016-09-17 12:36:05 +02:00
RunE: func(cmd *cobra.Command, args []string) error {
2022-10-02 23:24:37 +02:00
return runCat(cmd.Context(), globalOptions, args)
2016-09-17 12:36:05 +02:00
},
}
2014-12-07 16:30:52 +01:00
2014-11-30 22:39:58 +01:00
func init() {
2016-09-17 12:36:05 +02:00
cmdRoot.AddCommand(cmdCat)
2014-11-30 22:39:58 +01:00
}
func validateCatArgs(args []string) error {
var allowedCmds = []string{"config", "index", "snapshot", "key", "masterkey", "lock", "pack", "blob", "tree"}
if len(args) < 1 {
return errors.Fatal("type not specified")
}
validType := false
for _, v := range allowedCmds {
if v == args[0] {
validType = true
break
}
}
if !validType {
return errors.Fatalf("invalid type %q, must be one of [%s]", args[0], strings.Join(allowedCmds, "|"))
}
if args[0] != "masterkey" && args[0] != "config" && len(args) != 2 {
return errors.Fatal("ID not specified")
2014-12-07 16:30:52 +01:00
}
return nil
}
func runCat(ctx context.Context, gopts GlobalOptions, args []string) error {
if err := validateCatArgs(args); err != nil {
return err
}
ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock)
2014-12-07 16:30:52 +01:00
if err != nil {
return err
2014-10-05 14:44:59 +02:00
}
defer unlock()
2015-06-27 14:40:18 +02:00
2014-10-05 14:44:59 +02:00
tpe := args[0]
2016-09-01 16:04:29 +02:00
var id restic.ID
if tpe != "masterkey" && tpe != "config" && tpe != "snapshot" && tpe != "tree" {
2016-09-01 16:04:29 +02:00
id, err = restic.ParseID(args[1])
if err != nil {
return errors.Fatalf("unable to parse ID: %v\n", err)
}
2014-10-05 14:44:59 +02:00
}
switch tpe {
2015-05-03 17:37:21 +02:00
case "config":
2016-09-01 16:04:29 +02:00
buf, err := json.MarshalIndent(repo.Config(), "", " ")
2015-05-03 17:37:21 +02:00
if err != nil {
return err
}
Println(string(buf))
2015-05-03 17:37:21 +02:00
return nil
case "index":
buf, err := repo.LoadUnpacked(ctx, restic.IndexFile, id)
if err != nil {
return err
}
Println(string(buf))
return nil
2014-10-05 14:44:59 +02:00
case "snapshot":
sn, _, err := restic.FindSnapshot(ctx, repo, repo, args[1])
2014-10-05 14:44:59 +02:00
if err != nil {
return errors.Fatalf("could not find snapshot: %v\n", err)
2014-10-05 14:44:59 +02:00
}
buf, err := json.MarshalIndent(sn, "", " ")
2014-10-05 14:44:59 +02:00
if err != nil {
return err
}
Println(string(buf))
2014-10-05 14:44:59 +02:00
return nil
case "key":
2022-10-15 16:01:38 +02:00
key, err := repository.LoadKey(ctx, repo, id)
2014-10-05 14:44:59 +02:00
if err != nil {
return err
}
buf, err := json.MarshalIndent(&key, "", " ")
2014-10-05 14:44:59 +02:00
if err != nil {
return err
}
Println(string(buf))
return nil
case "masterkey":
buf, err := json.MarshalIndent(repo.Key(), "", " ")
if err != nil {
return err
}
2014-10-05 14:44:59 +02:00
Println(string(buf))
2014-10-05 14:44:59 +02:00
return nil
case "lock":
lock, err := restic.LoadLock(ctx, repo, id)
2015-06-24 18:17:01 +02:00
if err != nil {
return err
}
buf, err := json.MarshalIndent(&lock, "", " ")
if err != nil {
return err
}
Println(string(buf))
2015-06-24 18:17:01 +02:00
return nil
case "pack":
h := backend.Handle{Type: restic.PackFile, Name: id.String()}
buf, err := backend.LoadAll(ctx, nil, repo.Backend(), h)
if err != nil {
return err
}
2016-09-01 16:04:29 +02:00
hash := restic.Hash(buf)
if !hash.Equal(id) {
Warnf("Warning: hash of data does not match ID, want\n %v\ngot:\n %v\n", id.String(), hash.String())
}
_, err = globalOptions.stdout.Write(buf)
return err
case "blob":
2023-10-01 19:38:09 +02:00
bar := newIndexProgress(gopts.Quiet, gopts.JSON)
err = repo.LoadIndex(ctx, bar)
if err != nil {
return err
}
2016-09-01 16:04:29 +02:00
for _, t := range []restic.BlobType{restic.DataBlob, restic.TreeBlob} {
2020-11-05 22:18:00 +01:00
bh := restic.BlobHandle{ID: id, Type: t}
if !repo.Index().Has(bh) {
2016-08-28 21:23:46 +02:00
continue
}
buf, err := repo.LoadBlob(ctx, t, id, nil)
2016-08-28 21:23:46 +02:00
if err != nil {
return err
}
_, err = globalOptions.stdout.Write(buf)
return err
}
2016-09-01 22:17:37 +02:00
return errors.Fatal("blob not found")
case "tree":
sn, subfolder, err := restic.FindSnapshot(ctx, repo, repo, args[1])
if err != nil {
return errors.Fatalf("could not find snapshot: %v\n", err)
}
2023-10-01 19:38:09 +02:00
bar := newIndexProgress(gopts.Quiet, gopts.JSON)
err = repo.LoadIndex(ctx, bar)
if err != nil {
return err
}
sn.Tree, err = restic.FindTreeDirectory(ctx, repo, sn.Tree, subfolder)
if err != nil {
return err
}
buf, err := repo.LoadBlob(ctx, restic.TreeBlob, *sn.Tree, nil)
if err != nil {
return err
}
_, err = globalOptions.stdout.Write(buf)
return err
2014-10-05 14:44:59 +02:00
default:
2016-09-01 22:17:37 +02:00
return errors.Fatal("invalid type")
2014-10-05 14:44:59 +02:00
}
}