Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAC-36 | add seed:run cli command #96

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions console/commands/run_seed.go
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions console/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -27,7 +26,6 @@ func (s *ServeCommand) Run() framework.CommandRunner {
router infrastructure.Router,
logger framework.Logger,
database infrastructure.Database,
seeds seeds.Seeds,

) {
logger.Info(`+-----------------------+`)
Expand All @@ -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{
Expand Down
4 changes: 4 additions & 0 deletions seeds/admin_seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions seeds/seeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Loading