This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
161 lines (150 loc) · 4.06 KB
/
command.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
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
package main
import (
"context"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/zevst/zlog"
"go.uber.org/multierr"
"go.uber.org/zap"
"strings"
)
type Action string
const (
UP Action = "up"
DOWN Action = "down"
)
var migrationDir string
func (m Action) is(value string) bool {
return m.String() == strings.ToLower(value)
}
func (m Action) String() string {
return string(m)
}
func rootCmd() *cobra.Command {
var configFilePath string
cmd := &cobra.Command{
Use: "Gomig",
Long: "Gomig is a migration tool",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := viper.ReadInConfig(); err != nil {
return err
}
if len(configFilePath) > 0 {
viper.SetConfigFile(configFilePath)
if err := viper.MergeInConfig(); err != nil {
return err
}
}
if err := viper.Unmarshal(&config); err != nil {
return err
}
if config.Loggers != nil {
zlog.Start(config.Loggers.Core(), zap.AddCaller(), zap.AddCallerSkip(1), zap.AddStacktrace(zap.DPanicLevel))
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
cmd.PersistentFlags().StringVarP(&configFilePath, "config", "c", getEnv("GOMIG_CONFIG_FILE_PATH", ""), "config file path")
cmd.PersistentFlags().StringVarP(&migrationDir, "dir", "d", getEnv("GOMIG_DIR", "migrations"), "directory with migrations")
return cmd
}
func upCmd(ctx context.Context) *cobra.Command {
var base string
cmd := &cobra.Command{
Use: "up",
Short: "Apply all up migrations",
RunE: func(cmd *cobra.Command, args []string) error {
if len(config.Databases) == 0 {
return ErrDatabaseNotConfigured
}
if len(base) > 0 {
if dbConfig, ok := config.Databases[base]; ok {
return up(ctx, dbConfig)
}
return ErrDatabaseNotFound
}
var err error
for _, dbConfig := range config.Databases {
err = multierr.Append(err, up(ctx, dbConfig))
}
return err
},
SilenceUsage: true,
SilenceErrors: true,
}
cmd.Flags().StringVarP(&base, "base", "b", "", "database name in the config")
return cmd
}
func downCmd(ctx context.Context) *cobra.Command {
var base string
cmd := &cobra.Command{
Use: "down",
Short: "Apply all down migrations",
RunE: func(cmd *cobra.Command, args []string) error {
if len(config.Databases) == 0 {
return ErrDatabaseNotConfigured
}
if len(base) > 0 {
if dbConfig, ok := config.Databases[base]; ok {
return down(ctx, dbConfig)
}
return ErrDatabaseNotFound
}
var err error
for _, dbConfig := range config.Databases {
err = multierr.Append(err, down(ctx, dbConfig))
}
return err
},
SilenceUsage: true,
SilenceErrors: true,
}
cmd.Flags().StringVarP(&base, "base", "b", "", "database name in the config")
return cmd
}
func applyCmd(ctx context.Context) *cobra.Command {
var base, file string
cmd := &cobra.Command{
Use: "apply",
Short: "Apply migration from file",
RunE: func(cmd *cobra.Command, args []string) error {
if len(config.Databases) == 0 {
return ErrDatabaseNotConfigured
}
if len(base) > 0 {
if dbConfig, ok := config.Databases[base]; ok {
return apply(ctx, dbConfig, file)
}
}
return ErrDatabaseNotFound
},
SilenceUsage: true,
SilenceErrors: true,
}
cmd.Flags().StringVarP(&base, "base", "b", "", "database name in the config")
cmd.Flags().StringVarP(&file, "file", "f", "", "migration file path")
_ = cmd.MarkFlagRequired("base")
_ = cmd.MarkFlagRequired("file")
return cmd
}
func createCmd() *cobra.Command {
var base, name, out string
cmd := &cobra.Command{
Use: "create",
Short: "Create migration file",
RunE: func(cmd *cobra.Command, args []string) error {
return create(base, name, out)
},
SilenceUsage: true,
SilenceErrors: true,
}
cmd.Flags().StringVarP(&base, "base", "b", "", "database name in the config")
cmd.Flags().StringVarP(&name, "name", "n", "", "migration name")
cmd.Flags().StringVarP(&out, "out", "o", getEnv("GOMIG_DIR", "migrations"), "out directory")
_ = cmd.MarkFlagRequired("base")
_ = cmd.MarkFlagRequired("name")
return cmd
}