更新
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/example/xingyu/internal/testusers"
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func main() {
|
||||
apply := flag.Bool("apply", false, "write the 100-user batch (default: print JSON only)")
|
||||
base := flag.String("public-base", "", "public media URL, e.g. https://im.bchongw.com/uploads")
|
||||
confirm := flag.String("confirm-database", "", "exact target database name, required for --apply")
|
||||
production := flag.Bool("allow-production-test-data", false, "confirm intentional production fixture import")
|
||||
source := flag.String("avatars-dir", "../fixtures/test-users/avatars", "generated avatar source directory")
|
||||
media := flag.String("media-dir", "", "actual configured storage.local.directory / IM_MEDIA_DIR")
|
||||
flag.Parse()
|
||||
profiles, err := testusers.Generate(*base)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if !*apply {
|
||||
encoder := json.NewEncoder(os.Stdout)
|
||||
encoder.SetIndent("", " ")
|
||||
if err = encoder.Encode(profiles); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if os.Getenv("IM_ENV") != "development" && os.Getenv("IM_ENV") != "test" && !*production {
|
||||
log.Fatal("set IM_ENV=development/test, or explicitly confirm with --allow-production-test-data")
|
||||
}
|
||||
dsn := os.Getenv("IM_DB_DSN")
|
||||
cfg, err := mysql.ParseDSN(dsn)
|
||||
if err != nil || dsn == "" || *confirm == "" || cfg.DBName != *confirm {
|
||||
log.Fatal("set IM_DB_DSN and matching --confirm-database; no default database is used")
|
||||
}
|
||||
cfg.ParseTime = true
|
||||
db, err := sql.Open("mysql", cfg.FormatDSN())
|
||||
if err != nil {
|
||||
log.Fatal("cannot open database")
|
||||
}
|
||||
defer db.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
if err = db.PingContext(ctx); err != nil {
|
||||
log.Fatal("database connection failed (credentials not logged)")
|
||||
}
|
||||
if err = testusers.CopyAvatars(*source, *media); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
result, err := testusers.Seed(ctx, db, *base, *confirm)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
data, _ := json.MarshalIndent(result, "", " ")
|
||||
fmt.Println(string(data))
|
||||
}
|
||||
Reference in New Issue
Block a user