-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
143 lines (136 loc) · 3.43 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
package main
import (
"bytes"
"context"
_ "embed"
"fmt"
"os"
"strconv"
"strings"
"text/template"
"ariga.io/atlas-go-sdk/atlasexec"
"github.com/sethvargo/go-githubactions"
)
var (
//go:embed atlashcl.tmpl
tmpl string
config = template.Must(template.New("atlashcl").Parse(tmpl))
)
func main() {
act := githubactions.New()
act.Warningf("This action is deprecated. Please use ariga/atlas-action/migrate/apply instead. " +
"For details see: https://github.com/ariga/atlas-action#arigaatlas-actionmigrateapply")
inp, err := Load(act)
if err != nil {
act.Fatalf("failed to load input: %v", err)
}
run, err := Run(context.Background(), inp)
if err != nil {
act.Fatalf("failed to run: %v", err)
}
act.SetOutput("error", run.Error)
act.SetOutput("current", run.Current)
act.SetOutput("target", run.Target)
act.SetOutput("pending_count", strconv.Itoa(len(run.Pending)))
act.SetOutput("applied_count", strconv.Itoa(len(run.Applied)))
act.Infof("Run complete: +%v", run)
}
type (
// Input is created from the GitHub Action "with" configuration.
Input struct {
URL string
Amount uint64
TxMode string
Baseline string
AllowDirty bool
Dir string
RevisionsSchema string
Cloud Cloud
}
Cloud struct {
Dir string
Token string
URL string
Tag string
}
)
// Load loads the input from the GitHub Action configuration.
func Load(act *githubactions.Action) (*Input, error) {
i := &Input{
URL: act.GetInput("url"),
}
if i.URL == "" {
return nil, fmt.Errorf("url is required")
}
if as := act.GetInput("amount"); as != "" {
a, err := strconv.ParseUint(as, 10, 64)
if err != nil {
return nil, err
}
i.Amount = a
}
if txm := act.GetInput("tx-mode"); txm != "" {
switch txm {
case "all", "none", "file":
i.TxMode = txm
default:
return nil, fmt.Errorf("invalid tx-mode %q", txm)
}
i.TxMode = act.GetInput("tx-mode")
}
i.Baseline = act.GetInput("baseline")
if ad := act.GetInput("allow-dirty"); ad != "" {
allowDirty, err := strconv.ParseBool(strings.ToLower(ad))
if err != nil {
return nil, fmt.Errorf("invalid allow-dirty %q", ad)
}
i.AllowDirty = allowDirty
}
i.Dir = act.GetInput("dir")
i.Cloud.Dir = act.GetInput("cloud-dir")
if i.Dir != "" && i.Cloud.Dir != "" {
return nil, fmt.Errorf("dir and cloud-dir are mutually exclusive")
}
i.Cloud.Token = act.GetInput("cloud-token")
if i.Cloud.Dir != "" && i.Cloud.Token == "" {
return nil, fmt.Errorf("cloud-token is required when cloud-dir is set")
}
i.Cloud.URL = act.GetInput("cloud-url")
i.Cloud.Tag = act.GetInput("cloud-tag")
return i, nil
}
// Run runs the "migrate apply" for the input.
func Run(ctx context.Context, i *Input) (*atlasexec.MigrateApply, error) {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
client, err := atlasexec.NewClient(wd, "atlas")
if err != nil {
return nil, err
}
params := &atlasexec.MigrateApplyParams{
URL: i.URL,
Amount: i.Amount,
TxMode: i.TxMode,
BaselineVersion: i.Baseline,
}
if i.Dir != "" {
params.DirURL = "file://" + i.Dir
}
if i.Cloud.Dir != "" {
var buf bytes.Buffer
if err := config.Execute(&buf, i); err != nil {
return nil, err
}
cfg, clean, err := atlasexec.TempFile(buf.String(), "hcl")
if err != nil {
return nil, err
}
// nolint:errcheck
defer clean()
params.ConfigURL = cfg
params.Env = "atlas"
}
return client.MigrateApply(ctx, params)
}