Merged -c|--count flag into -v|--verbose, reformatted verbose output

This commit is contained in:
Seednode 2022-10-20 17:55:25 -05:00
parent e39950de68
commit ae82762745
5 changed files with 33 additions and 17 deletions

View File

@ -60,7 +60,6 @@ Available Commands:
version Print version
Flags:
-c, --count display number of files/directories matched/skipped
-f, --filter enable filtering via query parameters
-h, --help help for roulette
-p, --port uint16 port to listen on (default 8080)

View File

@ -35,6 +35,10 @@ type Stats struct {
DirectoriesMatched uint64
}
func (s *Stats) GetFilesTotal() uint64 {
return atomic.LoadUint64(&s.FilesMatched) + atomic.LoadUint64(&s.FilesSkipped)
}
func (s *Stats) IncrementFilesMatched() {
atomic.AddUint64(&s.FilesMatched, 1)
}
@ -283,10 +287,12 @@ func pathIsValid(filePath string, paths []string) bool {
matchesPrefix = true
}
}
if !matchesPrefix {
if Verbose {
fmt.Printf("%v Failed to serve file outside specified path(s): %v\n", time.Now().Format(LOGDATE), filePath)
}
if Verbose && !matchesPrefix {
fmt.Printf("%v | Error: Failed to serve file outside specified path(s): %v\n",
time.Now().Format(LOGDATE),
filePath,
)
return false
}
@ -428,10 +434,11 @@ func pickFile(args []string, filters *Filters, sort string) (string, error) {
getFileList(args, &files, filters, &stats, &concurrency)
runTime := time.Since(startTime)
if Count {
fmt.Printf("Scanned %v files (skipped %v) across %v directories in %v\n",
if Verbose {
fmt.Printf("%v | Scanned %v/%v files across %v directories in %v\n",
time.Now().Format(LOGDATE),
stats.GetFilesMatched(),
stats.GetFilesSkipped(),
stats.GetFilesTotal(),
stats.GetDirectoriesMatched(),
runTime,
)

View File

@ -24,7 +24,6 @@ type Concurrency struct {
FileScans chan int
}
var Count bool
var Filter bool
var Port uint16
var Recursive bool
@ -52,7 +51,6 @@ 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")

View File

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

View File

@ -195,11 +195,7 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err
return nil
}
var startTime time.Time
if Verbose {
startTime = time.Now()
fmt.Printf("%v Serving file: %v", startTime.Format(LOGDATE), filePath)
}
startTime := time.Now()
buf, err := os.ReadFile(filePath)
if err != nil {
@ -209,7 +205,12 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err
w.Write(buf)
if Verbose {
fmt.Printf(" (Finished in %v)\n", time.Since(startTime).Round(time.Microsecond))
fmt.Printf("%v | Served \"%v\" to %v in %v\n",
startTime.Format(LOGDATE),
filePath,
r.RemoteAddr,
time.Since(startTime).Round(time.Microsecond),
)
}
return nil
@ -406,6 +407,17 @@ func serveHtmlHandler(paths []string) appHandler {
default:
filePath := r.URL.Path
exists, err := fileExists(filePath)
if err != nil {
return err
}
if !exists {
http.NotFound(w, r)
return nil
}
image, err := isImage(filePath)
if err != nil {
return err