restic/cmd/restic/cmd_cat.go

192 lines
3.8 KiB
Go
Raw Normal View History

2014-10-05 12:44:59 +00:00
package main
import (
2017-06-04 09:16:55 +00:00
"context"
2014-10-05 12:44:59 +00:00
"encoding/json"
"fmt"
"os"
2016-09-17 10:36:05 +00:00
"github.com/spf13/cobra"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/repository"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
2014-10-05 12:44:59 +00:00
)
2016-09-17 10:36:05 +00:00
var cmdCat = &cobra.Command{
Use: "cat [flags] [pack|blob|snapshot|index|key|masterkey|config|lock] ID",
Short: "Print internal objects to stdout",
2016-09-17 10:36:05 +00:00
Long: `
The "cat" command is used to print internal objects to stdout.
`,
DisableAutoGenTag: true,
2016-09-17 10:36:05 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
return runCat(globalOptions, args)
},
}
2014-12-07 15:30:52 +00:00
2014-11-30 21:39:58 +00:00
func init() {
2016-09-17 10:36:05 +00:00
cmdRoot.AddCommand(cmdCat)
2014-11-30 21:39:58 +00:00
}
2016-09-17 10:36:05 +00:00
func runCat(gopts GlobalOptions, args []string) error {
2015-05-03 15:37:21 +00:00
if len(args) < 1 || (args[0] != "masterkey" && args[0] != "config" && len(args) != 2) {
return errors.Fatal("type or ID not specified")
2014-12-07 15:30:52 +00:00
}
2016-09-17 10:36:05 +00:00
repo, err := OpenRepository(gopts)
2014-12-07 15:30:52 +00:00
if err != nil {
return err
2014-10-05 12:44:59 +00:00
}
lock, err := lockRepo(repo)
defer unlockRepo(lock)
2015-06-27 12:40:18 +00:00
if err != nil {
return err
}
2014-10-05 12:44:59 +00:00
tpe := args[0]
2016-09-01 14:04:29 +00:00
var id restic.ID
2015-05-03 15:37:21 +00:00
if tpe != "masterkey" && tpe != "config" {
2016-09-01 14:04:29 +00:00
id, err = restic.ParseID(args[1])
if err != nil {
if tpe != "snapshot" {
2016-09-01 20:17:37 +00:00
return errors.Fatalf("unable to parse ID: %v\n", err)
}
// find snapshot id with prefix
id, err = restic.FindSnapshot(repo, args[1])
if err != nil {
return err
}
}
2014-10-05 12:44:59 +00:00
}
// handle all types that don't need an index
2014-10-05 12:44:59 +00:00
switch tpe {
2015-05-03 15:37:21 +00:00
case "config":
2016-09-01 14:04:29 +00:00
buf, err := json.MarshalIndent(repo.Config(), "", " ")
2015-05-03 15:37:21 +00:00
if err != nil {
return err
}
fmt.Println(string(buf))
return nil
case "index":
2017-06-04 09:16:55 +00:00
buf, err := repo.LoadAndDecrypt(context.TODO(), restic.IndexFile, id)
if err != nil {
return err
}
_, err = os.Stdout.Write(append(buf, '\n'))
return err
2014-10-05 12:44:59 +00:00
case "snapshot":
sn := &restic.Snapshot{}
2017-06-04 09:16:55 +00:00
err = repo.LoadJSONUnpacked(context.TODO(), restic.SnapshotFile, id, sn)
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
buf, err := json.MarshalIndent(&sn, "", " ")
if err != nil {
return err
}
fmt.Println(string(buf))
return nil
case "key":
2016-09-01 19:19:30 +00:00
h := restic.Handle{Type: restic.KeyFile, Name: id.String()}
2017-06-04 09:16:55 +00:00
buf, err := backend.LoadAll(context.TODO(), repo.Backend(), h)
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
2016-01-23 23:18:12 +00:00
key := &repository.Key{}
err = json.Unmarshal(buf, key)
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
2016-01-23 23:18:12 +00:00
buf, err = json.MarshalIndent(&key, "", " ")
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
fmt.Println(string(buf))
return nil
case "masterkey":
buf, err := json.MarshalIndent(repo.Key(), "", " ")
if err != nil {
return err
}
2014-10-05 12:44:59 +00:00
fmt.Println(string(buf))
2014-10-05 12:44:59 +00:00
return nil
case "lock":
2017-06-04 09:16:55 +00:00
lock, err := restic.LoadLock(context.TODO(), repo, id)
2015-06-24 16:17:01 +00:00
if err != nil {
return err
}
buf, err := json.MarshalIndent(&lock, "", " ")
if err != nil {
return err
}
fmt.Println(string(buf))
return nil
}
// load index, handle all the other types
2017-06-04 09:16:55 +00:00
err = repo.LoadIndex(context.TODO())
if err != nil {
return err
}
switch tpe {
case "pack":
2016-09-01 19:19:30 +00:00
h := restic.Handle{Type: restic.DataFile, Name: id.String()}
2017-06-04 09:16:55 +00:00
buf, err := backend.LoadAll(context.TODO(), repo.Backend(), h)
if err != nil {
return err
}
2016-09-01 14:04:29 +00:00
hash := restic.Hash(buf)
if !hash.Equal(id) {
2016-09-17 10:36:05 +00:00
fmt.Fprintf(stderr, "Warning: hash of data does not match ID, want\n %v\ngot:\n %v\n", id.String(), hash.String())
}
2016-01-23 23:18:12 +00:00
_, err = os.Stdout.Write(buf)
return err
case "blob":
2016-09-01 14:04:29 +00:00
for _, t := range []restic.BlobType{restic.DataBlob, restic.TreeBlob} {
2016-08-28 19:23:46 +00:00
list, err := repo.Index().Lookup(id, t)
if err != nil {
continue
}
blob := list[0]
2016-08-28 19:23:46 +00:00
buf := make([]byte, blob.Length)
2017-06-04 09:16:55 +00:00
n, err := repo.LoadBlob(context.TODO(), t, id, buf)
2016-08-28 19:23:46 +00:00
if err != nil {
return err
}
buf = buf[:n]
2016-08-28 19:23:46 +00:00
_, err = os.Stdout.Write(buf)
return err
}
2016-09-01 20:17:37 +00:00
return errors.Fatal("blob not found")
2014-10-05 12:44:59 +00:00
default:
2016-09-01 20:17:37 +00:00
return errors.Fatal("invalid type")
2014-10-05 12:44:59 +00:00
}
}