2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-24 20:00:21 +00:00

Merge pull request #723 from ulziibuyan/ls-latest-cmd

Added latest keyword in ls command.
This commit is contained in:
Alexander Neumann 2017-01-15 12:40:58 +01:00
commit 094e80f4a4

View File

@ -17,22 +17,35 @@ var cmdLs = &cobra.Command{
Short: "list files in a snapshot", Short: "list files in a snapshot",
Long: ` Long: `
The "ls" command allows listing files and directories in a snapshot. 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 { RunE: func(cmd *cobra.Command, args []string) error {
return runLs(globalOptions, args) return runLs(globalOptions, args)
}, },
} }
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() { func init() {
cmdRoot.AddCommand(cmdLs) 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 { func printNode(prefix string, n *restic.Node) string {
if !listLong { if !lsOptions.ListLong {
return filepath.Join(prefix, n.Name) return filepath.Join(prefix, n.Name)
} }
@ -86,9 +99,19 @@ func runLs(gopts GlobalOptions, args []string) error {
return err return err
} }
id, err := restic.FindSnapshot(repo, args[0]) snapshotIDString := args[0]
if err != nil { var id restic.ID
return err
if snapshotIDString == "latest" {
id, err = restic.FindLatestSnapshot(repo, lsOptions.Paths, lsOptions.Host)
if err != nil {
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 {
Exitf(1, "invalid id %q: %v", snapshotIDString, err)
}
} }
sn, err := restic.LoadSnapshot(repo, id) sn, err := restic.LoadSnapshot(repo, id)