Skip to content

Commit

Permalink
find only directories that contain Chart.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
pete911 committed Jan 14, 2022
1 parent ebfabc5 commit ebebaac
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -37,7 +37,7 @@ running hcr.
```
Usage of hcr:
-charts-dir string
The Helm charts location (default "charts")
The Helm charts location, can be specific chart (default "charts")
-dry-run
Whether to skip release update gh-pages index update
-pages-branch string
Expand Down
2 changes: 1 addition & 1 deletion internal/flag/flag.go
Expand Up @@ -24,7 +24,7 @@ func ParseFlags() (hcr.Config, error) {
var f flags

flagSet.StringVar(&f.pagesBranch, "pages-branch", getStringEnv("HCR_PAGES_BRANCH", "gh-pages"), "The GitHub pages branch")
flagSet.StringVar(&f.chartsDir, "charts-dir", getStringEnv("HCR_CHARTS_DIR", "charts"), "The Helm charts location")
flagSet.StringVar(&f.chartsDir, "charts-dir", getStringEnv("HCR_CHARTS_DIR", "charts"), "The Helm charts location, can be specific chart")
flagSet.BoolVar(&f.preRelease, "pre-release", getBoolEnv("HCR_PRE_RELEASE", false), "Whether the (chart) release should be marked as pre-release")
flagSet.StringVar(&f.tag, "tag", getStringEnv("HCR_TAG", ""), "Release tag, defaults to chart version")
flagSet.StringVar(&f.remote, "remote", getStringEnv("HCR_REMOTE", "origin"), "The Git remote for the GitHub Pages branch")
Expand Down
41 changes: 26 additions & 15 deletions internal/helm/client.go
Expand Up @@ -9,7 +9,7 @@ import (
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/provenance"
"helm.sh/helm/v3/pkg/repo"
"io/ioutil"
"io/fs"
"os"
"path/filepath"
"strings"
Expand All @@ -26,31 +26,28 @@ func NewClient(log *zap.Logger) Client {
}

func (c Client) PackageCharts(chartsDir string) (charts map[string]*chart.Chart, cleanup func(), err error) {
files, err := ioutil.ReadDir(chartsDir)
chartsPaths, err := c.getChartsPaths(chartsDir)
if err != nil {
return nil, nil, fmt.Errorf("read charts directory: %w", err)
return nil, nil, err
}

var paths []string
var packagedChartsPaths []string
chs := make(map[string]*chart.Chart)
for _, file := range files {
if !file.IsDir() {
continue
}
path, ch, err := c.PackageChart(filepath.Join(chartsDir, file.Name()))
for _, chartPath := range chartsPaths {
packagedChartPath, ch, err := c.PackageChart(chartPath)
if err != nil {
return nil, nil, err
}
paths = append(paths, path)
chs[path] = ch
packagedChartsPaths = append(packagedChartsPaths, packagedChartPath)
chs[packagedChartPath] = ch
}

cleanup = func() {
for _, p := range paths {
if err := os.Remove(p); err != nil {
c.log.Warn(fmt.Sprintf("remove %s chart: %v", p, err))
for _, packagedChartPath := range packagedChartsPaths {
if err := os.Remove(packagedChartPath); err != nil {
c.log.Warn(fmt.Sprintf("remove %s chart: %v", packagedChartPath, err))
}
c.log.Info(fmt.Sprintf("removed generated chart %s", p))
c.log.Info(fmt.Sprintf("removed generated chart %s", packagedChartPath))
}
}
return chs, cleanup, nil
Expand Down Expand Up @@ -122,3 +119,17 @@ func (c Client) loadIndexFile(filePath string) (*repo.IndexFile, error) {
c.log.Info(fmt.Sprintf("loaded %s index file", filePath))
return indexFile, nil
}

// getChartsPaths walks supplied charts dir recursively and returns parent directories of all 'Chart.yaml' files
func (c Client) getChartsPaths(chartsDir string) ([]string, error) {
var paths []string
if err := filepath.WalkDir(chartsDir, func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() && d.Name() == "Chart.yaml" {
paths = append(paths, filepath.Dir(path))
}
return nil
}); err != nil {
return nil, err
}
return paths, nil
}

0 comments on commit ebebaac

Please sign in to comment.