Skip to content

Commit

Permalink
fix: use custom loadFile function to create necessary dir/file before…
Browse files Browse the repository at this point in the history
… helm exec

Signed-off-by: Ardika Bagus <[email protected]>
  • Loading branch information
ardikabs committed Nov 10, 2023
1 parent 254c83d commit 89ee207
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions pkg/helm/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helm

import (
"context"
"errors"
"fmt"
"os"

Expand All @@ -10,7 +11,6 @@ import (
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/repo"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/utils/ptr"
)

Expand Down Expand Up @@ -44,14 +44,8 @@ func (r HelmRepo) Init(ctx context.Context, client *action.Install) error {
repoName := ptr.Deref[string](r.Name, "")
errChan := make(chan error)
go func(name, url string) {
b, err := os.ReadFile(settings.RepositoryConfig)
if err != nil && !os.IsNotExist(err) {
errChan <- err
return
}

var repoFile repo.File
if err := yaml.Unmarshal(b, &repoFile); err != nil {
repoFile, err := r.loadFile(settings.RepositoryConfig)
if err != nil {
errChan <- err
return
}
Expand Down Expand Up @@ -90,6 +84,19 @@ func (r HelmRepo) Init(ctx context.Context, client *action.Install) error {
}
}

func (r HelmRepo) loadFile(path string) (file *repo.File, err error) {
file, err = repo.LoadFile(path)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return
}

if err = file.WriteFile(path, 0644); err != nil {
return
}

return
}

// NameAndChart returns the name and chart that should be used.
// The precedence during the NameAndChart generation are as follows:
// 1. Path will be used when available
Expand Down

0 comments on commit 89ee207

Please sign in to comment.