From 02c02283cf0f8785a26ddf507fcc0c457151cb97 Mon Sep 17 00:00:00 2001 From: Uzi Date: Thu, 12 Jan 2017 19:24:08 +0800 Subject: [PATCH 1/2] Added latest keyword in ls command. --- src/cmds/restic/cmd_ls.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/cmds/restic/cmd_ls.go b/src/cmds/restic/cmd_ls.go index 3b090543e..4f2bb5be2 100644 --- a/src/cmds/restic/cmd_ls.go +++ b/src/cmds/restic/cmd_ls.go @@ -17,6 +17,8 @@ var cmdLs = &cobra.Command{ Short: "list files in a snapshot", Long: ` The "ls" command allows listing files and directories in a snapshot. + +The special snapshot-ID "latest" can be used to list files and directories of the latest snapshot in the repository. `, RunE: func(cmd *cobra.Command, args []string) error { return runLs(globalOptions, args) @@ -86,9 +88,20 @@ func runLs(gopts GlobalOptions, args []string) error { return err } - id, err := restic.FindSnapshot(repo, args[0]) - if err != nil { - return err + snapshotIDString := args[0] + var id restic.ID + var paths []string + + if snapshotIDString == "latest" { + id, err = restic.FindLatestSnapshot(repo, paths, "") + if err != nil { + return err + } + } else { + id, err = restic.FindSnapshot(repo, snapshotIDString) + if err != nil { + return err + } } sn, err := restic.LoadSnapshot(repo, id) From c13a0953c8521e8476707abee6078c461c5947eb Mon Sep 17 00:00:00 2001 From: Uzi Date: Sat, 14 Jan 2017 11:19:47 +0800 Subject: [PATCH 2/2] User interface inconsistency fixed --- src/cmds/restic/cmd_ls.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/cmds/restic/cmd_ls.go b/src/cmds/restic/cmd_ls.go index 4f2bb5be2..bee9d1e55 100644 --- a/src/cmds/restic/cmd_ls.go +++ b/src/cmds/restic/cmd_ls.go @@ -25,16 +25,27 @@ The special snapshot-ID "latest" can be used to list files and directories of th }, } -var listLong bool +// LsOptions collects all options for the ls command. +type LsOptions struct { + ListLong bool + Host string + Paths []string +} + +var lsOptions LsOptions func init() { cmdRoot.AddCommand(cmdLs) - cmdLs.Flags().BoolVarP(&listLong, "long", "l", false, "use a long listing format showing size and mode") + flags := cmdLs.Flags() + flags.BoolVarP(&lsOptions.ListLong, "long", "l", false, "use a long listing format showing size and mode") + + flags.StringVarP(&lsOptions.Host, "host", "H", "", `only consider snapshots for this host when the snapshot ID is "latest"`) + flags.StringSliceVar(&lsOptions.Paths, "path", nil, "only consider snapshots which include this (absolute) `path` for snapshot ID \"latest\"") } func printNode(prefix string, n *restic.Node) string { - if !listLong { + if !lsOptions.ListLong { return filepath.Join(prefix, n.Name) } @@ -90,17 +101,16 @@ func runLs(gopts GlobalOptions, args []string) error { snapshotIDString := args[0] var id restic.ID - var paths []string if snapshotIDString == "latest" { - id, err = restic.FindLatestSnapshot(repo, paths, "") + id, err = restic.FindLatestSnapshot(repo, lsOptions.Paths, lsOptions.Host) if err != nil { - return err + Exitf(1, "latest snapshot for criteria not found: %v Paths:%v Host:%v", err, lsOptions.Paths, lsOptions.Host) } } else { id, err = restic.FindSnapshot(repo, snapshotIDString) if err != nil { - return err + Exitf(1, "invalid id %q: %v", snapshotIDString, err) } }