Merge pull request #113 from charmbracelet/log-warn

Kujtim Hoxha created

Remove Ripgrep and FZF warnings from stderr

Change summary

internal/config/load.go    |  8 ++------
internal/fsext/fileutil.go |  9 +++++++--
internal/log/log.go        | 13 +++++++++++--
3 files changed, 20 insertions(+), 10 deletions(-)

Detailed changes

internal/config/load.go 🔗

@@ -52,16 +52,12 @@ func Load(workingDir string, debug bool) (*Config, error) {
 		cfg.Options.Debug = true
 	}
 
-	// Init logs
-	log.Init(
+	// Setup logs
+	log.Setup(
 		filepath.Join(cfg.Options.DataDirectory, "logs", fmt.Sprintf("%s.log", appName)),
 		cfg.Options.Debug,
 	)
 
-	if err != nil {
-		return nil, fmt.Errorf("failed to load config: %w", err)
-	}
-
 	// Load known providers, this loads the config from fur
 	providers, err := LoadProviders(client.New())
 	if err != nil || len(providers) == 0 {

internal/fsext/fileutil.go 🔗

@@ -12,6 +12,7 @@ import (
 
 	"github.com/bmatcuk/doublestar/v4"
 	"github.com/charlievieth/fastwalk"
+	"github.com/charmbracelet/crush/internal/log"
 
 	ignore "github.com/sabhiram/go-gitignore"
 )
@@ -25,11 +26,15 @@ func init() {
 	var err error
 	rgPath, err = exec.LookPath("rg")
 	if err != nil {
-		slog.Warn("Ripgrep (rg) not found in $PATH. Some features might be limited or slower.")
+		if log.Initialized() {
+			slog.Warn("Ripgrep (rg) not found in $PATH. Some features might be limited or slower.")
+		}
 	}
 	fzfPath, err = exec.LookPath("fzf")
 	if err != nil {
-		slog.Warn("FZF not found in $PATH. Some features might be limited or slower.")
+		if log.Initialized() {
+			slog.Warn("FZF not found in $PATH. Some features might be limited or slower.")
+		}
 	}
 }
 

internal/log/log.go 🔗

@@ -6,14 +6,18 @@ import (
 	"os"
 	"runtime/debug"
 	"sync"
+	"sync/atomic"
 	"time"
 
 	"gopkg.in/natefinch/lumberjack.v2"
 )
 
-var initOnce sync.Once
+var (
+	initOnce    sync.Once
+	initialized atomic.Bool
+)
 
-func Init(logFile string, debug bool) {
+func Setup(logFile string, debug bool) {
 	initOnce.Do(func() {
 		logRotator := &lumberjack.Logger{
 			Filename:   logFile,
@@ -34,9 +38,14 @@ func Init(logFile string, debug bool) {
 		})
 
 		slog.SetDefault(slog.New(logger))
+		initialized.Store(true)
 	})
 }
 
+func Initialized() bool {
+	return initialized.Load()
+}
+
 func RecoverPanic(name string, cleanup func()) {
 	if r := recover(); r != nil {
 		// Create a timestamped panic log file