@@ -24,6 +24,24 @@ const (
24
24
repoName = "cluster-api-operator"
25
25
)
26
26
27
+ // chartInfo represents information about a chart to be processed
28
+ type chartInfo struct {
29
+ name string
30
+ description string
31
+ }
32
+
33
+ // List of charts to process
34
+ var charts = []chartInfo {
35
+ {
36
+ name : "cluster-api-operator" ,
37
+ description : "Cluster API Operator" ,
38
+ },
39
+ {
40
+ name : "cluster-api-operator-providers" ,
41
+ description : "Cluster API Provider Custom Resources" ,
42
+ },
43
+ }
44
+
27
45
func main () {
28
46
fmt .Println ("🚀 Starting index.yaml update tool" )
29
47
@@ -38,45 +56,66 @@ func main() {
38
56
39
57
fmt .Println ("⚙️ Loading index.yaml file from repo root directory" )
40
58
41
- indexFile := loadIndexFile (tag )
59
+ indexFile := loadIndexFile ()
60
+
61
+ fmt .Println ("🔎 Finding chart archives in release assets" )
42
62
43
- fmt .Println ("🔎 Finding chart archive in release assets" )
63
+ // Get all release assets once
64
+ releaseAssets := getReleaseAssets (tag )
44
65
45
- chartAsset := findChartReleaseAsset (tag )
66
+ // Process each chart
67
+ processedCharts := 0
68
+ for _ , chartInfo := range charts {
69
+ fmt .Printf ("\n 📊 Processing chart: %s\n " , chartInfo .name )
46
70
47
- fmt .Println ("📦 Downloading chart archive to a temp directory" )
71
+ // Check if chart already exists in index
72
+ if _ , err := indexFile .Get (chartInfo .name , tag [1 :]); err == nil {
73
+ fmt .Printf ("✅ Chart %s already exists in index file, skipping\n " , chartInfo .name )
74
+ continue
75
+ }
48
76
49
- archivePath , chart := downloadChart (chartAsset )
77
+ // Find chart asset
78
+ chartAsset := findChartAsset (releaseAssets , chartInfo .name , tag )
79
+ if chartAsset == nil {
80
+ fmt .Printf ("⚠️ Chart archive for %s not found in release assets, skipping\n " , chartInfo .name )
81
+ continue
82
+ }
83
+
84
+ fmt .Printf ("📦 Downloading %s chart archive to a temp directory\n " , chartInfo .name )
85
+ archivePath , chart := downloadChart (chartAsset )
86
+
87
+ fmt .Printf ("👉🏻 Adding %s entry to index.yaml\n " , chartInfo .name )
88
+ addEntryToIndexFile (indexFile , chartAsset , archivePath , chart )
89
+
90
+ processedCharts ++
91
+ }
50
92
51
- fmt .Println ("👉🏻 Adding entry to index.yaml" )
52
- addEntryToIndexFile (indexFile , chartAsset , archivePath , chart )
93
+ if processedCharts == 0 {
94
+ fmt .Println ("\n ⚠️ No new charts were added to index.yaml" )
95
+ os .Exit (0 )
96
+ }
53
97
54
- fmt .Println ("📝 Writing index.yaml file to repo root directory" )
98
+ fmt .Println ("\n 📝 Writing index.yaml file to repo root directory" )
55
99
56
100
if err := indexFile .WriteFile (indexFilePath , 0644 ); err != nil {
57
101
fmt .Println ("❌ Error writing index file: " , err )
58
102
os .Exit (1 )
59
103
}
60
104
61
- fmt .Println ( " ✅ Done updating index.yaml file" )
105
+ fmt .Printf ( " \n ✅ Done updating index.yaml file. Added %d chart(s) \n " , processedCharts )
62
106
}
63
107
64
- func loadIndexFile (tag string ) * repo.IndexFile {
108
+ func loadIndexFile () * repo.IndexFile {
65
109
indexFile , err := repo .LoadIndexFile (indexFilePath )
66
110
if err != nil {
67
111
fmt .Println ("❌ Error loading index file: " , err )
68
112
os .Exit (1 )
69
113
}
70
114
71
- if _ , err := indexFile .Get (repoName , tag [1 :]); err == nil {
72
- fmt .Println ("✅ Chart already exists in index file, no need to update" )
73
- os .Exit (0 )
74
- }
75
-
76
115
return indexFile
77
116
}
78
117
79
- func findChartReleaseAsset (tag string ) * github.ReleaseAsset {
118
+ func getReleaseAssets (tag string ) [] * github.ReleaseAsset {
80
119
ghClient := github .NewClient (nil )
81
120
82
121
release , _ , err := ghClient .Repositories .GetReleaseByTag (context .TODO (), gitHubOrgName , repoName , tag )
@@ -85,22 +124,19 @@ func findChartReleaseAsset(tag string) *github.ReleaseAsset {
85
124
os .Exit (1 )
86
125
}
87
126
88
- chartAsset := & github.ReleaseAsset {}
89
- found := false
90
- for _ , asset := range release .Assets {
91
- if * asset .Name == fmt .Sprintf ("%s-%s.tgz" , repoName , tag [1 :]) {
92
- chartAsset = asset
93
- found = true
94
- break
95
- }
96
- }
127
+ return release .Assets
128
+ }
97
129
98
- if ! found {
99
- fmt .Printf ("❌ Chart archive not found in release assets for release %s, please check if release was published correctly\n " , tag )
100
- os .Exit (1 )
130
+ func findChartAsset (assets []* github.ReleaseAsset , chartName , tag string ) * github.ReleaseAsset {
131
+ expectedFileName := fmt .Sprintf ("%s-%s.tgz" , chartName , tag [1 :])
132
+
133
+ for _ , asset := range assets {
134
+ if * asset .Name == expectedFileName {
135
+ return asset
136
+ }
101
137
}
102
138
103
- return chartAsset
139
+ return nil
104
140
}
105
141
106
142
func downloadChart (chartAsset * github.ReleaseAsset ) (string , * chart.Chart ) {
0 commit comments