-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_create.go
123 lines (98 loc) · 3.29 KB
/
cmd_create.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
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"time"
"github.com/google/subcommands"
"github.com/jacobbrewer1/goschema/pkg/migrations"
)
type createCmd struct {
// name is the name of the migration to create.
name string
// OutputLocation is the location to write the generated files to.
outputLocation string
}
func (c *createCmd) Name() string {
return "create"
}
func (c *createCmd) Synopsis() string {
return "Create a new migration"
}
func (c *createCmd) Usage() string {
return `create:
Create a new migration.
`
}
func (c *createCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&c.name, "name", "", "The name of the migration to create.")
f.StringVar(&c.outputLocation, "out", ".", "The location to write the generated files to.")
}
func (c *createCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
if c.name == "" {
slog.Error("Name is required")
return subcommands.ExitUsageError
}
if c.outputLocation == "" {
slog.Error("Output location is required")
return subcommands.ExitUsageError
} else if c.outputLocation == "." {
// Get the directory that called the command
dir, err := os.Getwd()
if err != nil {
slog.Error("Error getting working directory", slog.String("error", err.Error()))
return subcommands.ExitFailure
}
c.outputLocation = dir
} else {
// Attach the current working directory to the output location
c.outputLocation = filepath.Join(c.outputLocation)
}
// File name is timestamp_name.up.sql and timestamp_name.down.sql
// The timestamp is the current time in the format YYYYMMDDHHMMSS
// The name is the name of the migration with spaces as underscores
now := time.Now().UTC()
name := fmt.Sprintf("%s_%s", now.Format(migrations.FilePrefix), strings.TrimSpace(c.name))
name = strings.ReplaceAll(name, " ", "_")
upName := fmt.Sprintf("%s.up.sql", name)
downName := fmt.Sprintf("%s.down.sql", name)
upPath := fmt.Sprintf("%s/%s", c.outputLocation, upName)
downPath := fmt.Sprintf("%s/%s", c.outputLocation, downName)
upAbs, err := filepath.Abs(upPath)
if err != nil {
slog.Error("Error getting absolute path", slog.String("path", upPath), slog.String("error", err.Error()))
return subcommands.ExitFailure
}
downAbs, err := filepath.Abs(downPath)
if err != nil {
slog.Error("Error getting absolute path", slog.String("path", downPath), slog.String("error", err.Error()))
return subcommands.ExitFailure
}
if err := createFile(upAbs); err != nil {
slog.Error("Error creating file", slog.String("path", upAbs), slog.String("error", err.Error()))
return subcommands.ExitFailure
}
slog.Info("Up migration created", slog.String("path", upAbs))
if err := createFile(downAbs); err != nil {
slog.Error("Error creating file", slog.String("path", downAbs), slog.String("error", err.Error()))
return subcommands.ExitFailure
}
slog.Info("Down migration created", slog.String("path", downAbs))
return subcommands.ExitSuccess
}
func createFile(name string) error {
// Create the path if it does not exist.
dir := filepath.Dir(name)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return fmt.Errorf("error creating path: %w", err)
}
f, err := os.Create(name)
if err != nil {
return fmt.Errorf("error creating file: %w", err)
}
return f.Close()
}