@@ -5,13 +5,18 @@ import (
5
5
"errors"
6
6
"fmt"
7
7
"io"
8
+ "log/slog"
8
9
"os"
10
+ "strings"
9
11
12
+ "github.com/go-git/go-billy/v5"
10
13
"github.com/rancher/charts-build-scripts/pkg/filesystem"
11
14
"github.com/rancher/charts-build-scripts/pkg/git"
12
15
"github.com/rancher/charts-build-scripts/pkg/lifecycle"
16
+ "github.com/rancher/charts-build-scripts/pkg/logger"
13
17
"github.com/rancher/charts-build-scripts/pkg/path"
14
18
"gopkg.in/yaml.v3"
19
+ helmChartutil "helm.sh/helm/v3/pkg/chartutil"
15
20
)
16
21
17
22
// Release holds necessary metadata to release a chart version
@@ -154,3 +159,55 @@ func (r *Release) UpdateReleaseYaml() error {
154
159
155
160
return nil
156
161
}
162
+
163
+ // PullIcon will pull the icon from the chart and save it to the local assets/logos directory
164
+ func (r * Release ) PullIcon (ctx context.Context , rootFs billy.Filesystem ) error {
165
+ logger .Log (ctx , slog .LevelInfo , "starting to pull icon process" )
166
+
167
+ // Get Chart.yaml path and load it
168
+ chartYamlPath := path .RepositoryChartsDir + "/" + r .Chart + "/" + r .ChartVersion + "/Chart.yaml"
169
+ absChartPath := filesystem .GetAbsPath (rootFs , chartYamlPath )
170
+
171
+ // Load Chart.yaml file
172
+ chartMetadata , err := helmChartutil .LoadChartfile (absChartPath )
173
+ if err != nil {
174
+ return fmt .Errorf ("could not load %s: %s" , chartYamlPath , err )
175
+ }
176
+
177
+ logger .Log (ctx , slog .LevelDebug , "checking if chart has downloaded icon" )
178
+ iconField := chartMetadata .Icon
179
+
180
+ // Check file prefix if it is a URL just skip this process
181
+ if ! strings .HasPrefix (iconField , "file://" ) {
182
+ logger .Log (ctx , slog .LevelInfo , "icon path is not a file:// prefix" )
183
+ return nil
184
+ }
185
+
186
+ relativeIconPath , _ := strings .CutPrefix (iconField , "file://" )
187
+
188
+ // Check if icon is already present
189
+ exists , err := filesystem .PathExists (ctx , rootFs , relativeIconPath )
190
+ if err != nil {
191
+ return err
192
+ }
193
+
194
+ // Icon is already present, no need to pull it
195
+ if exists {
196
+ logger .Log (ctx , slog .LevelDebug , "icon already exists" )
197
+ return nil
198
+ }
199
+
200
+ // Check if the icon exists in the dev branch
201
+ if err := r .git .CheckFileExists (relativeIconPath , r .VR .DevBranch ); err != nil {
202
+ logger .Log (ctx , slog .LevelError , "icon file not found in dev branch but should" , slog .String ("icon" , relativeIconPath ), logger .Err (err ))
203
+ return fmt .Errorf ("icon file not found in dev branch but should: %w" , err )
204
+ }
205
+
206
+ // checkout the icon file from the dev branch
207
+ if err := r .git .CheckoutFile (r .VR .DevBranch , relativeIconPath ); err != nil {
208
+ return err
209
+ }
210
+
211
+ // git reset return
212
+ return r .git .ResetHEAD ()
213
+ }
0 commit comments