From 4679208eae8c6c7241ce38da5645f545ab0d0911 Mon Sep 17 00:00:00 2001 From: Seednode Date: Wed, 31 May 2023 13:01:24 -0500 Subject: [PATCH] Replace fmt.Println() + os.Exit(1) with log.Fatal() in error handler, error out if no useful paths are passed in --- cmd/files.go | 4 ++-- cmd/root.go | 8 +++----- cmd/web.go | 6 ++---- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/cmd/files.go b/cmd/files.go index dec51a4..de0fe72 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -649,7 +649,7 @@ func normalizePath(path string) (string, error) { } func normalizePaths(args []string) ([]string, error) { - paths := make([]string, len(args)) + var paths []string var pathList strings.Builder pathList.WriteString("Paths:\n") @@ -683,7 +683,7 @@ func normalizePaths(args []string) ([]string, error) { } if addPath { - paths[i] = path + paths = append(paths, path) } } diff --git a/cmd/root.go b/cmd/root.go index 3b8f928..d57a4ea 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,8 +5,7 @@ Copyright © 2023 Seednode package cmd import ( - "fmt" - "os" + "log" "github.com/spf13/cobra" ) @@ -48,8 +47,7 @@ var ( func Execute() { err := rootCmd.Execute() if err != nil { - fmt.Println(err) - os.Exit(1) + log.Fatal(err) } } @@ -76,5 +74,5 @@ func init() { }) rootCmd.SetVersionTemplate("roulette v{{.Version}}\n") - rootCmd.Version = "0.48.0" + rootCmd.Version = "0.49.0" } diff --git a/cmd/web.go b/cmd/web.go index fb66b2a..ce44c07 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -943,8 +943,7 @@ func ServePage(args []string) error { bindAddr := net.ParseIP(bindHost[0]) if bindAddr == nil { - fmt.Println("Invalid bind address provided. Please specify a hostname, or an IPv4 or IPv6 address in dotted decimal or IPv6 format.") - os.Exit(1) + return errors.New("invalid bind address provided") } paths, err := normalizePaths(args) @@ -953,8 +952,7 @@ func ServePage(args []string) error { } if len(paths) == 0 { - fmt.Println("No supported files found in provided paths. Exiting.") - os.Exit(0) + return errors.New("no supported files found in provided paths") } Regexes := &Regexes{