mercury/cmd/seed/seed.go

68 lines
1.7 KiB
Go

package seed
import (
"fmt"
"emperror.dev/errors"
"git.sleepycat.moe/sam/mercury/config"
"git.sleepycat.moe/sam/mercury/internal/database"
"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, err := app.NewApp(c.Context, cfg, db)
if err != nil {
return errors.Wrap(err, "creating app")
}
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)
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
}