From 45f63df3a13c43bdcb30c26a58eeac40fcf87dab Mon Sep 17 00:00:00 2001 From: John Norwood Date: Wed, 29 Jun 2022 11:28:58 +0100 Subject: [PATCH] fix: fixes file operations to work when not running from the chart root and fixes several tests --- cmd/helm-docs/main.go | 8 +-- example-charts/files-values/README.md.gotmpl | 2 + pkg/document/files.go | 68 +++++++++++--------- pkg/document/files_test.go | 22 ++++--- pkg/helm/chart_info.go | 2 +- 5 files changed, 58 insertions(+), 44 deletions(-) diff --git a/cmd/helm-docs/main.go b/cmd/helm-docs/main.go index 2bc1784..da8e49e 100644 --- a/cmd/helm-docs/main.go +++ b/cmd/helm-docs/main.go @@ -95,7 +95,7 @@ func readDocumentationInfoByChartPath(chartSearchRoot string, parallelism int) ( return documentationInfoByChartPath, nil } -func getChartToGenerate(documentationInfoByChartPath map[string]helm.ChartDocumentationInfo)(map[string]helm.ChartDocumentationInfo) { +func getChartToGenerate(documentationInfoByChartPath map[string]helm.ChartDocumentationInfo) map[string]helm.ChartDocumentationInfo { generateDirectories := viper.GetStringSlice("chart-to-generate") if len(generateDirectories) == 0 { return documentationInfoByChartPath @@ -111,11 +111,11 @@ func getChartToGenerate(documentationInfoByChartPath map[string]helm.ChartDocume } } if skipped { - possibleCharts:= []string{} - for path := range(documentationInfoByChartPath) { + possibleCharts := []string{} + for path := range documentationInfoByChartPath { possibleCharts = append(possibleCharts, path) } - log.Warnf("Some charts listed in `chart-to-generate` wasn't found. List of charts to choose", strings.Join(possibleCharts, ", ")) + log.Warnf("Some charts listed in `chart-to-generate` wasn't found. List of charts to choose: [%s]", strings.Join(possibleCharts, ", ")) } return documentationInfoToGenerate } diff --git a/example-charts/files-values/README.md.gotmpl b/example-charts/files-values/README.md.gotmpl index 87a949c..4e4ef6c 100644 --- a/example-charts/files-values/README.md.gotmpl +++ b/example-charts/files-values/README.md.gotmpl @@ -35,6 +35,8 @@ metadata: name: test data: {{ (.Files.Glob "templates/**.yaml").AsConfig | indent 2 }} +dataSecret: +{{ (.Files.Glob "templates/**.yaml").AsSecrets | indent 2 }} ``` {{ template "chart.requirementsSection" . }} diff --git a/pkg/document/files.go b/pkg/document/files.go index fdec92e..8065e09 100644 --- a/pkg/document/files.go +++ b/pkg/document/files.go @@ -13,30 +13,21 @@ import ( "gopkg.in/yaml.v3" ) -// Near identical to https://github.com/helm/helm/blob/main/pkg/engine/files.go as to preserve the interface. +type files struct { + baseDir string + foundFiles map[string]*fileEntry +} type fileEntry struct { Path string data []byte } -func (f *fileEntry) GetData() []byte { - if f.data == nil { - data, err := ioutil.ReadFile(f.Path) - if err != nil { - log.Warnf("Error reading file contents for %s: %s", f.Path, err.Error()) - return []byte{} - } - f.data = data - } - - return f.data -} - -type files map[string]*fileEntry - func getFiles(dir string) (files, error) { - result := make(files) + result := files{ + baseDir: dir, + foundFiles: make(map[string]*fileEntry), + } err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil { @@ -47,18 +38,32 @@ func getFiles(dir string) (files, error) { return nil } - result[path] = &fileEntry{Path: path} + result.foundFiles[path] = &fileEntry{Path: path} return nil }) + if err != nil { - return map[string]*fileEntry{}, err + return files{}, err } return result, nil } +func (f *fileEntry) GetData() []byte { + if f.data == nil { + data, err := ioutil.ReadFile(f.Path) + if err != nil { + log.Warnf("Error reading file contents for %s: %s", f.Path, err.Error()) + return []byte{} + } + f.data = data + } + + return f.data +} + func (f files) GetBytes(name string) []byte { - if v, ok := f[name]; ok { + if v, ok := f.foundFiles[filepath.Join(f.baseDir, name)]; ok { return v.GetData() } return []byte{} @@ -69,16 +74,19 @@ func (f files) Get(name string) string { } func (f files) Glob(pattern string) files { - result := make(files) - g, err := glob.Compile(pattern, '/') + result := files{ + baseDir: f.baseDir, + foundFiles: make(map[string]*fileEntry), + } + g, err := glob.Compile(filepath.Join(f.baseDir, pattern), filepath.Separator) if err != nil { log.Warnf("Error compiling Glob patten %s: %s", pattern, err.Error()) return result } - for filePath, entry := range f { + for filePath, entry := range f.foundFiles { if g.Match(filePath) { - result[filePath] = entry + result.foundFiles[filePath] = entry } } @@ -86,14 +94,14 @@ func (f files) Glob(pattern string) files { } func (f files) AsConfig() string { - if f == nil { + if len(f.foundFiles) == 0 { return "" } m := make(map[string]string) // Explicitly convert to strings, and file names - for k, v := range f { + for k, v := range f.foundFiles { m[path.Base(k)] = string(v.GetData()) } @@ -101,13 +109,13 @@ func (f files) AsConfig() string { } func (f files) AsSecrets() string { - if f == nil { + if len(f.foundFiles) == 0 { return "" } m := make(map[string]string) - for k, v := range f { + for k, v := range f.foundFiles { m[path.Base(k)] = base64.StdEncoding.EncodeToString(v.GetData()) } @@ -115,10 +123,10 @@ func (f files) AsSecrets() string { } func (f files) Lines(path string) []string { - if f == nil { + if len(f.foundFiles) == 0 { return []string{} } - entry, exists := f[path] + entry, exists := f.foundFiles[path] if !exists { return []string{} } diff --git a/pkg/document/files_test.go b/pkg/document/files_test.go index 769f33f..8303d23 100644 --- a/pkg/document/files_test.go +++ b/pkg/document/files_test.go @@ -21,21 +21,25 @@ var cases = []struct { } func getTestFiles() files { - a := make(files, len(cases)) + a := files{ + baseDir: "", + foundFiles: make(map[string]*fileEntry), + } for _, c := range cases { - a[c.path] = &fileEntry{ + a.foundFiles[c.path] = &fileEntry{ Path: c.path, data: []byte(c.data), } } + return a } func TestNewFiles(t *testing.T) { files := getTestFiles() - if len(files) != len(cases) { - t.Errorf("Expected len() = %d, got %d", len(cases), len(files)) + if len(files.foundFiles) != len(cases) { + t.Errorf("Expected len() = %d, got %d", len(cases), len(files.foundFiles)) } for i, f := range cases { @@ -55,7 +59,7 @@ func TestFileGlob(t *testing.T) { matched := f.Glob("story/**") - as.Len(matched, 2, "Should be two files in glob story/**") + as.Len(matched.foundFiles, 2, "Should be two files in glob story/**") as.Equal("Joseph Conrad", matched.Get("story/author.txt")) } @@ -101,7 +105,7 @@ func TestGetFiles(t *testing.T) { }) testFiles := getTestFiles() - for filePath, entry := range testFiles { + for filePath, entry := range testFiles.foundFiles { fullPath := path.Join(chartDir, filePath) baseDir := path.Dir(fullPath) if err = os.MkdirAll(baseDir, 0o755); err != nil { @@ -119,12 +123,12 @@ func TestGetFiles(t *testing.T) { t.Fatal(err) } - if len(chartFiles) != len(testFiles) { - t.Errorf("chart files: expected %d, got %d", len(chartFiles), len(testFiles)) + if len(chartFiles.foundFiles) != len(testFiles.foundFiles) { + t.Errorf("chart files: expected %d, got %d", len(chartFiles.foundFiles), len(testFiles.foundFiles)) } // Sanity check the files have been read - for filePath, entry := range chartFiles { + for filePath, entry := range chartFiles.foundFiles { data := entry.GetData() if len(data) == 0 { diff --git a/pkg/helm/chart_info.go b/pkg/helm/chart_info.go index 58ef471..3cbc5ce 100644 --- a/pkg/helm/chart_info.go +++ b/pkg/helm/chart_info.go @@ -147,7 +147,7 @@ func parseChartRequirementsFile(chartDirectory string, apiVersion string) (Chart } func removeIgnored(rootNode *yaml.Node, parentKind yaml.Kind) { - newContent := make([]*yaml.Node , 0, len(rootNode.Content)) + newContent := make([]*yaml.Node, 0, len(rootNode.Content)) for i := 0; i < len(rootNode.Content); i++ { node := rootNode.Content[i] if !strings.Contains(node.HeadComment, "@ignore") {