From f9d7be008d624e2f76a04a3d16574cc49c791bbd Mon Sep 17 00:00:00 2001 From: Seednode Date: Fri, 11 Nov 2022 09:44:47 -0600 Subject: [PATCH] Combined declare/initialize steps for structs --- cmd/files.go | 22 ++++++++++++++-------- cmd/version.go | 2 +- cmd/web.go | 31 +++++++++++++------------------ 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index a9f79fb..e93da6f 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -485,17 +485,23 @@ func pickFile(args []string, filters *Filters, sort string, fileCache *[]string) if Cache && len(*fileCache) != 0 { fileList = *fileCache } else { - files := Files{} - files.List = make(map[string][]string) + files := &Files{ + List: make(map[string][]string), + } - stats := Stats{} + stats := &Stats{ + FilesMatched: 0, + FilesSkipped: 0, + DirectoriesMatched: 0, + } - concurrency := Concurrency{} - concurrency.DirectoryScans = make(chan int, maxDirectoryScans) - concurrency.FileScans = make(chan int, maxFileScans) + concurrency := &Concurrency{ + DirectoryScans: make(chan int, maxDirectoryScans), + FileScans: make(chan int, maxFileScans), + } startTime := time.Now() - getFileList(args, &files, filters, &stats, &concurrency) + getFileList(args, files, filters, stats, concurrency) runTime := time.Since(startTime) 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 { *fileCache = append(*fileCache, fileList...) diff --git a/cmd/version.go b/cmd/version.go index f5c13ed..249ed73 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.26.1" +var Version = "0.26.2" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index 4020732..7cbf051 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -321,9 +321,10 @@ func serveHtmlHandler(paths []string, regexes *Regexes, fileCache *[]string) htt log.Fatal(err) } - filters := Filters{} - filters.Includes = splitQueryParams(r.URL.Query().Get("include"), regexes) - filters.Excludes = splitQueryParams(r.URL.Query().Get("exclude"), regexes) + filters := &Filters{ + Includes: splitQueryParams(r.URL.Query().Get("include"), regexes), + Excludes: splitQueryParams(r.URL.Query().Get("exclude"), regexes), + } sortOrder := getSortOrder(r) @@ -341,7 +342,7 @@ func serveHtmlHandler(paths []string, regexes *Regexes, fileCache *[]string) htt } if filePath == "" { - filePath, err = getNewFile(paths, &filters, sortOrder, regexes, fileCache) + filePath, err = getNewFile(paths, filters, sortOrder, regexes, fileCache) switch { case err != nil && err == ErrNoImagesFound: 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", r.Host, @@ -392,7 +393,7 @@ func serveHtmlHandler(paths []string, regexes *Regexes, fileCache *[]string) htt log.Fatal(err) } - err = serveHtml(w, r, filePath, dimensions, &filters) + err = serveHtml(w, r, filePath, dimensions, filters) if err != nil { log.Fatal(err) } @@ -402,15 +403,6 @@ func serveHtmlHandler(paths []string, regexes *Regexes, fileCache *[]string) htt 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 { fmt.Printf("roulette v%v\n\n", Version) @@ -419,13 +411,16 @@ func ServePage(args []string) error { return err } - regexes := generateRegularExpressions() + regexes := &Regexes{ + Filename: regexp.MustCompile(`(.+)([0-9]{3})(\..+)`), + Alphanumeric: regexp.MustCompile(`^[a-zA-Z0-9]*$`), + } 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.HandleFunc("/favicon.ico", doNothing)