Skip to content

Commit 47caf9b

Browse files
committed
chore: simplify logic
Signed-off-by: juan131 <juan.ariza@broadcom.com>
1 parent 0eaba61 commit 47caf9b

File tree

7 files changed

+54
-54
lines changed

7 files changed

+54
-54
lines changed

cmd/dt/carvelize/carvelize.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ func GenerateBundle(chartPath string, opts ...chartutils.Option) error {
101101

102102
imgPkgPath := filepath.Join(chartPath, ".imgpkg")
103103
if !utils.FileExists(imgPkgPath) {
104-
if mkdirErr := os.Mkdir(imgPkgPath, os.FileMode(0755)); mkdirErr != nil {
105-
return fmt.Errorf("failed to create .imgpkg directory: %w", mkdirErr)
104+
if err = os.Mkdir(imgPkgPath, os.FileMode(0755)); err != nil {
105+
return fmt.Errorf("failed to create .imgpkg directory: %w", err)
106106
}
107107
}
108108

cmd/dt/unwrap/unwrap.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -411,31 +411,33 @@ func normalizeOCIURL(url string) string {
411411
}
412412

413413
func pushChart(ctx context.Context, wrap wrapping.Wrap, pushChartURL string, cfg *Config) error {
414-
tmpDir, tmpErr := cfg.GetTemporaryDirectory()
415-
if tmpErr != nil {
416-
return fmt.Errorf("failed to get temp dir: %w", tmpErr)
414+
var tmpDir, dir string
415+
var err error
416+
tmpDir, err = cfg.GetTemporaryDirectory()
417+
if err != nil {
418+
return fmt.Errorf("failed to get temp dir: %w", err)
417419
}
418420

419-
dir, mkdirErr := os.MkdirTemp(tmpDir, "chart-*")
420-
if mkdirErr != nil {
421-
return fmt.Errorf("failed to create temp directory: %w", mkdirErr)
421+
dir, err = os.MkdirTemp(tmpDir, "chart-*")
422+
if err != nil {
423+
return fmt.Errorf("failed to create temp directory: %w", err)
422424
}
423425

424426
chart := wrap.Chart()
425427
chartPath := chart.RootDir()
426428
tempTarFile := filepath.Join(dir, fmt.Sprintf("%s.tgz", chart.Name()))
427-
if err := utils.Tar(chartPath, tempTarFile, utils.TarConfig{
429+
if err = utils.Tar(chartPath, tempTarFile, utils.TarConfig{
428430
Prefix: chart.Name(),
429431
}); err != nil {
430432
return fmt.Errorf("failed to untar filename %q: %w", chartPath, err)
431433
}
432434

433-
tmpDir, tmpErr = cfg.GetTemporaryDirectory()
434-
if tmpErr != nil {
435-
return fmt.Errorf("failed to get temp dir: %w", tmpErr)
435+
tmpDir, err = cfg.GetTemporaryDirectory()
436+
if err != nil {
437+
return fmt.Errorf("failed to get temp dir: %w", err)
436438
}
437439

438-
if err := artifacts.PushChart(tempTarFile, pushChartURL,
440+
if err = artifacts.PushChart(tempTarFile, pushChartURL,
439441
artifacts.WithInsecure(cfg.Insecure),
440442
artifacts.WithPlainHTTP(cfg.UsePlainHTTP),
441443
artifacts.WithRegistryAuth(cfg.Auth.Username, cfg.Auth.Password),

cmd/dt/wrap/wrap.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,18 +220,17 @@ func ResolveInputChartPath(inputPath string, cfg *Config) (string, error) {
220220
l := cfg.GetLogger()
221221
var chartPath string
222222

223-
tmpDir, tmpErr := cfg.GetTemporaryDirectory()
224-
if tmpErr != nil {
225-
return "", fmt.Errorf("failed to create temporary directory: %w", tmpErr)
223+
tmpDir, err := cfg.GetTemporaryDirectory()
224+
if err != nil {
225+
return "", fmt.Errorf("failed to create temporary directory: %w", err)
226226
}
227227

228228
if chartutils.IsRemoteChart(inputPath) {
229-
if err := l.ExecuteStep("Fetching remote Helm chart", func() error {
230-
var fetchErr error
229+
if err = l.ExecuteStep("Fetching remote Helm chart", func() error {
231230
version := cfg.Version
232-
chartPath, fetchErr = fetchRemoteChart(inputPath, version, tmpDir, cfg)
233-
if fetchErr != nil {
234-
return fetchErr
231+
chartPath, err = fetchRemoteChart(inputPath, version, tmpDir, cfg)
232+
if err != nil {
233+
return err
235234
}
236235

237236
return nil
@@ -391,8 +390,8 @@ func wrapChart(inputPath string, opts ...Option) (string, error) {
391390

392391
if cfg.ShouldFetchChartArtifacts(inputPath) {
393392
chartURL := fmt.Sprintf("%s:%s", inputPath, chart.Version())
394-
if fetchErr := fetchArtifacts(chartURL, filepath.Join(wrap.RootDir(), artifacts.HelmChartArtifactMetadataDir), subCfg); fetchErr != nil {
395-
return "", fetchErr
393+
if err = fetchArtifacts(chartURL, filepath.Join(wrap.RootDir(), artifacts.HelmChartArtifactMetadataDir), subCfg); err != nil {
394+
return "", err
396395
}
397396
}
398397

pkg/artifacts/helm.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ func getRegistryClientWrap(cfg *RegistryClientConfig) (*registryClientWrap, erro
139139

140140
// PullChart retrieves the specified chart
141141
func PullChart(chartURL, version string, destDir string, opts ...RegistryClientOption) (string, error) {
142-
u, urlErr := url.Parse(chartURL)
143-
if urlErr != nil {
144-
return "", fmt.Errorf("invalid url: %w", urlErr)
142+
u, err := url.Parse(chartURL)
143+
if err != nil {
144+
return "", fmt.Errorf("invalid url: %w", err)
145145
}
146146
cfg := &action.Configuration{}
147147
cc := NewRegistryClientConfig(opts...)
@@ -151,7 +151,7 @@ func PullChart(chartURL, version string, destDir string, opts ...RegistryClientO
151151
}
152152
cfg.RegistryClient = reg.client
153153
if cc.Auth.Username != "" && cc.Auth.Password != "" && reg.credentialsFile != "" {
154-
if err := reg.client.Login(u.Host, registry.LoginOptBasicAuth(cc.Auth.Username, cc.Auth.Password)); err != nil {
154+
if err = reg.client.Login(u.Host, registry.LoginOptBasicAuth(cc.Auth.Username, cc.Auth.Password)); err != nil {
155155
return "", fmt.Errorf("error logging in to %s: %w", u.Host, err)
156156
}
157157
defer func() {
@@ -161,21 +161,21 @@ func PullChart(chartURL, version string, destDir string, opts ...RegistryClientO
161161
}
162162
client := action.NewPullWithOpts(action.WithConfig(cfg))
163163

164-
dir, mkdirErr := os.MkdirTemp(destDir, "chart-*")
165-
if mkdirErr != nil {
166-
return "", fmt.Errorf("failed to upload Helm chart: failed to create temp directory: %w", mkdirErr)
164+
dir, err := os.MkdirTemp(destDir, "chart-*")
165+
if err != nil {
166+
return "", fmt.Errorf("failed to upload Helm chart: failed to create temp directory: %w", err)
167167
}
168168
client.Settings = cli.New()
169169
client.DestDir = dir
170170
client.Untar = true
171171
client.Version = version
172-
if _, err := client.Run(chartURL); err != nil {
172+
if _, err = client.Run(chartURL); err != nil {
173173
return "", fmt.Errorf("failed to pull Helm chart: %w", err)
174174
}
175175

176-
charts, globErr := filepath.Glob(filepath.Join(dir, "*/Chart.yaml"))
177-
if globErr != nil {
178-
return "", fmt.Errorf("failed to located fetched Helm charts: %w", globErr)
176+
charts, err := filepath.Glob(filepath.Join(dir, "*/Chart.yaml"))
177+
if err != nil {
178+
return "", fmt.Errorf("failed to located fetched Helm charts: %w", err)
179179
}
180180
if len(charts) == 0 {
181181
return "", fmt.Errorf("cannot find any Helm chart")
@@ -212,10 +212,10 @@ func showRemoteHelmChart(chartURL string, version string, cfg *RegistryClientCon
212212
client.SetRegistryClient(reg.client)
213213
client.Version = version
214214
cp, err := client.LocateChart(chartURL, cli.New())
215-
216215
if err != nil {
217216
return "", err
218217
}
218+
219219
return client.Run(cp)
220220
}
221221

pkg/chartutils/images.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ func PushImages(lock *imagelock.ImagesLock, imagesDir string, opts ...Option) er
205205
}
206206

207207
func loadImage(path string) (v1.Image, error) {
208-
stat, statErr := os.Stat(path)
209-
if statErr != nil {
210-
return nil, statErr
208+
stat, err := os.Stat(path)
209+
if err != nil {
210+
return nil, err
211211
}
212212

213213
if !stat.IsDir() {
@@ -218,9 +218,9 @@ func loadImage(path string) (v1.Image, error) {
218218
return img, nil
219219
}
220220

221-
l, layoutErr := layout.ImageIndexFromPath(path)
222-
if layoutErr != nil {
223-
return nil, fmt.Errorf("could load %q as OCI layout: %w", path, layoutErr)
221+
l, err := layout.ImageIndexFromPath(path)
222+
if err != nil {
223+
return nil, fmt.Errorf("could load %q as OCI layout: %w", path, err)
224224
}
225225
m, err := l.IndexManifest()
226226
if err != nil {

pkg/relocator/chart.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func relocateChart(chart *cu.Chart, prefix string, cfg *RelocateConfig) error {
3838

3939
for _, result := range valuesReplRes {
4040
if result.Count > 0 {
41-
if writeErr := os.WriteFile(chart.AbsFilePath(result.Name), result.Data, 0644); writeErr != nil {
42-
return fmt.Errorf("failed to write %s: %v", result.Name, writeErr)
41+
if err = os.WriteFile(chart.AbsFilePath(result.Name), result.Data, 0644); err != nil {
42+
return fmt.Errorf("failed to write %s: %v", result.Name, err)
4343
}
4444
}
4545
}
@@ -51,10 +51,10 @@ func relocateChart(chart *cu.Chart, prefix string, cfg *RelocateConfig) error {
5151
} else {
5252
if annotationsRelocResult.Count > 0 {
5353
annotationsKeyPath := fmt.Sprintf("$.annotations['%s']", cfg.ImageLockConfig.AnnotationsKey)
54-
if yamlErr := utils.YamlFileSet(chart.AbsFilePath("Chart.yaml"), map[string]string{
54+
if err = utils.YamlFileSet(chart.AbsFilePath("Chart.yaml"), map[string]string{
5555
annotationsKeyPath: string(annotationsRelocResult.Data),
56-
}); yamlErr != nil {
57-
allErrors = errors.Join(allErrors, fmt.Errorf("failed to relocate Helm chart: failed to write annotations: %v", yamlErr))
56+
}); err != nil {
57+
allErrors = errors.Join(allErrors, fmt.Errorf("failed to relocate Helm chart: failed to write annotations: %v", err))
5858
}
5959
}
6060
}

pkg/relocator/values.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,21 @@ import (
1010
)
1111

1212
func relocateValuesData(valuesFile string, valuesData []byte, prefix string) (*RelocationResult, error) {
13-
valuesMap, readErr := chartutil.ReadValues(valuesData)
14-
if readErr != nil {
15-
return nil, fmt.Errorf("failed to parse Helm chart values: %v", readErr)
13+
valuesMap, err := chartutil.ReadValues(valuesData)
14+
if err != nil {
15+
return nil, fmt.Errorf("failed to parse Helm chart values: %v", err)
1616
}
17-
imageElems, findErr := cu.FindImageElementsInValuesMap(valuesMap)
18-
if findErr != nil {
19-
return nil, fmt.Errorf("failed to find Helm chart image elements from values.yaml: %v", findErr)
17+
imageElems, err := cu.FindImageElementsInValuesMap(valuesMap)
18+
if err != nil {
19+
return nil, fmt.Errorf("failed to find Helm chart image elements from values.yaml: %v", err)
2020
}
2121
if len(imageElems) == 0 {
2222
return &RelocationResult{Data: valuesData, Count: 0}, nil
2323
}
2424

2525
data := make(map[string]string, 0)
2626
for _, e := range imageElems {
27-
err := e.Relocate(prefix)
28-
if err != nil {
27+
if err = e.Relocate(prefix); err != nil {
2928
return nil, fmt.Errorf("unexpected error relocating: %v", err)
3029
}
3130
for k, v := range e.YamlReplaceMap() {

0 commit comments

Comments
 (0)