Skip to content

Commit

Permalink
Fix cli to work on windows
Browse files Browse the repository at this point in the history
Previously would error about missing file `consul\Chart.yaml`. This
is because the go embedded filesystem must be accessed using `/`
delimiters but we were using `filepath` functions which on windows
use `\`.
  • Loading branch information
lkysow committed Apr 4, 2022
1 parent a713f85 commit e2aceb7
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions cli/helm/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package helm

import (
"embed"
"path/filepath"
"path"
"strings"

"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
Expand Down Expand Up @@ -34,17 +35,23 @@ func LoadChart(chart embed.FS, chartDirName string) (*chart.Chart, error) {
func ReadChartFiles(chart embed.FS, chartDirName string) ([]*loader.BufferedFile, error) {
var chartFiles []*loader.BufferedFile

// NOTE: Because we're using the embedded filesystem, we must use path.* functions,
// *not* filepath.* functions. This is because the embedded filesystem always uses
// linux-style separators, even if this code is running on Windows. If we use
// filepath.* functions, then Go on Windows will try to use `\` delimiters to access
// the embedded filesystem, which will then fail.

// Load Chart.yaml and values.yaml first.
for _, f := range []string{chartFileName, valuesFileName} {
file, err := readFile(chart, filepath.Join(chartDirName, f), chartDirName)
file, err := readFile(chart, path.Join(chartDirName, f), chartDirName)
if err != nil {
return nil, err
}
chartFiles = append(chartFiles, file)
}

// Now load everything under templates/.
dirs, err := chart.ReadDir(filepath.Join(chartDirName, templatesDirName))
dirs, err := chart.ReadDir(path.Join(chartDirName, templatesDirName))
if err != nil {
return nil, err
}
Expand All @@ -54,7 +61,7 @@ func ReadChartFiles(chart embed.FS, chartDirName string) ([]*loader.BufferedFile
continue
}

file, err := readFile(chart, filepath.Join(chartDirName, templatesDirName, f.Name()), chartDirName)
file, err := readFile(chart, path.Join(chartDirName, templatesDirName, f.Name()), chartDirName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -86,11 +93,7 @@ func readFile(chart embed.FS, f string, pathPrefix string) (*loader.BufferedFile
if err != nil {
return nil, err
}
// Remove the path prefix.
rel, err := filepath.Rel(pathPrefix, f)
if err != nil {
return nil, err
}
rel := strings.TrimPrefix(f, pathPrefix+"/")
return &loader.BufferedFile{
Name: rel,
Data: bytes,
Expand Down

0 comments on commit e2aceb7

Please sign in to comment.