This commit is contained in:
sam 2023-09-03 00:23:48 +02:00
commit 2586161abd
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
49 changed files with 4171 additions and 0 deletions

64
cmd/seed/seed.go Normal file
View file

@ -0,0 +1,64 @@
package seed
import (
"fmt"
"emperror.dev/errors"
"git.sleepycat.moe/sam/mercury/config"
"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")
}
a := app.NewApp(cfg, db)
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!")
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
}