Added argument to filter displayed results
This commit is contained in:
parent
b2ad413844
commit
ed1ecfd278
11
README.md
11
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.
|
||||
```
|
36
cmd/files.go
36
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 {
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var Version = "0.8.3"
|
||||
var Version = "0.9.0"
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(versionCmd)
|
||||
|
|
Loading…
Reference in New Issue