Combined declare/initialize steps for structs

This commit is contained in:
Seednode 2022-11-11 09:44:47 -06:00
parent d3c063017d
commit f9d7be008d
3 changed files with 28 additions and 27 deletions

View File

@ -485,17 +485,23 @@ func pickFile(args []string, filters *Filters, sort string, fileCache *[]string)
if Cache && len(*fileCache) != 0 { if Cache && len(*fileCache) != 0 {
fileList = *fileCache fileList = *fileCache
} else { } else {
files := Files{} files := &Files{
files.List = make(map[string][]string) List: make(map[string][]string),
}
stats := Stats{} stats := &Stats{
FilesMatched: 0,
FilesSkipped: 0,
DirectoriesMatched: 0,
}
concurrency := Concurrency{} concurrency := &Concurrency{
concurrency.DirectoryScans = make(chan int, maxDirectoryScans) DirectoryScans: make(chan int, maxDirectoryScans),
concurrency.FileScans = make(chan int, maxFileScans) FileScans: make(chan int, maxFileScans),
}
startTime := time.Now() startTime := time.Now()
getFileList(args, &files, filters, &stats, &concurrency) getFileList(args, files, filters, stats, concurrency)
runTime := time.Since(startTime) runTime := time.Since(startTime)
if Verbose { if Verbose {
@ -508,7 +514,7 @@ func pickFile(args []string, filters *Filters, sort string, fileCache *[]string)
) )
} }
fileList = prepareDirectories(&files, sort) fileList = prepareDirectories(files, sort)
if Cache { if Cache {
*fileCache = append(*fileCache, fileList...) *fileCache = append(*fileCache, fileList...)

View File

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

View File

@ -321,9 +321,10 @@ func serveHtmlHandler(paths []string, regexes *Regexes, fileCache *[]string) htt
log.Fatal(err) log.Fatal(err)
} }
filters := Filters{} filters := &Filters{
filters.Includes = splitQueryParams(r.URL.Query().Get("include"), regexes) Includes: splitQueryParams(r.URL.Query().Get("include"), regexes),
filters.Excludes = splitQueryParams(r.URL.Query().Get("exclude"), regexes) Excludes: splitQueryParams(r.URL.Query().Get("exclude"), regexes),
}
sortOrder := getSortOrder(r) sortOrder := getSortOrder(r)
@ -341,7 +342,7 @@ func serveHtmlHandler(paths []string, regexes *Regexes, fileCache *[]string) htt
} }
if filePath == "" { if filePath == "" {
filePath, err = getNewFile(paths, &filters, sortOrder, regexes, fileCache) filePath, err = getNewFile(paths, filters, sortOrder, regexes, fileCache)
switch { switch {
case err != nil && err == ErrNoImagesFound: case err != nil && err == ErrNoImagesFound:
notFound(w, r, filePath) notFound(w, r, filePath)
@ -352,7 +353,7 @@ func serveHtmlHandler(paths []string, regexes *Regexes, fileCache *[]string) htt
} }
} }
queryParams := generateQueryParams(&filters, sortOrder, refreshInterval) queryParams := generateQueryParams(filters, sortOrder, refreshInterval)
newUrl := fmt.Sprintf("http://%v%v%v", newUrl := fmt.Sprintf("http://%v%v%v",
r.Host, r.Host,
@ -392,7 +393,7 @@ func serveHtmlHandler(paths []string, regexes *Regexes, fileCache *[]string) htt
log.Fatal(err) log.Fatal(err)
} }
err = serveHtml(w, r, filePath, dimensions, &filters) err = serveHtml(w, r, filePath, dimensions, filters)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -402,15 +403,6 @@ func serveHtmlHandler(paths []string, regexes *Regexes, fileCache *[]string) htt
func doNothing(http.ResponseWriter, *http.Request) {} func doNothing(http.ResponseWriter, *http.Request) {}
func generateRegularExpressions() *Regexes {
r := Regexes{}
r.Filename = regexp.MustCompile(`(.+)([0-9]{3})(\..+)`)
r.Alphanumeric = regexp.MustCompile(`^[a-zA-Z0-9]*$`)
return &r
}
func ServePage(args []string) error { func ServePage(args []string) error {
fmt.Printf("roulette v%v\n\n", Version) fmt.Printf("roulette v%v\n\n", Version)
@ -419,13 +411,16 @@ func ServePage(args []string) error {
return err return err
} }
regexes := generateRegularExpressions() regexes := &Regexes{
Filename: regexp.MustCompile(`(.+)([0-9]{3})(\..+)`),
Alphanumeric: regexp.MustCompile(`^[a-zA-Z0-9]*$`),
}
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
fileCache := []string{} fileCache := &[]string{}
http.Handle("/", serveHtmlHandler(paths, regexes, &fileCache)) http.Handle("/", serveHtmlHandler(paths, regexes, fileCache))
http.Handle(Prefix+"/", http.StripPrefix(Prefix, serveStaticFileHandler(paths))) http.Handle(Prefix+"/", http.StripPrefix(Prefix, serveStaticFileHandler(paths)))
http.HandleFunc("/favicon.ico", doNothing) http.HandleFunc("/favicon.ico", doNothing)