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 version Print version
Flags: Flags:
-c, --count display number of files/directories matched/skipped
-f, --filter enable filtering via query parameters -f, --filter enable filtering via query parameters
-h, --help help for roulette -h, --help help for roulette
-p, --port uint16 port to listen on (default 8080) -p, --port uint16 port to listen on (default 8080)

View File

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

View File

@ -24,7 +24,6 @@ type Concurrency struct {
FileScans chan int FileScans chan int
} }
var Count bool
var Filter bool var Filter bool
var Port uint16 var Port uint16
var Recursive bool var Recursive bool
@ -52,7 +51,6 @@ func Execute() {
} }
func init() { 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().BoolVarP(&Filter, "filter", "f", false, "enable filtering via query parameters")
rootCmd.Flags().Uint16VarP(&Port, "port", "p", 8080, "port to listen on") rootCmd.Flags().Uint16VarP(&Port, "port", "p", 8080, "port to listen on")
rootCmd.Flags().BoolVarP(&Recursive, "recursive", "r", false, "recurse into subdirectories") rootCmd.Flags().BoolVarP(&Recursive, "recursive", "r", false, "recurse into subdirectories")

View File

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

View File

@ -195,11 +195,7 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err
return nil return nil
} }
var startTime time.Time startTime := time.Now()
if Verbose {
startTime = time.Now()
fmt.Printf("%v Serving file: %v", startTime.Format(LOGDATE), filePath)
}
buf, err := os.ReadFile(filePath) buf, err := os.ReadFile(filePath)
if err != nil { if err != nil {
@ -209,7 +205,12 @@ func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) err
w.Write(buf) w.Write(buf)
if Verbose { 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 return nil
@ -406,6 +407,17 @@ func serveHtmlHandler(paths []string) appHandler {
default: default:
filePath := r.URL.Path 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) image, err := isImage(filePath)
if err != nil { if err != nil {
return err return err