Added argument to filter displayed results
This commit is contained in:
parent
b2ad413844
commit
ed1ecfd278
|
@ -36,6 +36,7 @@ Available Commands:
|
||||||
version Print version
|
version Print version
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
|
-f, --filter string only display images matching specified pattern (case-insensitive)
|
||||||
-h, --help help for roulette
|
-h, --help help for roulette
|
||||||
-p, --port int port to listen on (default 8080)
|
-p, --port int port to listen on (default 8080)
|
||||||
-r, --recursive recurse into subdirectories
|
-r, --recursive recurse into subdirectories
|
||||||
|
|
36
cmd/files.go
36
cmd/files.go
|
@ -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)
|
paths = append(paths, absolutePath)
|
||||||
}
|
}
|
||||||
|
case Filter == "" && !info.IsDir():
|
||||||
|
absolutePath, err := filepath.Abs(p)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
paths = append(paths, absolutePath)
|
||||||
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -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")
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue