-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (44 loc) · 1.19 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"cmp"
"fmt"
"log/slog"
"os"
tea "github.com/charmbracelet/bubbletea"
"github.com/hrvadl/gowatchsql/internal/domain/engine"
"github.com/hrvadl/gowatchsql/internal/platform/cfg"
"github.com/hrvadl/gowatchsql/internal/platform/db"
"github.com/hrvadl/gowatchsql/internal/platform/logger"
"github.com/hrvadl/gowatchsql/internal/ui/models/welcome"
)
const (
configPathEnvVarName = "XDG_CONFIG_HOME"
homeEnvVarName = "HOME"
)
var basePath = cmp.Or(os.Getenv(configPathEnvVarName), os.Getenv(homeEnvVarName))
func main() {
f, err := tea.LogToFile("debug.log", "")
if err != nil {
panic(err)
}
l := logger.New(f)
cfg, err := cfg.NewFromFile(basePath)
if err != nil {
l.Error("Failed to load config", slog.Any("err", err))
os.Exit(1)
}
pool := db.NewPool(cfg)
defer func() {
if err := pool.Close(); err != nil {
l.Error("Failed to close the pool", slog.Any("err", err))
os.Exit(1)
}
}()
factory := engine.NewFactory(pool)
p := tea.NewProgram(welcome.NewModel(l, factory, cfg))
slog.SetLogLoggerLevel(slog.LevelDebug)
l.Info("Starting the program")
if _, err := p.Run(); err != nil {
panic(fmt.Sprintf("Alas, there's been an error: %v", err))
}
}