chore: pass config to app

Kujtim Hoxha created

Change summary

cmd/root.go         |  2 +-
internal/app/app.go |  7 ++++---
internal/app/lsp.go | 12 ++++--------
3 files changed, 9 insertions(+), 12 deletions(-)

Detailed changes

cmd/root.go 🔗

@@ -91,7 +91,7 @@ to assist developers in writing, debugging, and understanding code directly from
 			return err
 		}
 
-		app, err := app.New(ctx, conn)
+		app, err := app.New(ctx, conn, cfg)
 		if err != nil {
 			slog.Error(fmt.Sprintf("Failed to create app instance: %v", err))
 			return err

internal/app/app.go 🔗

@@ -37,22 +37,23 @@ type App struct {
 	watcherCancelFuncs []context.CancelFunc
 	cancelFuncsMutex   sync.Mutex
 	watcherWG          sync.WaitGroup
+
+	config *config.Config
 }
 
-func New(ctx context.Context, conn *sql.DB) (*App, error) {
+func New(ctx context.Context, conn *sql.DB, cfg *config.Config) (*App, error) {
 	q := db.New(conn)
 	sessions := session.NewService(q)
 	messages := message.NewService(q)
 	files := history.NewService(q, conn)
 
-	cfg := config.Get()
-
 	app := &App{
 		Sessions:    sessions,
 		Messages:    messages,
 		History:     files,
 		Permissions: permission.NewPermissionService(cfg.WorkingDir()),
 		LSPClients:  make(map[string]*lsp.Client),
+		config:      cfg,
 	}
 
 	// Initialize LSP clients in the background

internal/app/lsp.go 🔗

@@ -5,17 +5,14 @@ import (
 	"log/slog"
 	"time"
 
-	"github.com/charmbracelet/crush/internal/config"
 	"github.com/charmbracelet/crush/internal/log"
 	"github.com/charmbracelet/crush/internal/lsp"
 	"github.com/charmbracelet/crush/internal/lsp/watcher"
 )
 
 func (app *App) initLSPClients(ctx context.Context) {
-	cfg := config.Get()
-
 	// Initialize LSP clients
-	for name, clientConfig := range cfg.LSP {
+	for name, clientConfig := range app.config.LSP {
 		// Start each client initialization in its own goroutine
 		go app.createAndStartLSPClient(ctx, name, clientConfig.Command, clientConfig.Args...)
 	}
@@ -39,7 +36,7 @@ func (app *App) createAndStartLSPClient(ctx context.Context, name string, comman
 	defer cancel()
 
 	// Initialize with the initialization context
-	_, err = lspClient.InitializeLSPClient(initCtx, config.Get().WorkingDir())
+	_, err = lspClient.InitializeLSPClient(initCtx, app.config.WorkingDir())
 	if err != nil {
 		slog.Error("Initialize failed", "name", name, "error", err)
 		// Clean up the client to prevent resource leaks
@@ -92,15 +89,14 @@ func (app *App) runWorkspaceWatcher(ctx context.Context, name string, workspaceW
 		app.restartLSPClient(ctx, name)
 	})
 
-	workspaceWatcher.WatchWorkspace(ctx, config.Get().WorkingDir())
+	workspaceWatcher.WatchWorkspace(ctx, app.config.WorkingDir())
 	slog.Info("Workspace watcher stopped", "client", name)
 }
 
 // restartLSPClient attempts to restart a crashed or failed LSP client
 func (app *App) restartLSPClient(ctx context.Context, name string) {
 	// Get the original configuration
-	cfg := config.Get()
-	clientConfig, exists := cfg.LSP[name]
+	clientConfig, exists := app.config.LSP[name]
 	if !exists {
 		slog.Error("Cannot restart client, configuration not found", "client", name)
 		return