-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
200 lines (174 loc) · 4.33 KB
/
cli.go
File metadata and controls
200 lines (174 loc) · 4.33 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package gomigration
import (
"context"
"log"
"strconv"
"github.com/spf13/cobra"
)
type CliConfig struct {
GoMigration *GoMigration
CliName string
}
type Cli struct {
migration *GoMigration
cliName string
}
func NewCli(config CliConfig) (*Cli, error) {
if config.GoMigration == nil {
return nil, ErrGoMigrationNotProvided
}
if config.CliName == "" {
config.CliName = "migration"
}
return &Cli{
migration: config.GoMigration,
cliName: config.CliName,
}, nil
}
func (c *Cli) ListCommand(ctx context.Context) *cobra.Command {
var listCmd = &cobra.Command{
Use: "list",
Short: "List all migrations",
Run: func(cmd *cobra.Command, args []string) {
list, err := c.migration.List(ctx)
if err != nil {
log.Println("Error listing migrations:", err)
return
}
list.Print()
},
}
return listCmd
}
func (c *Cli) MigrateCommand(ctx context.Context) *cobra.Command {
var migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Run all pending migrations",
Run: func(cmd *cobra.Command, args []string) {
fresh := false
var err error
freshFlag := cmd.Flags().Lookup("fresh")
if freshFlag != nil && freshFlag.Changed {
fresh, err = strconv.ParseBool(freshFlag.Value.String())
if err != nil {
log.Println("Invalid fresh flag:", err)
return
}
}
if fresh {
err = c.migration.Fresh(ctx)
if err != nil {
log.Println("Error running fresh migrations:", err)
return
}
} else {
err = c.migration.Migrate(ctx)
if err != nil {
log.Println("Error running migrations:", err)
return
}
}
},
}
migrateCmd.Flags().BoolP("fresh", "f", false, "Run fresh migrations")
return migrateCmd
}
func (c *Cli) RollbackCommand(ctx context.Context) *cobra.Command {
var rollbackCmd = &cobra.Command{
Use: "rollback",
Short: "Rollback the last migration",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var err error
step := 1
stepFlag := cmd.Flags().Lookup("step")
if stepFlag != nil && stepFlag.Changed {
step, err = strconv.Atoi(stepFlag.Value.String())
if err != nil {
log.Println("Invalid step:", err)
return
}
if step < 1 {
log.Println("Step must be greater than 0")
return
}
}
err = c.migration.Rollback(ctx, step)
if err != nil {
log.Println("Error rolling back migrations:", err)
return
}
},
}
rollbackCmd.Flags().IntP("step", "s", 1, "Number of migrations to rollback")
return rollbackCmd
}
func (c *Cli) ResetCommand(ctx context.Context) *cobra.Command {
var resetCmd = &cobra.Command{
Use: "reset",
Short: "Rollback all migrations and re-run all migrations",
Run: func(cmd *cobra.Command, args []string) {
err := c.migration.Reset(ctx)
if err != nil {
log.Println("Error resetting migrations:", err)
return
}
},
}
return resetCmd
}
func (c *Cli) CleanCommand(ctx context.Context) *cobra.Command {
var cleanCmd = &cobra.Command{
Use: "clean",
Short: "Clean database (delete all tables)",
Run: func(cmd *cobra.Command, args []string) {
err := c.migration.Clean(ctx)
if err != nil {
log.Println("Error cleaning database:", err)
return
}
},
}
return cleanCmd
}
func (c *Cli) CreateCommand(ctx context.Context) *cobra.Command {
var createCmd = &cobra.Command{
Use: "create",
Short: "Create a new migration",
Run: func(cmd *cobra.Command, args []string) {
dir, _ := cmd.Flags().GetString("dir")
name, _ := cmd.Flags().GetString("name")
err := c.migration.SetMigrationFilesDir(dir).Create(name)
if err != nil {
log.Println("Error creating migration:", err)
return
}
},
}
createCmd.Flags().StringP("name", "n", "", "name of the migration")
createCmd.Flags().StringP("dir", "d", "", "directory of the migration")
createCmd.MarkFlagRequired("name")
createCmd.MarkFlagRequired("dir")
return createCmd
}
func (c *Cli) Execute(ctx context.Context) error {
var rootCmd = &cobra.Command{
Use: c.cliName,
CompletionOptions: cobra.CompletionOptions{
HiddenDefaultCmd: true,
},
Short: "GoMigration CLI",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
rootCmd.AddCommand(
c.ListCommand(ctx),
c.MigrateCommand(ctx),
c.RollbackCommand(ctx),
c.ResetCommand(ctx),
c.CleanCommand(ctx),
c.CreateCommand(ctx),
)
return rootCmd.Execute()
}