70 lines
2.1 KiB
Go
70 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"Rezina-Backend/internal/demoseed"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
func main() {
|
|
environment := flag.String("environment", "", "Required: development, test, or staging")
|
|
flag.Parse()
|
|
_ = godotenv.Load()
|
|
skip, err := seedEnvironmentPolicy(*environment, os.Getenv)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
if skip {
|
|
fmt.Println("Skipping demo seed: production environment detected; no database connection or demo data changes were made.")
|
|
return
|
|
}
|
|
url := os.Getenv("DB_URL")
|
|
if url == "" {
|
|
fmt.Fprintln(os.Stderr, "DB_URL is required")
|
|
os.Exit(1)
|
|
}
|
|
password := os.Getenv("DEMO_SEED_PASSWORD")
|
|
if password == "" {
|
|
password = demoseed.DefaultPassword
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
|
defer cancel()
|
|
conn, err := pgx.Connect(ctx, url)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "Cannot connect to the demo database. Check DB_URL and start PostgreSQL.")
|
|
os.Exit(1)
|
|
}
|
|
defer conn.Close(ctx)
|
|
video := os.Getenv("DEMO_VIDEO_URL")
|
|
if video == "" {
|
|
video = "https://storage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
|
|
}
|
|
if err = demoseed.Run(ctx, conn, password, video); err != nil {
|
|
fmt.Fprintln(os.Stderr, "Demo seed failed:", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println("Makeup artistry demo seed complete: 3 published courses, 6 chapters, 18 lessons, 6 quizzes, 18 questions, and 1 hidden draft course.")
|
|
fmt.Println("Test accounts (password: DEMO_SEED_PASSWORD, or RezinaDemo!2026 when unset):")
|
|
for _, a := range demoseed.Accounts {
|
|
fmt.Printf(" %-36s %-16s %s\n", a.Email, a.Role, a.Audience)
|
|
}
|
|
}
|
|
|
|
// Production skips are successful no-ops for deployment runners that invoke the
|
|
// optional seed step unconditionally. Invalid command targets still fail.
|
|
func seedEnvironmentPolicy(target string, getenv func(string) string) (bool, error) {
|
|
err := demoseed.ValidateEnvironment(target, getenv("APP_ENV"), getenv("ENVIRONMENT"), getenv("GO_ENV"), getenv("ENV"))
|
|
if errors.Is(err, demoseed.ErrProductionEnvironment) {
|
|
return true, nil
|
|
}
|
|
return false, err
|
|
}
|