-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathmain.go
More file actions
126 lines (108 loc) · 3.22 KB
/
Copy pathmain.go
File metadata and controls
126 lines (108 loc) · 3.22 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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"regexp"
"knative.dev/func/hack/cmd/shared"
)
const (
cePomPath = "templates/quarkus/cloudevents/pom.xml"
httpPomPath = "templates/quarkus/http/pom.xml"
quarkusPlatformAPI = "https://code.quarkus.io/api/platforms"
quarkusVersionTag = "quarkus.platform.version"
quarkusVersionExpr = `<quarkus\.platform\.version>([^<]+)</quarkus\.platform\.version>`
)
var quarkusVersionRe = regexp.MustCompile(quarkusVersionExpr)
type quarkusPlatformResponse struct {
Platforms []struct {
Streams []struct {
Releases []struct {
QuarkusCoreVersion string `json:"quarkusCoreVersion"`
} `json:"releases"`
} `json:"streams"`
} `json:"platforms"`
}
func main() {
ctx, stop := shared.NotifyContext(context.Background())
defer stop()
if err := run(ctx); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
os.Exit(1)
}
}
func run(ctx context.Context) error {
latestVersion, err := getLatestQuarkusVersion(ctx)
if err != nil {
return fmt.Errorf("cannot get latest Quarkus platform version: %w", err)
}
fmt.Printf("Latest Quarkus platform version: %s\n", latestVersion)
ceVersion, err := versionFromPOM(cePomPath)
if err != nil {
return fmt.Errorf("cannot read version from %s: %w", cePomPath, err)
}
httpVersion, err := versionFromPOM(httpPomPath)
if err != nil {
return fmt.Errorf("cannot read version from %s: %w", httpPomPath, err)
}
if ceVersion == latestVersion && httpVersion == latestVersion {
fmt.Println("Quarkus platform is up-to-date!")
return nil
}
for _, pomPath := range []string{cePomPath, httpPomPath} {
if err := updatePOM(pomPath, latestVersion); err != nil {
return fmt.Errorf("cannot update %s: %w", pomPath, err)
}
}
return nil
}
func getLatestQuarkusVersion(ctx context.Context) (string, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, quarkusPlatformAPI, nil)
if err != nil {
return "", err
}
resp, err := shared.HTTPClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected status %d from %s", resp.StatusCode, quarkusPlatformAPI)
}
var data quarkusPlatformResponse
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return "", err
}
if len(data.Platforms) == 0 ||
len(data.Platforms[0].Streams) == 0 ||
len(data.Platforms[0].Streams[0].Releases) == 0 {
return "", fmt.Errorf("unexpected response structure from Quarkus platform API")
}
v := data.Platforms[0].Streams[0].Releases[0].QuarkusCoreVersion
if v == "" {
return "", fmt.Errorf("quarkusCoreVersion is empty in API response")
}
return v, nil
}
func versionFromPOM(path string) (string, error) {
data, err := os.ReadFile(path)
if err != nil {
return "", err
}
m := quarkusVersionRe.FindSubmatch(data)
if len(m) < 2 {
return "", fmt.Errorf("cannot find <%s> in %s", quarkusVersionTag, path)
}
return string(m[1]), nil
}
func updatePOM(path, newVersion string) error {
data, err := os.ReadFile(path)
if err != nil {
return err
}
updated := quarkusVersionRe.ReplaceAll(data,
[]byte(fmt.Sprintf("<%s>%s</%s>", quarkusVersionTag, newVersion, quarkusVersionTag)))
return os.WriteFile(path, updated, 0644)
}