github.com/advanderveer/restic@v0.8.1-0.20171209104529-42a8c19aaea6/cmd/restic/cmd_list.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/restic/restic/internal/errors"
     7  	"github.com/restic/restic/internal/index"
     8  	"github.com/restic/restic/internal/restic"
     9  
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  var cmdList = &cobra.Command{
    14  	Use:   "list [blobs|packs|index|snapshots|keys|locks]",
    15  	Short: "List objects in the repository",
    16  	Long: `
    17  The "list" command allows listing objects in the repository based on type.
    18  `,
    19  	DisableAutoGenTag: true,
    20  	RunE: func(cmd *cobra.Command, args []string) error {
    21  		return runList(globalOptions, args)
    22  	},
    23  }
    24  
    25  func init() {
    26  	cmdRoot.AddCommand(cmdList)
    27  }
    28  
    29  func runList(opts GlobalOptions, args []string) error {
    30  	if len(args) != 1 {
    31  		return errors.Fatal("type not specified")
    32  	}
    33  
    34  	repo, err := OpenRepository(opts)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	if !opts.NoLock {
    40  		lock, err := lockRepo(repo)
    41  		defer unlockRepo(lock)
    42  		if err != nil {
    43  			return err
    44  		}
    45  	}
    46  
    47  	var t restic.FileType
    48  	switch args[0] {
    49  	case "packs":
    50  		t = restic.DataFile
    51  	case "index":
    52  		t = restic.IndexFile
    53  	case "snapshots":
    54  		t = restic.SnapshotFile
    55  	case "keys":
    56  		t = restic.KeyFile
    57  	case "locks":
    58  		t = restic.LockFile
    59  	case "blobs":
    60  		idx, err := index.Load(opts.ctx, repo, nil)
    61  		if err != nil {
    62  			return err
    63  		}
    64  
    65  		for _, pack := range idx.Packs {
    66  			for _, entry := range pack.Entries {
    67  				fmt.Printf("%v %v\n", entry.Type, entry.ID)
    68  			}
    69  		}
    70  
    71  		return nil
    72  	default:
    73  		return errors.Fatal("invalid type")
    74  	}
    75  
    76  	for id := range repo.List(opts.ctx, t) {
    77  		Printf("%s\n", id)
    78  	}
    79  
    80  	return nil
    81  }