-
Notifications
You must be signed in to change notification settings - Fork 582
/
codegen.go
71 lines (62 loc) · 1.56 KB
/
codegen.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/coreos/go-semver/semver"
"github.com/rancher/rke/metadata"
)
const (
defaultDevURL = "https://releases.rancher.com/kontainer-driver-metadata/dev-v2.9/data.json"
defaultReleaseURL = "https://releases.rancher.com/kontainer-driver-metadata/release-v2.9/data.json"
dataFile = "data/data.json"
)
// Codegen fetch data.json from defaultURL or defaultReleaseURL and generates bindata
func main() {
u := getURL()
fmt.Printf("Reading data from %s \n", u)
data, err := http.Get(u)
if err != nil {
panic(fmt.Errorf("failed to fetch data.json from kontainer-driver-metadata repository"))
}
defer data.Body.Close()
b, err := io.ReadAll(data.Body)
if err != nil {
panic(err)
}
var jsonData map[string]interface{}
err = json.Unmarshal(b, &jsonData)
if err != nil {
panic(fmt.Errorf("failed to parse JSON: %v", err))
}
// rke doesn't need info about rke2 and k3s versions
delete(jsonData, "rke2")
delete(jsonData, "k3s")
b, err = json.Marshal(jsonData)
if err != nil {
panic(fmt.Errorf("failed to marshal json data: %v", err))
}
fmt.Println("Writing data")
if err := os.WriteFile(dataFile, b, 0755); err != nil {
return
}
}
func getURL() string {
u := os.Getenv(metadata.RancherMetadataURLEnv)
if u == "" {
u = defaultDevURL
tag := os.Getenv("TAG")
if strings.HasPrefix(tag, "v") {
tag = tag[1:]
}
if v, err := semver.NewVersion(tag); err == nil {
if v.PreRelease == "" && v.String() != "" {
u = defaultReleaseURL
}
}
}
return u
}