Skip to content

Commit eb3a1c1

Browse files
authored
sqlc (#3)
* sqlc, stuff from netenvelope, go slog, user table
1 parent ee71948 commit eb3a1c1

24 files changed

+555
-60
lines changed

.env.dist

+4-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
DUNCE_TOKEN=
1+
DUNCE_DB_PATH=
2+
DUNCE_TOKEN=
3+
DUNCE_APP_ID=
4+
DUNCE_GUILD_ID=

bot/bot.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ package bot
22

33
import (
44
"fmt"
5+
"log/slog"
56
"os"
67
"os/signal"
78
"syscall"
89

910
"github.com/bwmarrin/discordgo"
10-
"github.com/joho/godotenv"
11-
"github.com/kelseyhightower/envconfig"
1211
"pkg.nit.so/switchboard"
1312

1413
"github.com/fogo-sh/dunce/database"
14+
"github.com/fogo-sh/dunce/database/queries"
1515
)
1616

1717
type Config struct {
@@ -24,19 +24,19 @@ type Config struct {
2424
var config Config
2525
var Bot *discordgo.Session
2626

27-
func Run() error {
28-
err := godotenv.Load()
29-
if err != nil {
30-
fmt.Printf("Failed to load .env file: %s\n", err.Error())
31-
}
27+
var db *queries.Queries
28+
29+
func Run(inputConfig Config) error {
30+
config = inputConfig
31+
32+
slog.Info("Initializing Dunce...")
3233

33-
err = envconfig.Process("dunce", &config)
34+
dbInstance, err := database.New(config.DBPath)
3435
if err != nil {
35-
return fmt.Errorf("error loading config: %s", err)
36+
return fmt.Errorf("error opening database: %w", err)
3637
}
3738

38-
database.Initialize(config.DBPath)
39-
database.Migrate()
39+
db = dbInstance
4040

4141
Bot, err = discordgo.New(fmt.Sprintf("Bot %s", config.Token))
4242
if err != nil {
@@ -56,11 +56,11 @@ func Run() error {
5656
return fmt.Errorf("error opening Discord connection: %w", err)
5757
}
5858

59-
fmt.Println("Dunce is now running. Press Ctrl-C to exit.")
59+
slog.Info("Dunce is now running. Press Ctrl-C to exit.")
6060
sc := make(chan os.Signal, 1)
6161
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
6262
<-sc
63-
fmt.Println("Quitting Dunce")
63+
slog.Info("Quitting Dunce")
6464

6565
if err = Bot.Close(); err != nil {
6666
return fmt.Errorf("error closing Discord connection: %w", err)

bot/test.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
package bot
22

33
import (
4+
"context"
5+
"fmt"
6+
"log/slog"
7+
48
"github.com/bwmarrin/discordgo"
59
)
610

711
func TestCommand(_ *discordgo.Session, interaction *discordgo.InteractionCreate, args struct{}) {
12+
users, err := db.GetAllUsers(context.Background())
13+
14+
if err != nil {
15+
slog.Error("Failed to get users", "error", err)
16+
Bot.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
17+
Type: discordgo.InteractionResponseChannelMessageWithSource,
18+
Data: &discordgo.InteractionResponseData{
19+
Content: "Failed to get users",
20+
},
21+
})
22+
return
23+
}
24+
25+
countOfUsers := len(users)
26+
827
Bot.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
928
Type: discordgo.InteractionResponseChannelMessageWithSource,
1029
Data: &discordgo.InteractionResponseData{
11-
Content: "test",
30+
Content: fmt.Sprintf("test, %d users", countOfUsers),
1231
},
1332
})
1433
}

cmd/db.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cmd
2+
3+
import "github.com/spf13/cobra"
4+
5+
var dbCmd = &cobra.Command{
6+
Use: "db",
7+
Aliases: []string{"database", "migrate"},
8+
Short: "Interact with the database state",
9+
}
10+
11+
func init() {
12+
rootCmd.AddCommand(dbCmd)
13+
}

cmd/db_downgrade.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"log/slog"
6+
7+
"github.com/golang-migrate/migrate/v4"
8+
"github.com/spf13/cobra"
9+
10+
"github.com/fogo-sh/dunce/database"
11+
)
12+
13+
var dbDowngradeCmd = &cobra.Command{
14+
Use: "downgrade",
15+
Aliases: []string{"down"},
16+
Short: "Perform all database downgrades",
17+
Args: cobra.NoArgs,
18+
Run: func(cmd *cobra.Command, args []string) {
19+
m, err := database.NewMigrateInstance(config.DBPath)
20+
checkError(err, "Error setting up migrations")
21+
22+
slog.Info("Performing database downgrades...")
23+
err = m.Down()
24+
if err != nil {
25+
if errors.Is(err, migrate.ErrNoChange) {
26+
slog.Info("Database is already up to date")
27+
return
28+
} else {
29+
slog.Error("Error upgrading database", err)
30+
}
31+
}
32+
33+
slog.Info("Database downgrades complete!")
34+
},
35+
}
36+
37+
func init() {
38+
dbCmd.AddCommand(dbDowngradeCmd)
39+
}

cmd/db_drop.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cmd
2+
3+
import (
4+
"log/slog"
5+
6+
"github.com/spf13/cobra"
7+
8+
"github.com/fogo-sh/dunce/database"
9+
)
10+
11+
var dbDropCmd = &cobra.Command{
12+
Use: "drop",
13+
Short: "Completely empty the current database",
14+
Args: cobra.NoArgs,
15+
Run: func(cmd *cobra.Command, args []string) {
16+
m, err := database.NewMigrateInstance(config.DBPath)
17+
checkError(err, "Error setting up migrations")
18+
19+
slog.Info("Dropping database...")
20+
err = m.Drop()
21+
checkError(err, "Error dropping database")
22+
23+
slog.Info("Database dropped!")
24+
},
25+
}
26+
27+
func init() {
28+
dbCmd.AddCommand(dbDropCmd)
29+
}

cmd/db_step.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"log/slog"
6+
"strconv"
7+
8+
"github.com/golang-migrate/migrate/v4"
9+
"github.com/spf13/cobra"
10+
11+
"github.com/fogo-sh/dunce/database"
12+
)
13+
14+
var dbStepCmd = &cobra.Command{
15+
Use: "step [number of steps]",
16+
Example: ` Downgrade by one version: dunce db step -- -1
17+
Upgrade by 3 versions: dunce db step 3`,
18+
Short: "Perform a relative migration",
19+
Args: cobra.ExactArgs(1),
20+
Run: func(cmd *cobra.Command, args []string) {
21+
m, err := database.NewMigrateInstance(config.DBPath)
22+
checkError(err, "Error setting up migrations")
23+
24+
stepCountStr := args[0]
25+
26+
stepCount, err := strconv.Atoi(stepCountStr)
27+
checkError(err, "Error parsing step count")
28+
29+
slog.Info("Performing database migration...")
30+
err = m.Steps(stepCount)
31+
if err != nil {
32+
if errors.Is(err, migrate.ErrNoChange) {
33+
slog.Info("Database is already at desired version.")
34+
return
35+
} else {
36+
slog.Error("Error upgrading database", err)
37+
}
38+
}
39+
40+
slog.Info("Database migrations complete!")
41+
},
42+
}
43+
44+
func init() {
45+
dbCmd.AddCommand(dbStepCmd)
46+
}

cmd/db_upgrade.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"log/slog"
6+
7+
"github.com/golang-migrate/migrate/v4"
8+
"github.com/spf13/cobra"
9+
10+
"github.com/fogo-sh/dunce/database"
11+
)
12+
13+
var dbUpgradeCmd = &cobra.Command{
14+
Use: "upgrade",
15+
Aliases: []string{"up"},
16+
Short: "Perform all database upgrades",
17+
Args: cobra.NoArgs,
18+
Run: func(cmd *cobra.Command, args []string) {
19+
m, err := database.NewMigrateInstance(config.DBPath)
20+
checkError(err, "Error setting up migrations")
21+
22+
slog.Info("Performing database upgrades...")
23+
err = m.Up()
24+
if err != nil {
25+
if errors.Is(err, migrate.ErrNoChange) {
26+
slog.Info("Database is already up to date!")
27+
return
28+
} else {
29+
slog.Error("Error upgrading database", err)
30+
}
31+
}
32+
33+
slog.Info("Database upgrades complete!")
34+
},
35+
}
36+
37+
func init() {
38+
dbCmd.AddCommand(dbUpgradeCmd)
39+
}

cmd/db_version.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"log/slog"
6+
7+
"github.com/spf13/cobra"
8+
9+
"github.com/fogo-sh/dunce/database"
10+
)
11+
12+
var dbVersionCmd = &cobra.Command{
13+
Use: "version",
14+
Short: "Fetch the current database version",
15+
Args: cobra.NoArgs,
16+
Run: func(cmd *cobra.Command, args []string) {
17+
m, err := database.NewMigrateInstance(config.DBPath)
18+
checkError(err, "Error setting up migrations")
19+
20+
version, dirty, err := m.Version()
21+
checkError(err, "Error fetching database version")
22+
23+
slog.Info(fmt.Sprintf("Version %d", version), "dirty", dirty)
24+
},
25+
}
26+
27+
func init() {
28+
dbCmd.AddCommand(dbVersionCmd)
29+
}

cmd/root.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"log/slog"
6+
"os"
7+
8+
"github.com/joho/godotenv"
9+
"github.com/kelseyhightower/envconfig"
10+
"github.com/spf13/cobra"
11+
12+
"github.com/fogo-sh/dunce/bot"
13+
)
14+
15+
var config bot.Config
16+
17+
var rootCmd = &cobra.Command{
18+
Use: "dunce",
19+
Short: "Dunce",
20+
}
21+
22+
func Execute() {
23+
err := godotenv.Load()
24+
if err != nil {
25+
slog.Error("Failed to load .env file", "error", err)
26+
os.Exit(1)
27+
}
28+
29+
err = envconfig.Process("dunce", &config)
30+
if err != nil {
31+
slog.Error("Error loading config", "error", err)
32+
os.Exit(1)
33+
}
34+
35+
if err := rootCmd.Execute(); err != nil {
36+
_, _ = fmt.Fprintln(os.Stderr, err)
37+
os.Exit(1)
38+
}
39+
}

cmd/start.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cmd
2+
3+
import (
4+
"log/slog"
5+
6+
"github.com/spf13/cobra"
7+
8+
"github.com/fogo-sh/dunce/bot"
9+
)
10+
11+
var startCmd = &cobra.Command{
12+
Use: "start",
13+
Short: "Start the Discord bot",
14+
Args: cobra.NoArgs,
15+
Run: func(cmd *cobra.Command, args []string) {
16+
err := bot.Run(config)
17+
if err != nil {
18+
slog.Error("Error running Dunce", "error", err)
19+
}
20+
},
21+
}
22+
23+
func init() {
24+
rootCmd.AddCommand(startCmd)
25+
}

cmd/utils.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cmd
2+
3+
import "log/slog"
4+
5+
func checkError(err error, message string) {
6+
if err != nil {
7+
slog.Error(message)
8+
panic(err)
9+
}
10+
}

0 commit comments

Comments
 (0)