github.com/fawick/restic@v0.1.1-0.20171126184616-c02923fbfc79/cmd/restic/cmd_list.go (about)

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