Repository navigation
Expand file tree
/
Copy pathplugin_dev.go
More file actions
794 lines (752 loc) · 20.3 KB
/
Copy pathplugin_dev.go
File metadata and controls
794 lines (752 loc) · 20.3 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"text/tabwriter"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
const (
systemBoilerplateURL = "https://github.com/apito-io/apito-hello-world-go-plugin.git"
paymentBoilerplateURL = "https://github.com/apito-io/apito-payment-plugin-template.git"
)
var pluginInitCmd = &cobra.Command{
Use: "init [name-or-path]",
Short: "Clone a system or payment boilerplate",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
arg := ""
if len(args) > 0 {
arg = args[0]
}
if err := runPluginInit(cmd, arg); err != nil {
print_error(err.Error())
os.Exit(1)
}
},
}
var pluginListCmd = &cobra.Command{
Use: "list",
Short: "Pick a local plugin, then check, build, or push it",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if err := runPluginList(cmd); err != nil {
print_error(err.Error())
os.Exit(1)
}
},
}
var pluginCheckCmd = &cobra.Command{
Use: "check [dir]",
Short: "Check plugin files and the GitHub release workflow",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
arg := ""
if len(args) > 0 {
arg = args[0]
}
dir, err := resolvePluginWorkdir(arg)
if err != nil {
print_error(err.Error())
os.Exit(1)
}
if err := checkPlugin(dir); err != nil {
print_error(err.Error())
os.Exit(1)
}
print_success("Plugin check passed: " + dir)
},
}
var pluginPushCmd = &cobra.Command{
Use: "push [dir]",
Aliases: []string{"deploy"},
Short: "Bump the version, commit, tag, and push",
Long: `Runs check, then writes the next version into config.yml and sdk.Init. Requires -m. Does not upload a binary.`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
arg := ""
if len(args) > 0 {
arg = args[0]
}
if err := runPluginPush(cmd, arg); err != nil {
print_error(err.Error())
os.Exit(1)
}
},
}
func defaultPluginRoot() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".apito", "plugins"), nil
}
func runPluginInit(cmd *cobra.Command, arg string) error {
kind, _ := cmd.Flags().GetString("type")
kind = strings.ToLower(strings.TrimSpace(kind))
if kind == "" {
if !syncStdinInteractive() {
return fmt.Errorf("pass --type system or --type payment")
}
prompt := promptui.Select{
Label: "Plugin type",
Items: []string{"system", "payment"},
}
_, selected, err := prompt.Run()
if err != nil {
return fmt.Errorf("plugin type cancelled")
}
kind = selected
}
repo, err := boilerplateURL(kind)
if err != nil {
return err
}
name := strings.TrimSpace(arg)
explicitPath := name != "" && (strings.Contains(name, "/") || strings.Contains(name, string(os.PathSeparator)))
var dest string
if explicitPath {
dest, err = filepath.Abs(name)
if err != nil {
return err
}
name = filepath.Base(dest)
} else {
if name == "" {
if !syncStdinInteractive() {
return fmt.Errorf("pass a plugin name")
}
prompt := promptui.Prompt{Label: "Plugin name (prefixed with hc-)", Validate: validatePluginName}
name, err = prompt.Run()
if err != nil {
return fmt.Errorf("plugin name cancelled")
}
}
if err := validatePluginName(name); err != nil {
return err
}
if !strings.HasPrefix(name, "hc-") {
name = "hc-" + name
}
parent, err := initParent(cmd)
if err != nil {
return err
}
if err := os.MkdirAll(parent, 0o755); err != nil {
return err
}
dest = filepath.Join(parent, name)
}
if err := requireHCPluginID(ensureHCName(name)); err != nil {
return err
}
if _, err := os.Stat(dest); err == nil {
return fmt.Errorf("plugin directory already exists: %s", dest)
}
print_step("Create plugin")
print_status("Cloning " + repo)
if err := runGitClone(repo, dest); err != nil {
return err
}
if err := os.RemoveAll(filepath.Join(dest, ".git")); err != nil {
print_warning("Failed to remove .git: " + err.Error())
}
if err := rewritePluginIdentity(dest, ensureHCName(filepath.Base(dest))); err != nil {
return err
}
print_success("Plugin scaffold created: " + dest)
print_status("Next: apito plugin build " + dest)
return nil
}
func ensureHCName(name string) string {
name = strings.TrimSpace(name)
if name == "" || strings.HasPrefix(name, "hc-") {
return name
}
return "hc-" + name
}
func boilerplateURL(kind string) (string, error) {
switch strings.ToLower(strings.TrimSpace(kind)) {
case "system":
return systemBoilerplateURL, nil
case "payment":
return paymentBoilerplateURL, nil
default:
return "", fmt.Errorf("plugin type must be system or payment")
}
}
func initParent(cmd *cobra.Command) (string, error) {
dirFlag, _ := cmd.Flags().GetString("dir")
dirFlag = strings.TrimSpace(dirFlag)
if dirFlag == "" {
return os.Getwd()
}
return filepath.Abs(dirFlag)
}
type listedPlugin struct {
Name string
Version string
Path string
Text string
}
func runPluginList(cmd *cobra.Command) error {
roots, err := pluginListRoots(cmd)
if err != nil {
return err
}
for _, root := range roots {
print_status("Searching " + root)
}
rows, err := discoverLocalPlugins(roots)
if err != nil {
return err
}
if len(rows) == 0 {
print_status("No local plugins")
print_status("To search another directory, cd there or run: apito plugin --dir <path>")
return nil
}
if !syncStdinInteractive() {
fmt.Print(formatLocalPluginTable(rows))
return nil
}
row, ok, err := promptPlugin(rows)
if err != nil {
return err
}
if !ok {
print_status("Selection cancelled")
return nil
}
return runPluginAction(cmd, row)
}
func promptCancelled(err error) bool {
return errors.Is(err, promptui.ErrInterrupt) || errors.Is(err, promptui.ErrEOF) || errors.Is(err, promptui.ErrAbort)
}
func pluginPickText(row listedPlugin, showParent bool) string {
text := fmt.Sprintf("%s %s", row.Name, row.Version)
if showParent {
text += " " + filepath.Base(filepath.Dir(row.Path))
}
return text
}
func pluginPicks(rows []listedPlugin) []listedPlugin {
counts := map[string]int{}
for _, row := range rows {
counts[row.Name+"\x00"+row.Version]++
}
picks := make([]listedPlugin, len(rows))
for i, row := range rows {
row.Text = pluginPickText(row, counts[row.Name+"\x00"+row.Version] > 1)
picks[i] = row
}
return picks
}
func promptPlugin(rows []listedPlugin) (listedPlugin, bool, error) {
picks := pluginPicks(rows)
size := len(picks)
if size > 12 {
size = 12
}
prompt := promptui.Select{
Label: "Plugin",
Items: picks,
Size: size,
StartInSearchMode: true,
Templates: &promptui.SelectTemplates{
Active: "▸ {{ .Text | cyan }}",
Inactive: " {{ .Text }}",
Selected: "{{ .Text }}",
},
Searcher: func(input string, index int) bool {
return strings.Contains(strings.ToLower(picks[index].Text), strings.ToLower(strings.TrimSpace(input)))
},
}
index, _, err := prompt.Run()
if promptCancelled(err) {
return listedPlugin{}, false, nil
}
if err != nil {
return listedPlugin{}, false, err
}
return picks[index], true, nil
}
func runPluginAction(cmd *cobra.Command, row listedPlugin) error {
prompt := promptui.Select{
Label: row.Name + " " + row.Version,
Items: []string{"check", "build", "push"},
Size: 3,
}
_, action, err := prompt.Run()
if promptCancelled(err) {
print_status("Selection cancelled")
return nil
}
if err != nil {
return err
}
switch action {
case "check":
if err := checkPlugin(row.Path); err != nil {
return err
}
print_success("Plugin check passed: " + row.Path)
return nil
case "build":
buildPlugin(row.Path, cmd)
return nil
case "push":
return promptPluginPush(row)
default:
return fmt.Errorf("unknown plugin action %s", action)
}
}
func promptPluginPush(row listedPlugin) error {
next, err := bumpSemver(row.Version, "patch")
if err != nil {
return err
}
versionPrompt := promptui.Prompt{
Label: "Version",
Default: next,
Validate: func(value string) error {
chosen := strings.TrimPrefix(strings.TrimSpace(value), "v")
if _, err := parseSemver(chosen); err != nil {
return err
}
return requireNewerVersion(row.Version, chosen)
},
}
version, err := versionPrompt.Run()
if promptCancelled(err) {
print_status("Selection cancelled")
return nil
}
if err != nil {
return err
}
messagePrompt := promptui.Prompt{
Label: "Commit message",
Validate: func(value string) error {
if strings.TrimSpace(value) == "" {
return fmt.Errorf("commit message is required")
}
return nil
},
}
message, err := messagePrompt.Run()
if promptCancelled(err) {
print_status("Selection cancelled")
return nil
}
if err != nil {
return err
}
chosen := strings.TrimPrefix(strings.TrimSpace(version), "v")
return releasePluginVersion(row.Path, strings.TrimSpace(message), chosen)
}
func pluginListRoots(cmd *cobra.Command) ([]string, error) {
dirFlag, _ := cmd.Flags().GetString("dir")
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
return pluginListRootsFrom(strings.TrimSpace(dirFlag), cwd)
}
func pluginListRootsFrom(dirFlag, cwd string) ([]string, error) {
if dirFlag != "" {
abs, err := filepath.Abs(dirFlag)
if err != nil {
return nil, err
}
return []string{abs}, nil
}
roots := []string{cwd}
home, err := defaultPluginRoot()
if err != nil {
return roots, nil
}
if home != cwd {
roots = append(roots, home)
}
return roots, nil
}
func discoverLocalPlugins(roots []string) ([]listedPlugin, error) {
seen := map[string]struct{}{}
var rows []listedPlugin
var walk func(dir string, depth int)
walk = func(dir string, depth int) {
abs, err := filepath.Abs(dir)
if err != nil {
return
}
if _, ok := seen[abs]; ok {
return
}
seen[abs] = struct{}{}
if cfg, err := readPluginConfig(abs); err == nil && strings.TrimSpace(cfg.Plugin.ID) != "" {
rows = append(rows, listedPlugin{Name: cfg.Plugin.ID, Version: cfg.Plugin.Version, Path: abs})
return
}
if depth >= 2 {
return
}
entries, err := os.ReadDir(abs)
if err != nil {
return
}
for _, entry := range entries {
name := entry.Name()
if !entry.IsDir() || strings.HasPrefix(name, ".") || name == "node_modules" || name == "ui" || name == "vendor" {
continue
}
walk(filepath.Join(abs, name), depth+1)
}
}
for _, root := range roots {
walk(root, 0)
}
sort.Slice(rows, func(i, j int) bool {
if rows[i].Name == rows[j].Name {
return rows[i].Path < rows[j].Path
}
return rows[i].Name < rows[j].Name
})
return rows, nil
}
func formatLocalPluginTable(rows []listedPlugin) string {
var b strings.Builder
w := tabwriter.NewWriter(&b, 0, 0, 2, ' ', 0)
fmt.Fprintln(w, "NAME\tVERSION\tPATH")
for _, row := range rows {
fmt.Fprintf(w, "%s\t%s\t%s\n", row.Name, row.Version, row.Path)
}
_ = w.Flush()
return b.String()
}
func resolvePluginWorkdir(arg string) (string, error) {
arg = strings.TrimSpace(arg)
cwd, err := os.Getwd()
if err != nil {
return "", err
}
if arg != "" {
if strings.Contains(arg, "/") || strings.Contains(arg, string(os.PathSeparator)) {
abs, err := filepath.Abs(arg)
if err != nil {
return "", err
}
if _, err := readPluginConfig(abs); err != nil {
return "", err
}
return abs, nil
}
name := arg
if !strings.HasPrefix(name, "hc-") {
name = "hc-" + name
}
for _, root := range []string{cwd, mustDefaultPluginRoot()} {
dir := filepath.Join(root, name)
if _, err := readPluginConfig(dir); err == nil {
return dir, nil
}
if cfg, err := readPluginConfig(root); err == nil && (cfg.Plugin.ID == arg || cfg.Plugin.ID == name) {
return root, nil
}
}
return "", fmt.Errorf("plugin %s was not found in %s or %s", arg, cwd, mustDefaultPluginRoot())
}
if _, err := readPluginConfig(cwd); err == nil {
return cwd, nil
}
if id, err := solePluginID(cwd); err == nil {
return filepath.Join(cwd, id), nil
}
root := mustDefaultPluginRoot()
if id, err := solePluginID(root); err == nil {
return filepath.Join(root, id), nil
}
return "", fmt.Errorf("pass a plugin directory")
}
func mustDefaultPluginRoot() string {
root, err := defaultPluginRoot()
if err != nil {
return ""
}
return root
}
func checkPlugin(dir string) error {
cfg, err := readPluginConfig(dir)
if err != nil {
return err
}
var problems []string
id := strings.TrimSpace(cfg.Plugin.ID)
if !strings.HasPrefix(id, "hc-") {
problems = append(problems, "config.yml id must begin with hc-")
}
if strings.ToLower(strings.TrimSpace(cfg.Plugin.Language)) != "go" {
problems = append(problems, "config.yml language must be go")
}
version := strings.TrimSpace(cfg.Plugin.Version)
if _, err := parseSemver(version); err != nil {
problems = append(problems, "config.yml version: "+err.Error())
}
if strings.TrimSpace(cfg.Plugin.BinaryPath) == "" {
problems = append(problems, "config.yml binary_path is required")
}
repo := strings.TrimSpace(cfg.Plugin.RepositoryURL)
if !strings.Contains(strings.ToLower(repo), "github.com") {
problems = append(problems, "config.yml repository_url must be a github.com URL")
}
if strings.TrimSpace(cfg.Plugin.HandshakeConfig.MagicCookieKey) == "" || strings.TrimSpace(cfg.Plugin.HandshakeConfig.MagicCookieValue) == "" {
problems = append(problems, "config.yml handshake cookie is required")
}
if err := requireContribution(dir); err != nil {
problems = append(problems, err.Error())
}
for _, file := range []string{"main.go", "go.mod"} {
if _, err := os.Stat(filepath.Join(dir, file)); err != nil {
problems = append(problems, file+" is required")
}
}
mainPath := filepath.Join(dir, "main.go")
if main, err := os.ReadFile(mainPath); err == nil {
foundID, foundVersion := sdkInitIdentity(string(main))
if foundID != id || foundVersion != version {
problems = append(problems, fmt.Sprintf("sdk.Init(%q, %q) must match config.yml id %q version %q", foundID, foundVersion, id, version))
}
}
workflow := filepath.Join(dir, ".github", "workflows", "build-release.yml")
body, err := os.ReadFile(workflow)
if err != nil {
problems = append(problems, ".github/workflows/build-release.yml is required")
} else {
text := string(body)
for _, needle := range []string{"v*", "go test", "config.yml", "sdk.Init"} {
if !strings.Contains(text, needle) {
problems = append(problems, "build-release.yml must contain "+needle)
}
}
}
if cfg.Plugin.UIConfig != nil && strings.TrimSpace(cfg.Plugin.UIConfig.EntryPath) != "" {
if info, err := os.Stat(filepath.Join(dir, "ui")); err != nil || !info.IsDir() {
problems = append(problems, "ui/ is required when ui_config.entry_path is set")
}
entry := filepath.Join(dir, cfg.Plugin.UIConfig.EntryPath)
if _, err := os.Stat(entry); err != nil {
problems = append(problems, cfg.Plugin.UIConfig.EntryPath+" is required when ui_config.entry_path is set")
}
}
origin, err := gitOrigin(dir)
if err != nil || !strings.Contains(strings.ToLower(origin), "github.com") {
problems = append(problems, "git origin must be a github.com remote")
}
if len(problems) == 0 {
return nil
}
return fmt.Errorf("%s", strings.Join(problems, "\n"))
}
func requireContribution(dir string) error {
raw, err := os.ReadFile(filepath.Join(dir, "config.yml"))
if err != nil {
return err
}
var doc map[string]any
if err := yaml.Unmarshal(raw, &doc); err != nil {
return err
}
plugin, _ := doc["plugin"].(map[string]any)
contrib, _ := plugin["contributions"].(map[string]any)
if contrib == nil {
return fmt.Errorf("config.yml contributions are required")
}
if _, ok := contrib["api"]; ok {
return nil
}
if _, ok := contrib["payment"]; ok {
return nil
}
return fmt.Errorf("config.yml needs an api or payment contribution")
}
var sdkInitPattern = regexp.MustCompile(`sdk\.Init\(\s*"([^"]*)"\s*,\s*"([^"]*)"`)
func sdkInitIdentity(source string) (string, string) {
match := sdkInitPattern.FindStringSubmatch(source)
if len(match) != 3 {
return "", ""
}
return match[1], match[2]
}
func gitOrigin(dir string) (string, error) {
cmd := exec.Command("git", "remote", "get-url", "origin")
cmd.Dir = dir
out, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}
func runPluginPush(cmd *cobra.Command, arg string) error {
dir, err := resolvePluginWorkdir(arg)
if err != nil {
return err
}
message, _ := cmd.Flags().GetString("message")
minor, _ := cmd.Flags().GetBool("minor")
major, _ := cmd.Flags().GetBool("major")
if minor && major {
return fmt.Errorf("pass only one of --minor or --major")
}
kind := "patch"
if minor {
kind = "minor"
}
if major {
kind = "major"
}
return pushPluginRelease(dir, strings.TrimSpace(message), kind)
}
func pushPluginRelease(dir, message, kind string) error {
if message == "" {
return fmt.Errorf("pass -m with the commit message")
}
cfg, err := readPluginConfig(dir)
if err != nil {
return err
}
next, err := bumpSemver(cfg.Plugin.Version, kind)
if err != nil {
return err
}
return releasePluginVersion(dir, message, next)
}
func releasePluginVersion(dir, message, version string) error {
if message == "" {
return fmt.Errorf("pass -m with the commit message")
}
version = strings.TrimPrefix(strings.TrimSpace(version), "v")
if _, err := parseSemver(version); err != nil {
return err
}
if err := checkPlugin(dir); err != nil {
return err
}
cfg, err := readPluginConfig(dir)
if err != nil {
return err
}
if err := requireNewerVersion(cfg.Plugin.Version, version); err != nil {
return err
}
if err := writePluginVersion(dir, version); err != nil {
return err
}
if err := gitCommitTagPush(dir, version, message); err != nil {
return err
}
print_success("Pushed " + cfg.Plugin.ID + " " + version)
return nil
}
func requireNewerVersion(current, next string) error {
cur, err := parseSemver(current)
if err != nil {
return err
}
nxt, err := parseSemver(next)
if err != nil {
return err
}
if nxt[0] > cur[0] || (nxt[0] == cur[0] && (nxt[1] > cur[1] || (nxt[1] == cur[1] && nxt[2] > cur[2]))) {
return nil
}
return fmt.Errorf("%s is not newer than %s", strings.TrimPrefix(strings.TrimSpace(next), "v"), strings.TrimPrefix(strings.TrimSpace(current), "v"))
}
func parseSemver(version string) ([3]int, error) {
version = strings.TrimPrefix(strings.TrimSpace(version), "v")
parts := strings.Split(version, ".")
if len(parts) != 3 {
return [3]int{}, fmt.Errorf("%q is not major.minor.patch", version)
}
var parsed [3]int
for i, part := range parts {
n, err := strconv.Atoi(part)
if err != nil || n < 0 {
return [3]int{}, fmt.Errorf("%q is not major.minor.patch", version)
}
parsed[i] = n
}
return parsed, nil
}
func bumpSemver(version, kind string) (string, error) {
parsed, err := parseSemver(version)
if err != nil {
return "", err
}
switch kind {
case "major":
parsed[0]++
parsed[1] = 0
parsed[2] = 0
case "minor":
parsed[1]++
parsed[2] = 0
default:
parsed[2]++
}
return fmt.Sprintf("%d.%d.%d", parsed[0], parsed[1], parsed[2]), nil
}
func writePluginVersion(dir, version string) error {
configPath := filepath.Join(dir, "config.yml")
raw, err := os.ReadFile(configPath)
if err != nil {
return err
}
replaced := replaceVersionField(string(raw), version)
if replaced == string(raw) {
return fmt.Errorf("config.yml version field was not updated")
}
if err := os.WriteFile(configPath, []byte(replaced), 0o644); err != nil {
return err
}
mainPath := filepath.Join(dir, "main.go")
main, err := os.ReadFile(mainPath)
if err != nil {
return err
}
updated := sdkInitPattern.ReplaceAllString(string(main), fmt.Sprintf(`sdk.Init("$1", "%s"`, version))
if updated == string(main) {
return fmt.Errorf("sdk.Init version was not updated")
}
return os.WriteFile(mainPath, []byte(updated), 0o644)
}
var configVersionPattern = regexp.MustCompile(`(?m)^([ \t]*version:[ \t]*)"?([0-9]+\.[0-9]+\.[0-9]+)"?`)
func replaceVersionField(source, version string) string {
return configVersionPattern.ReplaceAllString(source, `${1}"`+version+`"`)
}
func gitCommitTagPush(dir, version, message string) error {
tag := "v" + version
steps := [][]string{
{"git", "add", "-A", "--", "."},
{"git", "commit", "-m", message},
{"git", "tag", tag},
{"git", "push", "-u", "origin", "HEAD"},
{"git", "push", "origin", tag},
}
for _, step := range steps {
cmd := exec.Command(step[0], step[1:]...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%s: %w\n%s", strings.Join(step, " "), err, strings.TrimSpace(string(out)))
}
}
return nil
}