Skip to content

Commit

Permalink
fix: fixes file operations to work when not running from the chart ro…
Browse files Browse the repository at this point in the history
…ot and fixes several tests
  • Loading branch information
norwoodj committed Jun 29, 2022
1 parent 935a760 commit 45f63df
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 44 deletions.
8 changes: 4 additions & 4 deletions cmd/helm-docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions example-charts/files-values/README.md.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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" . }}
Expand Down
68 changes: 38 additions & 30 deletions pkg/document/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{}
Expand All @@ -69,56 +74,59 @@ 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
}
}

return result
}

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())
}

return toYAML(m)
}

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())
}

return toYAML(m)
}

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{}
}
Expand Down
22 changes: 13 additions & 9 deletions pkg/document/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"))
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/helm/chart_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down

0 comments on commit 45f63df

Please sign in to comment.