onasty/cmd/seed/main.go (view raw)
Smirnov Oleksandr
Smirnov Oleksandr
ss2316544@gmail.com chore: update dockerizing setup (#129)..., 12 months ago
ss2316544@gmail.com chore: update dockerizing setup (#129)..., 12 months ago
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "log/slog" |
| 7 | "os" |
| 8 | |
| 9 | "github.com/olexsmir/onasty/internal/config" |
| 10 | "github.com/olexsmir/onasty/internal/hasher" |
| 11 | "github.com/olexsmir/onasty/internal/logger" |
| 12 | "github.com/olexsmir/onasty/internal/store/psqlutil" |
| 13 | ) |
| 14 | |
| 15 | func main() { |
| 16 | if err := run(context.Background()); err != nil { |
| 17 | fmt.Fprintf(os.Stderr, "error: %v\n", err) |
| 18 | os.Exit(1) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | func run(ctx context.Context) error { |
| 23 | cfg := config.NewConfig() |
| 24 | |
| 25 | logger, err := logger.NewCustomLogger(cfg.LogLevel, cfg.LogFormat, cfg.LogShowLine) |
| 26 | if err != nil { |
| 27 | return err |
| 28 | } |
| 29 | slog.SetDefault(logger) |
| 30 | |
| 31 | psql, err := psqlutil.Connect(ctx, cfg.PostgresDSN) |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | |
| 36 | userHasher := hasher.NewSHA256Hasher(cfg.PasswordSalt) |
| 37 | noteHasher := hasher.NewSHA256Hasher(cfg.NotePasswordSalt) |
| 38 | |
| 39 | if err := seedUsers(ctx, userHasher, psql); err != nil { |
| 40 | return fmt.Errorf("failed to seed users: %w", err) |
| 41 | } |
| 42 | |
| 43 | slog.Info("Users seeded successfully") |
| 44 | |
| 45 | if err := seedNotes(ctx, noteHasher, psql); err != nil { |
| 46 | return fmt.Errorf("failed to seed notes: %w", err) |
| 47 | } |
| 48 | |
| 49 | slog.Info("Notes seeded successfully") |
| 50 | |
| 51 | return nil |
| 52 | } |