-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd-init.go
49 lines (40 loc) · 1 KB
/
cmd-init.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
package cmds
import (
"errors"
"fmt"
"io/fs"
"log"
"os"
goose "github.com/pressly/goose/v3"
"github.com/spf13/cobra"
)
// initDir will create a directory with an empty SQL migration file.
func gooseInit(dir string) error {
_, err := os.Stat(dir)
createDir := true
switch {
case errors.Is(err, fs.ErrNotExist):
case err == nil, errors.Is(err, fs.ErrExist):
// return fmt.Errorf("Directory already exists : %s", dir)
createDir = false
default:
return err
}
if createDir {
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
fmt.Printf("Directory '%s' created…\n", dir)
}
return goose.CreateWithTemplate(nil, dir, sqlMigrationTemplate, "initial", "sql")
}
var initCmd = &cobra.Command{
Use: "init",
Short: "Create the migrations' directory and an empty initial SQL migration file.",
TraverseChildren: true,
Run: func(cmd *cobra.Command, args []string) {
if err := gooseInit(dbmConfig.DBMigration.Dir); err != nil {
log.Fatalf("%v", err)
}
},
}