From da5bdfe765c7e84db4ae5e1e41fb25746732b76e Mon Sep 17 00:00:00 2001 From: Seednode Date: Tue, 24 Jan 2023 10:13:09 -0600 Subject: [PATCH] Replaced %v with more specific format string in printf calls --- cmd/files.go | 12 ++++++------ cmd/root.go | 6 ++++-- cmd/version.go | 2 +- cmd/web.go | 49 +++++++++++++++++++++++-------------------------- 4 files changed, 34 insertions(+), 35 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index 7567a75..749b777 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -147,7 +147,7 @@ func imageDimensions(path string) (*Dimensions, error) { func preparePath(path string) string { if runtime.GOOS == "windows" { - path = fmt.Sprintf("/%v", filepath.ToSlash(path)) + path = fmt.Sprintf("/%s", filepath.ToSlash(path)) } return path @@ -320,7 +320,7 @@ func tryExtensions(p *Path) (string, error) { var fileName string for _, extension := range extensions { - fileName = fmt.Sprintf("%v%.3d%v", p.base, p.number, extension) + fileName = fmt.Sprintf("%s%.3d%s", p.base, p.number, extension) exists, err := fileExists(fileName) if err != nil { @@ -358,7 +358,7 @@ func pathIsValid(filePath string, paths []string) bool { switch { case Verbose && !matchesPrefix: - fmt.Printf("%v | Error: Failed to serve file outside specified path(s): %v\n", + fmt.Printf("%s | Error: Failed to serve file outside specified path(s): %s\n", time.Now().Format(logDate), filePath, ) @@ -478,7 +478,7 @@ func fileList(paths []string, filters *Filters, sort string, index *Index) ([]st fileList = prepareDirectories(files, sort) if Verbose { - fmt.Printf("%v | Indexed %v/%v files across %v directories in %v\n", + fmt.Printf("%s | Indexed %d/%d files across %d directories in %s\n", time.Now().Format(logDate), stats.FilesMatched(), stats.FilesTotal(), @@ -591,9 +591,9 @@ func normalizePaths(args []string) ([]string, error) { } if (args[i]) != absolutePath { - fmt.Printf("%v (resolved to %v)\n", args[i], absolutePath) + fmt.Printf("%s (resolved to %s)\n", args[i], absolutePath) } else { - fmt.Printf("%v\n", args[i]) + fmt.Printf("%s\n", args[i]) } paths = append(paths, absolutePath) diff --git a/cmd/root.go b/cmd/root.go index 8fa71d4..2c9166b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,7 +5,7 @@ Copyright © 2023 Seednode package cmd import ( - "log" + "fmt" "os" "github.com/spf13/cobra" @@ -39,7 +39,8 @@ var rootCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { err := ServePage(args) if err != nil { - log.Fatal(err) + fmt.Println(err) + os.Exit(1) } }, } @@ -47,6 +48,7 @@ var rootCmd = &cobra.Command{ func Execute() { err := rootCmd.Execute() if err != nil { + fmt.Println(err) os.Exit(1) } } diff --git a/cmd/version.go b/cmd/version.go index 096d8d1..5eb1301 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.34.0" +var Version = "0.34.1" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index f97e523..7bf33b9 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -8,7 +8,6 @@ import ( "encoding/json" "fmt" "io" - "log" "math/rand" "net/http" "net/url" @@ -160,7 +159,7 @@ func notFound(w http.ResponseWriter, r *http.Request, filePath string) error { startTime := time.Now() if Verbose { - fmt.Printf("%v | Unavailable file %v requested by %v\n", + fmt.Printf("%s | Unavailable file %s requested by %s\n", startTime.Format(logDate), filePath, r.RemoteAddr, @@ -196,9 +195,7 @@ func refreshInterval(r *http.Request, regexes *Regexes) (int64, string) { return 0, "0ms" } - durationInMs := duration.Milliseconds() - - return durationInMs, refreshInterval + return duration.Milliseconds(), refreshInterval } func sortOrder(r *http.Request) string { @@ -254,7 +251,7 @@ func generateQueryParams(filters *Filters, sortOrder, refreshInterval string) st queryParams.WriteString("&") } - queryParams.WriteString(fmt.Sprintf("sort=%v", sortOrder)) + queryParams.WriteString(fmt.Sprintf("sort=%s", sortOrder)) hasParams = true } @@ -262,7 +259,7 @@ func generateQueryParams(filters *Filters, sortOrder, refreshInterval string) st if hasParams { queryParams.WriteString("&") } - queryParams.WriteString(fmt.Sprintf("refresh=%v", refreshInterval)) + queryParams.WriteString(fmt.Sprintf("refresh=%s", refreshInterval)) return queryParams.String() } @@ -319,13 +316,13 @@ func realIP(r *http.Request) string { remotePort := remoteAddr[len(remoteAddr)-1] cfIP := r.Header.Get("Cf-Connecting-Ip") - xRealIp := r.Header.Get("X-Real-Ip") + xRealIP := r.Header.Get("X-Real-Ip") switch { case cfIP != "": return cfIP + ":" + remotePort - case xRealIp != "": - return xRealIp + ":" + remotePort + case xRealIP != "": + return xRealIP + ":" + remotePort default: return r.RemoteAddr } @@ -348,17 +345,17 @@ func html(w http.ResponseWriter, r *http.Request, filePath string, dimensions *D htmlBody.WriteString(`a{display:block;height:100%;width:100%;text-decoration:none;}`) htmlBody.WriteString(`img{margin:auto;display:block;max-width:97%;max-height:97%;object-fit:scale-down;`) htmlBody.WriteString(`position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);}`) - htmlBody.WriteString(fmt.Sprintf(`%v (%vx%v)`, + htmlBody.WriteString(fmt.Sprintf(`%s (%dx%d)`, fileName, dimensions.width, dimensions.height)) htmlBody.WriteString(``) if refreshInterval != "0ms" { - htmlBody.WriteString(fmt.Sprintf("", + htmlBody.WriteString(fmt.Sprintf("", queryParams, refreshTimer)) } - htmlBody.WriteString(fmt.Sprintf(`Roulette selected: %v`, + htmlBody.WriteString(fmt.Sprintf(`Roulette selected: %s`, queryParams, generateFilePath(filePath), dimensions.width, @@ -414,7 +411,7 @@ func staticFile(w http.ResponseWriter, r *http.Request, paths []string, stats *S fileSize := humanReadableSize(len(buf)) if Verbose { - fmt.Printf("%v | Served %v (%v) to %v in %v\n", + fmt.Printf("%s | Served %s (%s) to %s in %s\n", startTime.Format(logDate), filePath, fileSize, @@ -447,13 +444,13 @@ func statsHandler(args []string, stats *ServeStats) http.HandlerFunc { response, err := stats.ListImages() if err != nil { - log.Fatal(err) + fmt.Println(err) } w.Write(response) if Verbose { - fmt.Printf("%v | Served statistics page (%v) to %v in %v\n", + fmt.Printf("%s | Served statistics page (%s) to %s in %s\n", startTime.Format(logDate), humanReadableSize(len(response)), realIP(r), @@ -467,7 +464,7 @@ func staticFileHandler(paths []string, stats *ServeStats) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { err := staticFile(w, r, paths, stats) if err != nil { - log.Fatal(err) + fmt.Println(err) } } } @@ -476,7 +473,7 @@ func htmlHandler(paths []string, regexes *Regexes, index *Index) http.HandlerFun return func(w http.ResponseWriter, r *http.Request) { refererUri, err := stripQueryParams(refererToUri(r.Referer())) if err != nil { - log.Fatal(err) + fmt.Println(err) } filters := &Filters{ @@ -495,7 +492,7 @@ func htmlHandler(paths []string, regexes *Regexes, index *Index) http.HandlerFun if refererUri != "" { filePath, err = nextFile(refererUri, sortOrder, regexes) if err != nil { - log.Fatal(err) + fmt.Println(err) } } @@ -507,13 +504,13 @@ func htmlHandler(paths []string, regexes *Regexes, index *Index) http.HandlerFun return case err != nil: - log.Fatal(err) + fmt.Println(err) } } queryParams := generateQueryParams(filters, sortOrder, refreshInterval) - newUrl := fmt.Sprintf("http://%v%v%v", + newUrl := fmt.Sprintf("http://%s%s%s", r.Host, preparePath(filePath), queryParams, @@ -528,7 +525,7 @@ func htmlHandler(paths []string, regexes *Regexes, index *Index) http.HandlerFun exists, err := fileExists(filePath) if err != nil { - log.Fatal(err) + fmt.Println(err) } if !exists { notFound(w, r, filePath) @@ -538,7 +535,7 @@ func htmlHandler(paths []string, regexes *Regexes, index *Index) http.HandlerFun image, err := isImage(filePath) if err != nil { - log.Fatal(err) + fmt.Println(err) } if !image { notFound(w, r, filePath) @@ -548,12 +545,12 @@ func htmlHandler(paths []string, regexes *Regexes, index *Index) http.HandlerFun dimensions, err := imageDimensions(filePath) if err != nil { - log.Fatal(err) + fmt.Println(err) } err = html(w, r, filePath, dimensions, filters, regexes) if err != nil { - log.Fatal(err) + fmt.Println(err) } } } @@ -562,7 +559,7 @@ func htmlHandler(paths []string, regexes *Regexes, index *Index) http.HandlerFun func doNothing(http.ResponseWriter, *http.Request) {} func ServePage(args []string) error { - fmt.Printf("roulette v%v\n\n", Version) + fmt.Printf("roulette v%s\n\n", Version) paths, err := normalizePaths(args) if err != nil {