-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_create.go
More file actions
144 lines (119 loc) · 3.59 KB
/
cmd_create.go
File metadata and controls
144 lines (119 loc) · 3.59 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
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"time"
"github.com/google/subcommands"
"github.com/jacobbrewer1/goschema/pkg/logging"
"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, _ ...any) subcommands.ExitStatus {
if c.name == "" {
slog.Error("Name is required")
return subcommands.ExitUsageError
}
switch c.outputLocation {
case "":
slog.Error("Output location is required")
return subcommands.ExitUsageError
case ".":
// Get the directory that called the command
dir, err := os.Getwd()
if err != nil {
slog.Error("Error getting working directory", slog.String(logging.KeyError, err.Error()))
return subcommands.ExitFailure
}
c.outputLocation = dir
default:
// Attach the current working directory to the output location
wd, err := os.Getwd()
if err != nil {
slog.Error("Error getting working directory", slog.String(logging.KeyError, err.Error()))
return subcommands.ExitFailure
}
c.outputLocation = filepath.Join(c.outputLocation, wd)
}
// 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 := name + ".up.sql"
downName := name + ".down.sql"
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(logging.KeyPath, upPath),
slog.String(logging.KeyError, err.Error()),
)
return subcommands.ExitFailure
}
downAbs, err := filepath.Abs(downPath)
if err != nil {
slog.Error("Error getting absolute path",
slog.String(logging.KeyPath, downPath),
slog.String(logging.KeyError, err.Error()),
)
return subcommands.ExitFailure
}
if err := createFile(upAbs); err != nil {
slog.Error("Error creating file",
slog.String(logging.KeyPath, upAbs),
slog.String(logging.KeyError, err.Error()),
)
return subcommands.ExitFailure
}
slog.Info("Up migration created",
slog.String(logging.KeyPath, upAbs))
if err := createFile(downAbs); err != nil {
slog.Error("Error creating file",
slog.String(logging.KeyPath, downAbs),
slog.String(logging.KeyError, err.Error()),
)
return subcommands.ExitFailure
}
slog.Info("Down migration created",
slog.String(logging.KeyPath, 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()
}