2014-08-01 22:20:28 +02:00
|
|
|
package main
|
|
|
|
|
2016-09-01 22:17:37 +02:00
|
|
|
import (
|
2021-10-31 23:08:13 +01:00
|
|
|
"context"
|
|
|
|
|
2017-07-23 14:21:03 +02:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2022-06-12 14:43:43 +02:00
|
|
|
"github.com/restic/restic/internal/index"
|
2017-07-24 17:42:25 +02:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2016-09-17 12:36:05 +02:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2016-09-01 22:17:37 +02:00
|
|
|
)
|
2014-08-01 22:20:28 +02:00
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
var cmdList = &cobra.Command{
|
2020-08-27 03:29:43 +00:00
|
|
|
Use: "list [flags] [blobs|packs|index|snapshots|keys|locks]",
|
2017-09-11 09:32:44 -07:00
|
|
|
Short: "List objects in the repository",
|
2016-09-17 12:36:05 +02:00
|
|
|
Long: `
|
2017-02-13 16:06:27 +01:00
|
|
|
The "list" command allows listing objects in the repository based on type.
|
2019-11-04 22:03:38 -08:00
|
|
|
|
|
|
|
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
|
|
|
`,
|
2017-08-06 21:02:16 +02:00
|
|
|
DisableAutoGenTag: true,
|
2016-09-17 12:36:05 +02:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2024-01-21 17:43:13 +01:00
|
|
|
return runList(cmd.Context(), globalOptions, args)
|
2016-09-17 12:36:05 +02:00
|
|
|
},
|
2014-11-30 22:39:58 +01:00
|
|
|
}
|
|
|
|
|
2016-09-17 12:36:05 +02:00
|
|
|
func init() {
|
|
|
|
cmdRoot.AddCommand(cmdList)
|
2014-12-07 16:30:52 +01:00
|
|
|
}
|
|
|
|
|
2024-01-21 17:43:13 +01:00
|
|
|
func runList(ctx context.Context, gopts GlobalOptions, args []string) error {
|
2014-10-05 14:44:59 +02:00
|
|
|
if len(args) != 1 {
|
2024-01-21 17:43:13 +01:00
|
|
|
return errors.Fatal("type not specified")
|
2014-12-07 16:30:52 +01:00
|
|
|
}
|
|
|
|
|
2024-02-24 15:19:02 +01:00
|
|
|
ctx, repo, unlock, err := openWithReadLock(ctx, gopts, gopts.NoLock || args[0] == "locks")
|
2014-12-07 16:30:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-10-05 14:44:59 +02:00
|
|
|
}
|
2024-02-24 15:19:02 +01:00
|
|
|
defer unlock()
|
2015-06-27 14:40:18 +02:00
|
|
|
|
2016-09-01 16:04:29 +02:00
|
|
|
var t restic.FileType
|
2014-10-05 14:44:59 +02:00
|
|
|
switch args[0] {
|
2015-04-26 17:44:38 +02:00
|
|
|
case "packs":
|
2020-08-16 11:16:38 +02:00
|
|
|
t = restic.PackFile
|
2015-04-26 17:44:38 +02:00
|
|
|
case "index":
|
2016-09-01 16:04:29 +02:00
|
|
|
t = restic.IndexFile
|
2014-10-05 14:44:59 +02:00
|
|
|
case "snapshots":
|
2016-09-01 16:04:29 +02:00
|
|
|
t = restic.SnapshotFile
|
2014-10-05 14:44:59 +02:00
|
|
|
case "keys":
|
2016-09-01 16:04:29 +02:00
|
|
|
t = restic.KeyFile
|
2014-10-05 14:44:59 +02:00
|
|
|
case "locks":
|
2016-09-01 16:04:29 +02:00
|
|
|
t = restic.LockFile
|
2017-01-30 10:28:17 +01:00
|
|
|
case "blobs":
|
2024-02-10 22:58:10 +01:00
|
|
|
return index.ForAllIndexes(ctx, repo, repo, func(_ restic.ID, idx *index.Index, _ bool, err error) error {
|
2020-06-14 09:50:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-01-30 10:28:17 +01:00
|
|
|
}
|
2021-10-31 23:08:13 +01:00
|
|
|
idx.Each(ctx, func(blobs restic.PackedBlob) {
|
2020-06-14 09:50:11 +02:00
|
|
|
Printf("%v %v\n", blobs.Type, blobs.ID)
|
2022-08-19 20:04:39 +02:00
|
|
|
})
|
2020-06-14 09:50:11 +02:00
|
|
|
return nil
|
|
|
|
})
|
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
|
|
|
}
|
2014-08-01 22:20:28 +02:00
|
|
|
|
2024-02-10 22:58:10 +01:00
|
|
|
return repo.List(ctx, t, func(id restic.ID, _ int64) error {
|
2016-09-17 12:36:05 +02:00
|
|
|
Printf("%s\n", id)
|
2018-01-21 17:25:36 +01:00
|
|
|
return nil
|
|
|
|
})
|
2014-08-01 22:20:28 +02:00
|
|
|
}
|