-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.cmd.go
117 lines (97 loc) · 2.65 KB
/
seed.cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package commands
import (
"context"
"fmt"
"strings"
"github.com/spacetab-io/commands-go/log"
"github.com/spf13/cobra"
)
// SeedCmd is a database seeding wrapper command.
var (
SeedCmd = &cobra.Command{
Use: "seed",
Short: "Database seeding command",
ValidArgs: []string{"run", "run-all", "list"},
Args: cobra.MinimumNArgs(1),
}
seedListCmd = &cobra.Command{
Use: "list",
RunE: seedList,
}
SeedRunAllCmd = &cobra.Command{
Use: "run-all",
RunE: seedRunAll,
}
SeedRunCmd = &cobra.Command{
Use: "run",
Args: cobra.MinimumNArgs(1),
RunE: seedRun,
}
)
// seedUsage shows seed command usage.
// Add it to SeedCmd like SeedCmd.SetUsageFunc(SeedUsage).
func seedUsage(cmd *cobra.Command) error {
w := cmd.OutOrStderr()
if _, err := w.Write([]byte(fmt.Sprintf(`Usage:
%s %s [args]
Args:
run runs concreete seed
run-all applies all seeds
list shows available seeds list
`, cmd.Parent().Name(), cmd.Name()))); err != nil {
return fmt.Errorf("SeedUsage err: %w", err)
}
return nil
}
// seedList returns seeds list.
func seedList(cmd *cobra.Command, _ []string) error {
s, err := getAppSeeder(cmd.Context())
if err != nil {
return err
}
cmd.Printf("Available seed list:\n %s\n", strings.Join(s.SeedsList(), "\n "))
return nil
}
// seedRun Execute exact seed for passed method name.
func seedRun(cmd *cobra.Command, args []string) error {
s, err := getAppSeeder(cmd.Context())
if err != nil {
return fmt.Errorf("seedRun getAppSeeder() error: %w", err)
}
log.Debug().Strs("seeds", args).Msg("Running seeder...")
// Execute only the given method names
for _, item := range args {
seed, err := s.GetMethod(item)
if err != nil {
return fmt.Errorf("seedRun GetMethod error: %w", err)
}
if err := seed.Seed(); err != nil {
return fmt.Errorf("seedRun seed.Seed error: %w", err)
}
}
return nil
}
// seedRunAll Execute all seeds if no method name is given.
func seedRunAll(cmd *cobra.Command, _ []string) error {
s, err := getAppSeeder(cmd.Context())
if err != nil {
return fmt.Errorf("seedRunAll getAppSeeder() error: %w", err)
}
log.Debug().Msg("Running all seeder...")
// We are looping over the method on a Seeder struct
for _, seed := range s.GetMethods() {
// Get the method in the current iteration
// Execute seeder
if err := seed.Seed(); err != nil {
return fmt.Errorf("seedRunAll seed.Seed() error: %w", err)
}
}
return nil
}
func getAppSeeder(ctx context.Context) (SeederInterface, error) {
s, ok := ctx.Value(CommandContextObjectKeySeeder).(SeederInterface)
if !ok {
return nil, fmt.Errorf("%w: app seed (cfg.appInfo)", ErrBadContextValue)
}
return s, nil
}