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,6 +36,7 @@ Available Commands:
version Print version
Flags:
-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

View File

@ -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 {

View File

@ -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")

View File

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