Added argument to filter displayed results

This commit is contained in:
Seednode 2022-09-24 17:17:56 -05:00
parent b2ad413844
commit ed1ecfd278
4 changed files with 42 additions and 9 deletions

View File

@ -36,11 +36,12 @@ Available Commands:
version Print version version Print version
Flags: Flags:
-h, --help help for roulette -f, --filter string only display images matching specified pattern (case-insensitive)
-p, --port int port to listen on (default 8080) -h, --help help for roulette
-r, --recursive recurse into subdirectories -p, --port int port to listen on (default 8080)
-s, --successive load the next sequential file, if possible -r, --recursive recurse into subdirectories
-v, --verbose log accessed files to stdout -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. Use "roulette [command] --help" for more information about a command.
``` ```

View File

@ -12,11 +12,19 @@ import (
"path/filepath" "path/filepath"
"regexp" "regexp"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/h2non/filetype" "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) { func getFirstFile(path string) (string, error) {
re := regexp.MustCompile(`(.+)([0-9]{3})(\..+)`) re := regexp.MustCompile(`(.+)([0-9]{3})(\..+)`)
@ -107,9 +115,19 @@ func getFiles(path string) ([]string, error) {
var paths []string var paths []string
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error { 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 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) absolutePath, err := filepath.Abs(p)
if err != nil { if err != nil {
return err return err
@ -130,13 +148,25 @@ func getFilesRecursive(path string) ([]string, error) {
var paths []string var paths []string
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error { 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) absolutePath, err := filepath.Abs(p)
if err != nil { if err != nil {
return err 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) paths = append(paths, absolutePath)
} }
return err return err
}) })
if err != nil { if err != nil {

View File

@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var Filter string
var Port int var Port int
var Recursive bool var Recursive bool
var Successive bool var Successive bool
@ -32,6 +33,7 @@ func Execute() {
} }
func init() { 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().IntVarP(&Port, "port", "p", 8080, "port to listen on")
rootCmd.Flags().BoolVarP(&Recursive, "recursive", "r", false, "recurse into subdirectories") rootCmd.Flags().BoolVarP(&Recursive, "recursive", "r", false, "recurse into subdirectories")
rootCmd.Flags().BoolVarP(&Successive, "successive", "s", false, "load the next sequential file, if possible") rootCmd.Flags().BoolVarP(&Successive, "successive", "s", false, "load the next sequential file, if possible")

View File

@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var Version = "0.8.3" var Version = "0.9.0"
func init() { func init() {
rootCmd.AddCommand(versionCmd) rootCmd.AddCommand(versionCmd)