From b59aa87951a0cdb056cdc7d06aa431bb9f0acda1 Mon Sep 17 00:00:00 2001 From: Sudarshan Pathak Date: Thu, 11 Jan 2024 16:11:04 +0545 Subject: [PATCH] add seed:run cli command --- console/commands/run_seed.go | 77 ++++++++++++++++++++++++++++++++++++ console/console.go | 1 + console/serve.go | 3 -- seeds/admin_seed.go | 4 ++ seeds/seeds.go | 9 +---- 5 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 console/commands/run_seed.go diff --git a/console/commands/run_seed.go b/console/commands/run_seed.go new file mode 100644 index 0000000..4c6a086 --- /dev/null +++ b/console/commands/run_seed.go @@ -0,0 +1,77 @@ +package commands + +import ( + "clean-architecture/pkg/framework" + "clean-architecture/seeds" + "errors" + + "github.com/spf13/cobra" +) + +type SeedCommand struct { + names []string + runAll bool +} + +func (s *SeedCommand) Short() string { + return "run seed command" +} + +func NewSeedCommand() *SeedCommand { + return &SeedCommand{} +} + +func (s *SeedCommand) Setup(cmd *cobra.Command) { + cmd.Flags().StringArrayVarP( + &s.names, + "name", + "n", + []string{}, + "name of the seed to run (can be used multiple times)", + ) + cmd.Flags().BoolVar(&s.runAll, "all", false, "run all seeds") +} + +func (s *SeedCommand) Run() framework.CommandRunner { + return func( + l framework.Logger, + seeds seeds.Seeds, + ) { + + // run all seeds + if s.runAll { + for _, seed := range seeds { + seed.Setup() + } + return + } + + // validate names array + if len(s.names) == 0 { + l.Info("no seed name provided") + return + } + + // run selected seeds + for _, name := range s.names { + if err := runSeed(name, &seeds); err != nil { + l.Infof("Error running %s: %s", name, err) + } + } // end for loop + } // end return func +} // end run func + +func runSeed(name string, seeds *seeds.Seeds) error { + isValid := false + for _, seed := range *seeds { + if name == seed.Name() { + isValid = true + seed.Setup() + } + } // end for loop + + if !isValid { + return errors.New("invalid seed name") + } + return nil +} // end runSeed func diff --git a/console/console.go b/console/console.go index 2a8fc86..549eaf4 100644 --- a/console/console.go +++ b/console/console.go @@ -12,6 +12,7 @@ import ( var cmds = map[string]framework.Command{ "cmd:random": commands.NewRandomCommand(), "app:serve": NewServeCommand(), + "seed:run": commands.NewSeedCommand(), } // GetSubCommands gives a list of sub commands diff --git a/console/serve.go b/console/serve.go index 5af6471..c766982 100644 --- a/console/serve.go +++ b/console/serve.go @@ -4,7 +4,6 @@ import ( "clean-architecture/pkg/framework" "clean-architecture/pkg/infrastructure" "clean-architecture/pkg/middlewares" - "clean-architecture/seeds" "time" "github.com/getsentry/sentry-go" @@ -27,7 +26,6 @@ func (s *ServeCommand) Run() framework.CommandRunner { router infrastructure.Router, logger framework.Logger, database infrastructure.Database, - seeds seeds.Seeds, ) { logger.Info(`+-----------------------+`) @@ -39,7 +37,6 @@ func (s *ServeCommand) Run() framework.CommandRunner { time.Local = loc middleware.Setup() - seeds.Setup() if env.Environment != "local" && env.SentryDSN != "" { err := sentry.Init(sentry.ClientOptions{ diff --git a/seeds/admin_seed.go b/seeds/admin_seed.go index 1520c00..5307df6 100644 --- a/seeds/admin_seed.go +++ b/seeds/admin_seed.go @@ -28,6 +28,10 @@ func NewAdminSeed( } } +func (as AdminSeed) Name() string { + return "AdminSeed" +} + // Run admin seeder func (as AdminSeed) Setup() { // Create email manually in firebase diff --git a/seeds/seeds.go b/seeds/seeds.go index a7f694b..4c0e472 100644 --- a/seeds/seeds.go +++ b/seeds/seeds.go @@ -10,19 +10,14 @@ var Module = fx.Options( // Seed db seed type Seed interface { + // name is used to identify the seed + Name() string Setup() } // Seeds listing of seeds type Seeds []Seed -// Run run the seed data -func (s Seeds) Setup() { - for _, seed := range s { - seed.Setup() - } -} - // NewSeeds creates new seeds func NewSeeds(adminSeed AdminSeed) Seeds { return Seeds{