-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (104 loc) · 2.48 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"context"
"fmt"
"io/fs"
"os"
"os/signal"
"time"
"github.com/jxsl13/twstatus-bot/bot"
"github.com/jxsl13/twstatus-bot/config"
"github.com/jxsl13/twstatus-bot/db"
"github.com/jxsl13/twstatus-bot/migrations"
"github.com/spf13/cobra"
)
func main() {
err := NewRootCmd(migrations.FS).Execute()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func NewRootCmd(migrationsFs fs.FS) *cobra.Command {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
rootContext := rootContext{
Ctx: ctx,
MigrationsFS: migrationsFs,
}
// cmd represents the run command
cmd := &cobra.Command{
Use: "twstatus-bot",
Short: "twstatus-bot is a server status bot for Discord",
RunE: rootContext.RunE,
Args: cobra.ExactArgs(0),
PostRunE: func(cmd *cobra.Command, args []string) error {
rootContext.DB.Close()
cancel()
return nil
},
}
// register flags but defer parsing and validation of the final values
cmd.PreRunE = rootContext.PreRunE(cmd)
// register flags but defer parsing and validation of the final values
cmd.AddCommand(NewCompletionCmd(cmd.Name()))
return cmd
}
type rootContext struct {
Ctx context.Context
MigrationsFS fs.FS
// set in PreRunE
Config *config.Config
DB *db.DB
}
func (c *rootContext) PreRunE(cmd *cobra.Command) func(cmd *cobra.Command, args []string) error {
c.Config = &config.Config{
PostgresHostname: "postgres",
PostgresPort: 5432,
PostgresSSLMode: db.SSLModeDisable,
PostgresDatabase: "twdb",
PollInterval: 16 * time.Second,
}
runParser := config.RegisterFlags(c.Config, true, cmd)
return func(cmd *cobra.Command, args []string) error {
err := runParser()
if err != nil {
return err
}
db, err := db.New(
c.Ctx,
c.Config.PostgresHostname,
c.Config.PostgresPort,
c.Config.PostgresDatabase,
c.Config.PostgresUser,
c.Config.PostgresPassword,
db.WithMigrationsFs(c.MigrationsFS),
db.WithSSL(c.Config.PostgresSSLMode),
)
if err != nil {
return fmt.Errorf("failed to initialize database: %w", err)
}
c.DB = db
return nil
}
}
func (c *rootContext) RunE(cmd *cobra.Command, args []string) error {
b, err := bot.New(
c.Ctx,
c.Config.DiscordToken,
c.DB,
c.Config.SuperAdmins,
c.Config.GuildID,
c.Config.ChannelID,
c.Config.PollInterval,
c.Config.LegacyMessageFormat,
)
if err != nil {
return err
}
defer b.Close()
err = b.Connect(c.Ctx)
if err != nil {
return err
}
return nil
}