seed.go 797 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Author: simon (ynwdlxm@163.com)
  2. // Date: 2025/7/16 11:24
  3. // Desc: seed 命令
  4. package cmd
  5. import (
  6. "github.com/spf13/cobra"
  7. "github.com/runningwater/gohub/database/seeders"
  8. "github.com/runningwater/gohub/pkg/console"
  9. "github.com/runningwater/gohub/pkg/seed"
  10. )
  11. var CmdDBSeed = &cobra.Command{
  12. Use: "seed",
  13. Short: "Insert fake data to the database",
  14. Run: runSeeders,
  15. Args: cobra.MaximumNArgs(1), // 只允许一个参数
  16. }
  17. func runSeeders(cmd *cobra.Command, args []string) {
  18. seeders.Initialize()
  19. if len(args) > 0 {
  20. name := args[0]
  21. seeder := seed.GetSeeder(name)
  22. if len(seeder.Name) > 0 {
  23. seed.RunSeeder(name)
  24. } else {
  25. console.Error("Seeder not found: " + name)
  26. }
  27. } else {
  28. // 默认运行全部迁移
  29. seed.RunAll()
  30. console.Success("Done seeding.")
  31. }
  32. }