on main help sign in
chore: pass config to app
5f16af4Kujtim Hoxha
cmd/root.go+1−1
@@ -91,7 +91,7 @@ to assist developers in writing, debugging, and understanding code directly from
91 return err
91 return err
92 }
92 }
93
93
94− app, err := app.New(ctx, conn)
94+ app, err := app.New(ctx, conn, cfg)
95 if err != nil {
95 if err != nil {
96 slog.Error(fmt.Sprintf("Failed to create app instance: %v", err))
96 slog.Error(fmt.Sprintf("Failed to create app instance: %v", err))
97 return err
97 return err
internal/app/app.go+4−3
@@ -37,22 +37,23 @@ type App struct {
37 watcherCancelFuncs []context.CancelFunc
37 watcherCancelFuncs []context.CancelFunc
38 cancelFuncsMutex sync.Mutex
38 cancelFuncsMutex sync.Mutex
39 watcherWG sync.WaitGroup
39 watcherWG sync.WaitGroup
40+
41+ config *config.Config
40}
42}
41
43
42−func New(ctx context.Context, conn *sql.DB) (*App, error) {
44+func New(ctx context.Context, conn *sql.DB, cfg *config.Config) (*App, error) {
43 q := db.New(conn)
45 q := db.New(conn)
44 sessions := session.NewService(q)
46 sessions := session.NewService(q)
45 messages := message.NewService(q)
47 messages := message.NewService(q)
46 files := history.NewService(q, conn)
48 files := history.NewService(q, conn)
47
49
48− cfg := config.Get()
49−
50 app := &App{
50 app := &App{
51 Sessions: sessions,
51 Sessions: sessions,
52 Messages: messages,
52 Messages: messages,
53 History: files,
53 History: files,
54 Permissions: permission.NewPermissionService(cfg.WorkingDir()),
54 Permissions: permission.NewPermissionService(cfg.WorkingDir()),
55 LSPClients: make(map[string]*lsp.Client),
55 LSPClients: make(map[string]*lsp.Client),
56+ config: cfg,
56 }
57 }
57
58
58 // Initialize LSP clients in the background
59 // Initialize LSP clients in the background
internal/app/lsp.go+4−8
@@ -5,17 +5,14 @@ import (
5 "log/slog"
5 "log/slog"
6 "time"
6 "time"
7
7
8− "github.com/charmbracelet/crush/internal/config"
9 "github.com/charmbracelet/crush/internal/log"
8 "github.com/charmbracelet/crush/internal/log"
10 "github.com/charmbracelet/crush/internal/lsp"
9 "github.com/charmbracelet/crush/internal/lsp"
11 "github.com/charmbracelet/crush/internal/lsp/watcher"
10 "github.com/charmbracelet/crush/internal/lsp/watcher"
12)
11)
13
12
14func (app *App) initLSPClients(ctx context.Context) {
13func (app *App) initLSPClients(ctx context.Context) {
15− cfg := config.Get()
16−
17 // Initialize LSP clients
14 // Initialize LSP clients
18− for name, clientConfig := range cfg.LSP {
15+ for name, clientConfig := range app.config.LSP {
19 // Start each client initialization in its own goroutine
16 // Start each client initialization in its own goroutine
20 go app.createAndStartLSPClient(ctx, name, clientConfig.Command, clientConfig.Args...)
17 go app.createAndStartLSPClient(ctx, name, clientConfig.Command, clientConfig.Args...)
21 }
18 }
@@ -39,7 +36,7 @@ func (app *App) createAndStartLSPClient(ctx context.Context, name string, comman
39 defer cancel()
36 defer cancel()
40
37
41 // Initialize with the initialization context
38 // Initialize with the initialization context
42− _, err = lspClient.InitializeLSPClient(initCtx, config.Get().WorkingDir())
39+ _, err = lspClient.InitializeLSPClient(initCtx, app.config.WorkingDir())
43 if err != nil {
40 if err != nil {
44 slog.Error("Initialize failed", "name", name, "error", err)
41 slog.Error("Initialize failed", "name", name, "error", err)
45 // Clean up the client to prevent resource leaks
42 // Clean up the client to prevent resource leaks
@@ -92,15 +89,14 @@ func (app *App) runWorkspaceWatcher(ctx context.Context, name string, workspaceW
92 app.restartLSPClient(ctx, name)
89 app.restartLSPClient(ctx, name)
93 })
90 })
94
91
95− workspaceWatcher.WatchWorkspace(ctx, config.Get().WorkingDir())
92+ workspaceWatcher.WatchWorkspace(ctx, app.config.WorkingDir())
96 slog.Info("Workspace watcher stopped", "client", name)
93 slog.Info("Workspace watcher stopped", "client", name)
97}
94}
98
95
99// restartLSPClient attempts to restart a crashed or failed LSP client
96// restartLSPClient attempts to restart a crashed or failed LSP client
100func (app *App) restartLSPClient(ctx context.Context, name string) {
97func (app *App) restartLSPClient(ctx context.Context, name string) {
101 // Get the original configuration
98 // Get the original configuration
102− cfg := config.Get()
103− clientConfig, exists := cfg.LSP[name]
99+ clientConfig, exists := app.config.LSP[name]
104 if !exists {
100 if !exists {
105 slog.Error("Cannot restart client, configuration not found", "client", name)
101 slog.Error("Cannot restart client, configuration not found", "client", name)
106 return
102 return