-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
90 lines (74 loc) · 2.53 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"context"
"io"
"os"
"time"
"golang.org/x/exp/slog"
"github.com/alecthomas/kong"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type Context struct {
Debug bool
Logger *slog.Logger
gorm.Config
gorm.Dialector
}
var cli struct {
LogSQL bool `help:"Log SQL queries."`
DSN string `help:"data source name" default:"pub:pub@tcp(localhost:3306)/pub"`
AutoMigrate AutoMigrateCmd `cmd:"" help:"Automigrate the database."`
CreateAccount CreateAccountCmd `cmd:"" help:"Create a new account."`
CreateInstance CreateInstanceCmd `cmd:"" help:"Create a new instance."`
DeleteAccount DeleteAccountCmd `cmd:"" help:"Delete an account."`
FetchActor FetchActorCmd `cmd:"" help:"Fetch an actor."`
HouseKeeping HouseKeepingCmd `cmd:"" help:"Perform housekeeping."`
Serve ServeCmd `cmd:"" help:"Serve a local web server."`
ShowActor ShowActorCmd `cmd:"" help:"Display an actor."`
SynchroniseFollowers SynchroniseFollowersCmd `cmd:"" help:"Synchronise followers."`
RerunObjectHooks RerunObjectHooksCmd `cmd:"" help:"Rerun object hooks."`
Follow FollowCmd `cmd:"" help:"Follow an object."`
}
func main() {
ctx := kong.Parse(&cli)
log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{}))
err := ctx.Run(&Context{
Debug: cli.LogSQL,
Logger: log,
Config: gorm.Config{
Logger: &slogGORMLogger{slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: func() slog.Level {
if cli.LogSQL {
return slog.LevelInfo
}
return slog.LevelWarn
}(),
}))},
},
Dialector: newDialector(cli.DSN),
})
ctx.FatalIfErrorf(err)
}
type slogHandler struct {
out io.Writer
}
type slogGORMLogger struct {
*slog.Logger
}
func (s *slogGORMLogger) LogMode(level logger.LogLevel) logger.Interface {
return s
}
func (s *slogGORMLogger) Info(ctx context.Context, msg string, data ...interface{}) {
s.InfoContext(ctx, msg, data...)
}
func (s *slogGORMLogger) Warn(ctx context.Context, msg string, data ...interface{}) {
s.WarnContext(ctx, msg, data...)
}
func (s *slogGORMLogger) Error(ctx context.Context, msg string, data ...interface{}) {
s.ErrorContext(ctx, msg, data...)
}
func (s *slogGORMLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
sql, rowsAffected := fc()
s.InfoContext(ctx, "pub/sql.trace", "sql", sql, "rowsAffected", rowsAffected, "err", err)
}