-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
255 lines (240 loc) · 6.75 KB
/
main.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
"path"
"regexp"
"strconv"
"strings"
"ariga.io/atlas-go-sdk/atlasexec"
"ariga.io/atlas/sql/migrate"
"ariga.io/atlas/sql/sqltool"
"github.com/mitchellh/mapstructure"
"github.com/sethvargo/go-githubactions"
"ariga.io/atlas-sync-action/internal/atlascloud"
)
const cloudDomainPublic = "https://gh-api.atlasgo.cloud"
func main() {
act := githubactions.New()
act.Warningf("This action is deprecated. Please use ariga/atlas-action/migrate/push instead. " +
"For details see: https://github.com/ariga/atlas-action#arigaatlas-actionmigratepush")
if ok, err := strconv.ParseBool(act.GetInput("cloud-public")); err == nil && ok {
RunPublic(act)
} else {
RunCmd(act)
}
githubactions.Infof("Uploaded migration dir %q to Atlas Cloud", act.GetInput("dir"))
}
// loadPushParams loads the atlasexec params from the GitHub Action configuration.
func loadPushParams(act *githubactions.Action, c *githubactions.GitHubContext) *atlasexec.MigratePushParams {
dev := act.GetInput("dev-url")
if dev == "" {
act.Fatalf("'dev-url' input is required. See: https://github.com/marketplace/actions/atlas-sync-action#usage")
}
dirPath := act.GetInput("dir")
if dirPath == "" {
act.Fatalf("'dir' input is required. See: https://github.com/marketplace/actions/atlas-sync-action#usage")
}
name := act.GetInput("name")
if name == "" {
name = path.Join(c.Repository, dirPath)
}
// Normalize the name.
reNotSlug := regexp.MustCompile(`[^a-z0-9-._]`)
name = reNotSlug.ReplaceAllString(
strings.ToLower(strings.Trim(name, "- \t\n\r")),
"-",
)
ev := PushEvent{}
if err := mapstructure.Decode(c.Event, &ev); err != nil {
act.Fatalf("failed to parse push event: %v", err)
}
syncctx := ContextInput{
Repo: c.Repository,
Branch: c.RefName,
Commit: c.SHA,
Path: dirPath,
URL: ev.HeadCommit.URL,
}
buf, err := json.Marshal(syncctx)
if err != nil {
act.Fatalf("failed to encode context info: %v", err)
}
return &atlasexec.MigratePushParams{
Name: name,
DirURL: fmt.Sprintf("file://%s", dirPath),
DevURL: dev,
Context: string(buf),
}
}
// RunCmd pushed the directory to Atlas Cloud using atlasexec.
func RunCmd(act *githubactions.Action) {
ctx, err := act.Context()
if err != nil {
act.Fatalf("failed to load action context: %v", err)
}
params := loadPushParams(act, ctx)
token := act.GetInput("cloud-token")
if token == "" {
act.Fatalf("'cloud-token' input is required. See https://github.com/marketplace/actions/atlas-sync-action#usage")
}
c, err := atlasexec.NewClient("", "atlas")
if err != nil {
act.Fatalf("failed to connect to Atlas Cloud: %v", err)
}
if err = c.Login(context.Background(), &atlasexec.LoginParams{Token: token}); err != nil {
act.Fatalf("failed to login to Atlas Cloud: %v", err)
}
_, err = c.MigratePush(context.Background(), params)
if err != nil {
act.Fatalf("failed to push directory: %v", err)
}
// Create tag.
params.Tag = func() string {
if tag := act.GetInput("tag"); tag != "" {
return tag
}
return ctx.SHA
}()
resp, err := c.MigratePush(context.Background(), params)
if err != nil {
act.Fatalf("failed to create dir tag: %v", err)
}
act.SetOutput("url", resp)
}
// RunPublic uploads the directory to Atlas Cloud using the public API.
func RunPublic(act *githubactions.Action) {
c := client(act)
input, err := Input(act)
if err != nil {
act.Fatalf("failed to parse input: %v", err)
}
arc, err := Archive(input.Path, input.DirFormat)
if err != nil {
act.Fatalf("failed to archive migration dir: %v", err)
}
input.Dir = arc
if err := c.ReportDir(context.Background(), input); err != nil {
act.Fatalf("failed to upload dir: %v", err)
}
}
// Archive returns a b64 encoded tarball of the given migration directory.
func Archive(path string, format atlascloud.DirFormat) (string, error) {
var (
dir migrate.Dir
err error
)
switch format {
case atlascloud.DirFormatAtlas:
dir, err = migrate.NewLocalDir(path)
case atlascloud.DirFormatFlyway:
dir, err = sqltool.NewFlywayDir(path)
case atlascloud.DirFormatGolangMigrate:
dir, err = sqltool.NewGolangMigrateDir(path)
default:
return "", fmt.Errorf("unknown dir format %q", format)
}
if err != nil {
return "", err
}
arc, err := migrate.ArchiveDir(dir)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(arc), nil
}
func Input(act *githubactions.Action) (atlascloud.ReportDirInput, error) {
c, err := act.Context()
if err != nil {
return atlascloud.ReportDirInput{}, err
}
org, repo := c.Repo()
ev := PushEvent{}
if err := mapstructure.Decode(c.Event, &ev); err != nil {
return atlascloud.ReportDirInput{}, err
}
di := act.GetInput("driver")
drv, err := driver(di)
if err != nil {
return atlascloud.ReportDirInput{}, err
}
fi := act.GetInput("dir-format")
dirFmt, err := dirFormat(fi)
if err != nil {
return atlascloud.ReportDirInput{}, err
}
return atlascloud.ReportDirInput{
Name: func() *string {
if n := act.GetInput("name"); n != "" {
return &n
}
return nil
}(),
Repo: fmt.Sprintf("%s/%s", org, repo),
Branch: c.RefName,
Commit: c.SHA,
Path: act.GetInput("dir"),
Url: ev.HeadCommit.URL,
Driver: drv,
DirFormat: dirFmt,
ArchiveFormat: atlascloud.ArchiveFormatB64Tar,
}, nil
}
func driver(s string) (atlascloud.Driver, error) {
switch s := strings.ToLower(s); s {
case "postgres":
return atlascloud.DriverPostgresql, nil
case "mysql":
return atlascloud.DriverMysql, nil
case "sqlite":
return atlascloud.DriverSqlite, nil
case "maria", "mariadb":
return atlascloud.DriverMariadb, nil
default:
return "", fmt.Errorf("unknown driver %q", s)
}
}
func dirFormat(s string) (atlascloud.DirFormat, error) {
switch s := strings.ToLower(s); s {
case "atlas", "":
return atlascloud.DirFormatAtlas, nil
case "flyway":
return atlascloud.DirFormatFlyway, nil
case "golang-migrate":
return atlascloud.DirFormatGolangMigrate, nil
default:
return "", fmt.Errorf("unknown dir-format %q", s)
}
}
func client(act *githubactions.Action) *atlascloud.Client {
token, err := act.GetIDToken(context.Background(), "ariga://atlas-sync-action")
if err != nil {
act.Fatalf("failed to get id token: %v", err)
}
d := act.GetInput("cloud-url")
if d == "" {
d = cloudDomainPublic
}
u, err := url.Parse(d)
if err != nil {
act.Fatalf("failed to parse cloud-url: %v", err)
}
u.Path = "/query"
return atlascloud.New(u.String(), token)
}
type PushEvent struct {
HeadCommit struct {
URL string `mapstructure:"url"`
} `mapstructure:"head_commit"`
Ref string `mapstructure:"ref"`
}
type ContextInput struct {
Repo string `json:"repo"`
Path string `json:"path"`
Branch string `json:"branch"`
Commit string `json:"commit"`
URL string `json:"url"`
}