Added -c|--count flag to display number of files and directories matched and skipped
This commit is contained in:
parent
31d16f65b8
commit
9cc372d7c4
55
cmd/files.go
55
cmd/files.go
|
@ -18,6 +18,13 @@ import (
|
|||
"github.com/h2non/filetype"
|
||||
)
|
||||
|
||||
type Stats struct {
|
||||
FilesMatched uint64
|
||||
FilesSkipped uint64
|
||||
DirectoriesMatched uint64
|
||||
DirectoriesSkipped uint64
|
||||
}
|
||||
|
||||
type Path struct {
|
||||
Base string
|
||||
Number int
|
||||
|
@ -36,7 +43,7 @@ var (
|
|||
ErrNoImagesFound = fmt.Errorf("no supported image formats found")
|
||||
)
|
||||
|
||||
func appendPaths(m map[string][]string, path string, filters *Filters) (map[string][]string, error) {
|
||||
func appendPaths(m map[string][]string, path string, filters *Filters, stats *Stats) (map[string][]string, error) {
|
||||
absolutePath, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -54,11 +61,14 @@ func appendPaths(m map[string][]string, path string, filters *Filters) (map[stri
|
|||
filters.Includes[i],
|
||||
) {
|
||||
m[directory] = append(m[directory], path)
|
||||
stats.FilesMatched += 1
|
||||
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
||||
stats.FilesSkipped += 1
|
||||
|
||||
return m, nil
|
||||
case !filters.HasIncludes() && filters.HasExcludes():
|
||||
for i := 0; i < len(filters.Excludes); i++ {
|
||||
|
@ -66,11 +76,14 @@ func appendPaths(m map[string][]string, path string, filters *Filters) (map[stri
|
|||
filename,
|
||||
filters.Excludes[i],
|
||||
) {
|
||||
stats.FilesSkipped += 1
|
||||
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
||||
m[directory] = append(m[directory], path)
|
||||
stats.FilesMatched += 1
|
||||
|
||||
return m, nil
|
||||
case filters.HasIncludes() && filters.HasExcludes():
|
||||
|
@ -79,6 +92,8 @@ func appendPaths(m map[string][]string, path string, filters *Filters) (map[stri
|
|||
filename,
|
||||
filters.Excludes[i],
|
||||
) {
|
||||
stats.FilesSkipped += 1
|
||||
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
@ -89,14 +104,18 @@ func appendPaths(m map[string][]string, path string, filters *Filters) (map[stri
|
|||
filters.Includes[i],
|
||||
) {
|
||||
m[directory] = append(m[directory], path)
|
||||
stats.FilesMatched += 1
|
||||
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
||||
stats.FilesSkipped += 1
|
||||
|
||||
return m, nil
|
||||
default:
|
||||
m[directory] = append(m[directory], path)
|
||||
stats.FilesMatched += 1
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
@ -252,18 +271,20 @@ func isImage(path string) (bool, error) {
|
|||
return filetype.IsImage(head), nil
|
||||
}
|
||||
|
||||
func getFiles(m map[string][]string, path string, filters *Filters) (map[string][]string, error) {
|
||||
func getFiles(m map[string][]string, path string, filters *Filters, stats *Stats) (map[string][]string, error) {
|
||||
err := filepath.WalkDir(path, func(p string, info os.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if Filter && filters.HasExcludes() {
|
||||
if info.IsDir() && Filter && filters.HasExcludes() {
|
||||
for i := 0; i < len(filters.Excludes); i++ {
|
||||
if strings.Contains(
|
||||
strings.ToLower(p),
|
||||
strings.ToLower(filters.Excludes[i]),
|
||||
) {
|
||||
stats.DirectoriesSkipped += 1
|
||||
|
||||
return filepath.SkipDir
|
||||
}
|
||||
}
|
||||
|
@ -271,17 +292,16 @@ func getFiles(m map[string][]string, path string, filters *Filters) (map[string]
|
|||
|
||||
switch {
|
||||
case !Recursive && info.IsDir() && p != path:
|
||||
stats.DirectoriesSkipped += 1
|
||||
|
||||
return filepath.SkipDir
|
||||
case !filters.IsEmpty() && !info.IsDir():
|
||||
m, err = appendPaths(m, p, filters)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case !info.IsDir():
|
||||
m, err = appendPaths(m, p, filters)
|
||||
m, err = appendPaths(m, p, filters, stats)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case info.IsDir():
|
||||
stats.DirectoriesMatched += 1
|
||||
}
|
||||
|
||||
return err
|
||||
|
@ -293,12 +313,12 @@ func getFiles(m map[string][]string, path string, filters *Filters) (map[string]
|
|||
return m, nil
|
||||
}
|
||||
|
||||
func getFileList(paths []string, filters *Filters) (map[string][]string, error) {
|
||||
func getFileList(paths []string, filters *Filters, stats *Stats) (map[string][]string, error) {
|
||||
fileMap := map[string][]string{}
|
||||
var err error
|
||||
|
||||
for i := 0; i < len(paths); i++ {
|
||||
fileMap, err = getFiles(fileMap, paths[i], filters)
|
||||
fileMap, err = getFiles(fileMap, paths[i], filters, stats)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -350,11 +370,22 @@ func prepareDirectories(m map[string][]string, sort string) []string {
|
|||
}
|
||||
|
||||
func pickFile(args []string, filters *Filters, sort string) (string, error) {
|
||||
fileMap, err := getFileList(args, filters)
|
||||
stats := Stats{}
|
||||
|
||||
fileMap, err := getFileList(args, filters, &stats)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if Count {
|
||||
fmt.Printf("Choosing from %v files (skipped %v) out of %v directories (skipped %v)\n",
|
||||
stats.FilesMatched,
|
||||
stats.FilesSkipped,
|
||||
stats.DirectoriesMatched,
|
||||
stats.DirectoriesSkipped,
|
||||
)
|
||||
}
|
||||
|
||||
fileList := prepareDirectories(fileMap, sort)
|
||||
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var Count bool
|
||||
var Filter bool
|
||||
var Port uint16
|
||||
var Recursive bool
|
||||
|
@ -38,6 +39,7 @@ func Execute() {
|
|||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.Flags().BoolVarP(&Count, "count", "c", false, "display number of files/directories matched/skipped")
|
||||
rootCmd.Flags().BoolVarP(&Filter, "filter", "f", false, "enable filtering via query parameters")
|
||||
rootCmd.Flags().Uint16VarP(&Port, "port", "p", 8080, "port to listen on")
|
||||
rootCmd.Flags().BoolVarP(&Recursive, "recursive", "r", false, "recurse into subdirectories")
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var Version = "0.15.2"
|
||||
var Version = "0.16.0"
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(versionCmd)
|
||||
|
|
Loading…
Reference in New Issue