From ed1ecfd27872d1131fb33375629ec85c5c154613 Mon Sep 17 00:00:00 2001 From: Seednode Date: Sat, 24 Sep 2022 17:17:56 -0500 Subject: [PATCH] Added argument to filter displayed results --- README.md | 11 ++++++----- cmd/files.go | 36 +++++++++++++++++++++++++++++++++--- cmd/root.go | 2 ++ cmd/version.go | 2 +- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 3c1bead..7468a69 100644 --- a/README.md +++ b/README.md @@ -36,11 +36,12 @@ Available Commands: version Print version Flags: - -h, --help help for roulette - -p, --port int port to listen on (default 8080) - -r, --recursive recurse into subdirectories - -s, --successive load the next sequential file, if possible - -v, --verbose log accessed files to stdout + -f, --filter string only display images matching specified pattern (case-insensitive) + -h, --help help for roulette + -p, --port int port to listen on (default 8080) + -r, --recursive recurse into subdirectories + -s, --successive load the next sequential file, if possible + -v, --verbose log accessed files to stdout Use "roulette [command] --help" for more information about a command. ``` \ No newline at end of file diff --git a/cmd/files.go b/cmd/files.go index b5a3fe4..a2494d4 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -12,11 +12,19 @@ import ( "path/filepath" "regexp" "strconv" + "strings" "time" "github.com/h2non/filetype" ) +func containsCaseIntensitive(a string, b string) bool { + return strings.Contains( + strings.ToLower(a), + strings.ToLower(b), + ) +} + func getFirstFile(path string) (string, error) { re := regexp.MustCompile(`(.+)([0-9]{3})(\..+)`) @@ -107,9 +115,19 @@ func getFiles(path string) ([]string, error) { var paths []string err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error { - if info.IsDir() && p != path { + switch { + case info.IsDir() && p != path: return filepath.SkipDir - } else { + case Filter != "": + absolutePath, err := filepath.Abs(p) + if err != nil { + return err + } + + if containsCaseIntensitive(p, Filter) { + paths = append(paths, absolutePath) + } + default: absolutePath, err := filepath.Abs(p) if err != nil { return err @@ -130,13 +148,25 @@ func getFilesRecursive(path string) ([]string, error) { var paths []string err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error { - if !info.IsDir() { + switch { + case Filter != "" && !info.IsDir(): absolutePath, err := filepath.Abs(p) if err != nil { return err } + + if containsCaseIntensitive(p, Filter) { + paths = append(paths, absolutePath) + } + case Filter == "" && !info.IsDir(): + absolutePath, err := filepath.Abs(p) + if err != nil { + return err + } + paths = append(paths, absolutePath) } + return err }) if err != nil { diff --git a/cmd/root.go b/cmd/root.go index 24a6b5a..e297a16 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -10,6 +10,7 @@ import ( "github.com/spf13/cobra" ) +var Filter string var Port int var Recursive bool var Successive bool @@ -32,6 +33,7 @@ func Execute() { } func init() { + rootCmd.Flags().StringVarP(&Filter, "filter", "f", "", "only display images matching specified pattern (case-insensitive)") rootCmd.Flags().IntVarP(&Port, "port", "p", 8080, "port to listen on") rootCmd.Flags().BoolVarP(&Recursive, "recursive", "r", false, "recurse into subdirectories") rootCmd.Flags().BoolVarP(&Successive, "successive", "s", false, "load the next sequential file, if possible") diff --git a/cmd/version.go b/cmd/version.go index bec870a..25d8805 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.8.3" +var Version = "0.9.0" func init() { rootCmd.AddCommand(versionCmd)