2
2
mirror of https://github.com/octoleo/restic.git synced 2024-06-17 00:02:49 +00:00
restic/cmd/restic/cmd_ls.go

96 lines
2.0 KiB
Go
Raw Normal View History

2014-10-05 12:44:59 +00:00
package main
import (
"fmt"
"os"
"path/filepath"
2014-12-05 20:45:49 +00:00
"github.com/restic/restic"
"github.com/restic/restic/backend"
"github.com/restic/restic/repository"
2014-10-05 12:44:59 +00:00
)
type CmdLs struct {
global *GlobalOptions
}
2014-12-07 15:30:52 +00:00
2014-11-30 21:39:58 +00:00
func init() {
2014-12-07 15:30:52 +00:00
_, err := parser.AddCommand("ls",
"list files",
"The ls command lists all files and directories in a snapshot",
&CmdLs{global: &globalOpts})
2014-12-07 15:30:52 +00:00
if err != nil {
panic(err)
}
2014-11-30 21:39:58 +00:00
}
2015-04-24 23:39:32 +00:00
func printNode(prefix string, n *restic.Node) string {
2014-10-05 12:44:59 +00:00
switch n.Type {
case "file":
return fmt.Sprintf("%s %5d %5d %6d %s %s",
n.Mode, n.UID, n.GID, n.Size, n.ModTime, filepath.Join(prefix, n.Name))
case "dir":
return fmt.Sprintf("%s %5d %5d %6d %s %s",
n.Mode|os.ModeDir, n.UID, n.GID, n.Size, n.ModTime, filepath.Join(prefix, n.Name))
case "symlink":
return fmt.Sprintf("%s %5d %5d %6d %s %s -> %s",
n.Mode|os.ModeSymlink, n.UID, n.GID, n.Size, n.ModTime, filepath.Join(prefix, n.Name), n.LinkTarget)
default:
return fmt.Sprintf("<Node(%s) %s>", n.Type, n.Name)
}
}
2015-05-09 21:59:58 +00:00
func printTree(prefix string, repo *repository.Repository, id backend.ID) error {
2015-05-09 11:32:52 +00:00
tree, err := restic.LoadTree(repo, id)
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
for _, entry := range tree.Nodes {
2015-04-24 23:39:32 +00:00
fmt.Println(printNode(prefix, entry))
2014-10-05 12:44:59 +00:00
if entry.Type == "dir" && entry.Subtree != nil {
2015-05-09 11:32:52 +00:00
err = printTree(filepath.Join(prefix, entry.Name), repo, entry.Subtree)
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
}
}
return nil
}
2014-12-07 15:30:52 +00:00
func (cmd CmdLs) Usage() string {
return "snapshot-ID [DIR]"
2014-12-07 15:30:52 +00:00
}
func (cmd CmdLs) Execute(args []string) error {
2014-10-05 12:44:59 +00:00
if len(args) < 1 || len(args) > 2 {
2014-12-07 15:30:52 +00:00
return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage())
}
repo, err := cmd.global.OpenRepository()
2014-12-07 15:30:52 +00:00
if err != nil {
return err
2014-10-05 12:44:59 +00:00
}
err = repo.LoadIndex()
if err != nil {
return err
}
id, err := restic.FindSnapshot(repo, args[0])
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
sn, err := restic.LoadSnapshot(repo, id)
2014-10-05 12:44:59 +00:00
if err != nil {
return err
}
2015-03-02 13:48:47 +00:00
fmt.Printf("snapshot of %v at %s:\n", sn.Paths, sn.Time)
2014-10-05 12:44:59 +00:00
return printTree("", repo, sn.Tree)
2014-10-05 12:44:59 +00:00
}