2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-04 12:34:13 +00:00

Merge pull request #864 from restic/find-case-insensitive

find: Add option to ignore case
This commit is contained in:
Alexander Neumann 2017-03-07 11:20:33 +01:00
commit 340f2c80a0

View File

@ -2,6 +2,7 @@ package main
import ( import (
"path/filepath" "path/filepath"
"strings"
"time" "time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -25,9 +26,10 @@ repo. `,
// FindOptions bundle all options for the find command. // FindOptions bundle all options for the find command.
type FindOptions struct { type FindOptions struct {
Oldest string Oldest string
Newest string Newest string
Snapshot string Snapshot string
CaseInsensitive bool
} }
var findOptions FindOptions var findOptions FindOptions
@ -39,11 +41,13 @@ func init() {
f.StringVarP(&findOptions.Oldest, "oldest", "o", "", "oldest modification date/time") f.StringVarP(&findOptions.Oldest, "oldest", "o", "", "oldest modification date/time")
f.StringVarP(&findOptions.Newest, "newest", "n", "", "newest modification date/time") f.StringVarP(&findOptions.Newest, "newest", "n", "", "newest modification date/time")
f.StringVarP(&findOptions.Snapshot, "snapshot", "s", "", "snapshot ID to search in") f.StringVarP(&findOptions.Snapshot, "snapshot", "s", "", "snapshot ID to search in")
f.BoolVarP(&findOptions.CaseInsensitive, "ignore-case", "i", false, "ignore case for pattern")
} }
type findPattern struct { type findPattern struct {
oldest, newest time.Time oldest, newest time.Time
pattern string pattern string
ignoreCase bool
} }
type findResult struct { type findResult struct {
@ -86,7 +90,12 @@ func findInTree(repo *repository.Repository, pat findPattern, id restic.ID, path
for _, node := range tree.Nodes { for _, node := range tree.Nodes {
debug.Log(" testing entry %q\n", node.Name) debug.Log(" testing entry %q\n", node.Name)
m, err := filepath.Match(pat.pattern, node.Name) name := node.Name
if pat.ignoreCase {
name = strings.ToLower(name)
}
m, err := filepath.Match(pat.pattern, name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -190,6 +199,11 @@ func runFind(opts FindOptions, gopts GlobalOptions, args []string) error {
pat.pattern = args[0] pat.pattern = args[0]
if opts.CaseInsensitive {
pat.pattern = strings.ToLower(pat.pattern)
pat.ignoreCase = true
}
if opts.Snapshot != "" { if opts.Snapshot != "" {
snapshotID, err := restic.FindSnapshot(repo, opts.Snapshot) snapshotID, err := restic.FindSnapshot(repo, opts.Snapshot)
if err != nil { if err != nil {