-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.go
36 lines (31 loc) · 940 Bytes
/
new.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
package scriptup
import (
"errors"
"fmt"
"github.com/mg98/scriptup/pkg/scriptup/migration"
"os"
"regexp"
"time"
)
// templateContent is the initial content in a new migration file.
var templateContent = `### migrate up ###
### migrate down ###
`
// currentTime returns the current time as it would be used inside the migration's identifier.
// This is abstracted into a variable to let it be overridden in testing.
var currentTime = time.Now().UTC
// NewMigrationFile creates a new migration file.
func NewMigrationFile(cfg *Config, name string) error {
if name == "" {
return errors.New("no migration name")
}
if !regexp.MustCompile(fmt.Sprintf("^%s$", migration.NamePattern)).MatchString(name) {
return errors.New("name contains illegal characters")
}
ts := currentTime().Format(migration.DateLayout)
return os.WriteFile(
fmt.Sprintf("%s/%s_%s.sh", cfg.Directory, ts, name),
[]byte(templateContent),
0644,
)
}