mercury/cmd/seed/seed.go

69 lines
1.7 KiB
Go
Raw Permalink Normal View History

2023-09-03 00:23:48 +02:00
package seed
import (
"fmt"
"emperror.dev/errors"
"git.sleepycat.moe/sam/mercury/config"
"git.sleepycat.moe/sam/mercury/internal/database"
2023-09-03 00:23:48 +02:00
"git.sleepycat.moe/sam/mercury/internal/database/sql"
"git.sleepycat.moe/sam/mercury/web/app"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
)
var Command = &cli.Command{
Name: "seed",
Usage: "Seed the database with some debug data",
Action: run,
}
func run(c *cli.Context) error {
log.Debug().Msg("Reading configuration")
cfg, err := config.Parse("config.toml")
if err != nil {
return errors.Wrap(err, "reading configuration")
}
log.Debug().Msg("Connecting to database")
db, err := sql.NewBase(c.Context, cfg.Core.Postgres)
if err != nil {
return errors.Wrap(err, "creating postgres database")
}
2023-09-04 03:33:13 +02:00
a, err := app.NewApp(c.Context, cfg, db)
2023-09-03 04:11:56 +02:00
if err != nil {
return errors.Wrap(err, "creating app")
}
2023-09-03 00:23:48 +02:00
log.Debug().Msg("Creating account")
acct, err := a.Account().CreateLocal(c.Context, "testington", "no@mercury.example", []byte("password"))
if err != nil {
log.Err(err).Msg("Creating account")
return nil
}
log.Debug().Msg("Created account")
log.Debug().Msg("Creating blog")
blog, err := a.Blog().Create(c.Context, acct.ID, acct.Username)
if err != nil {
log.Err(err).Msg("Creating blog")
return nil
}
log.Debug().Msg("Created blog")
log.Debug().Msg("Creating post")
post, err := a.Post().Create(c.Context, blog, "Hello world!", database.PublicVisibility)
2023-09-03 00:23:48 +02:00
if err != nil {
log.Err(err).Msg("Creating post")
return nil
}
log.Debug().Msg("Created post")
fmt.Printf(`created account => %v (%v)
created blog => %v (%v)
created post => %v
`, acct.ID, acct.Username, blog.ID, blog.Name, post.ID)
return nil
}