Skip to content

Commit

Permalink
Merge pull request #18 from norwoodj/improve-ignoring
Browse files Browse the repository at this point in the history
feat: improves ignore feature, accepting an ignore file at the root o…
  • Loading branch information
norwoodj authored Aug 10, 2019
2 parents bddbcfe + d6aca61 commit c8635e6
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .helmdocsignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
example-charts/ignored
example-charts/ignored-zero
example-charts/ignored-one/Chart.yaml
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ test:

.PHONY: clean
clean:
rm helm-docs
rm -f helm-docs
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ in the templates you supply.
## Ignoring Chart Directories
helm-docs supports a `.helmdocsignore` file, exactly like a `.gitignore` file in which one can specify directories to ignore
when searching for charts. Directories specified need not be charts themselves, so parent directories containing potentially
many charts can be ignored and none of the charts underneath them will be processed.
many charts can be ignored and none of the charts underneath them will be processed. You may also directly reference the
Chart.yaml file for a chart to skip processing for it.


## values.yaml metadata
Expand Down
10 changes: 10 additions & 0 deletions example-charts/ignored-one/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
name: ignored-one
description: A demonstration of the ability of a .helmdocsignore file to ignore files by Chart.yaml file
version: "0.2.0"
home: "https://github.com/norwoodj/helm-docs/tree/master/example-charts/ignored-one"
sources: ["https://github.com/norwoodj/helm-docs/tree/master/example-charts/ignored-one"]
engine: gotpl
maintainers:
- email: [email protected]
name: John Norwood
File renamed without changes.
10 changes: 10 additions & 0 deletions example-charts/ignored-zero/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
name: ignored-zero
description: A demonstration of the ability of a .helmdocsignore file to ignore charts by directory name
version: "0.2.0"
home: "https://github.com/norwoodj/helm-docs/tree/master/example-charts/ignored-zero"
sources: ["https://github.com/norwoodj/helm-docs/tree/master/example-charts/ignored-zero"]
engine: gotpl
maintainers:
- email: [email protected]
name: John Norwood
1 change: 1 addition & 0 deletions example-charts/ignored-zero/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
10 changes: 0 additions & 10 deletions example-charts/ignored/Chart.yaml

This file was deleted.

32 changes: 12 additions & 20 deletions pkg/helm/chart_finder.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,37 @@
package helm

import (
"bufio"
"os"
"path/filepath"

"github.com/norwoodj/helm-docs/pkg/util"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"k8s.io/helm/pkg/ignore"
)

var defaultIgnore = map[string]bool{
".git": true,
}

func FindChartDirectories() ([]string, error) {
ignoreRules := ignore.Empty()
ignoreFilename := viper.GetString("ignore-file")
ignoreFile, err := os.Open(ignoreFilename)

if err == nil {
ignoreRules, err = ignore.Parse(bufio.NewReader(ignoreFile))

if err != nil {
log.Warnf("Failed to parse ignore rules from file %s", ignoreFilename)
ignoreRules = ignore.Empty()
}
}

ignoreContext := util.NewIgnoreContext(ignoreFilename)
chartDirs := make([]string, 0)
err = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {

err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() && (ignoreRules.Ignore(path, info) || defaultIgnore[path]) {
absolutePath, _ := filepath.Abs(path)

if info.IsDir() && ignoreContext.ShouldIgnore(absolutePath, info) {
log.Debugf("Ignoring directory %s", path)
return filepath.SkipDir
}

if filepath.Base(path) == "Chart.yaml" {
if ignoreContext.ShouldIgnore(absolutePath, info) {
log.Debugf("Ignoring chart file %s", path)
return nil
}

chartDirs = append(chartDirs, filepath.Dir(path))
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/util/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package util

import (
"os/exec"
"strings"
)

func FindGitRepositoryRoot() (string, error) {
path, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()

if err != nil {
return "", err
}

return strings.TrimSpace(string(path)), nil
}
78 changes: 78 additions & 0 deletions pkg/util/ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package util

import (
"bufio"
"fmt"
"os"
"path/filepath"

log "github.com/sirupsen/logrus"
"k8s.io/helm/pkg/ignore"
)

var defaultIgnore = map[string]bool{
".git": true,
}

type IgnoreContext struct {
rules *ignore.Rules
relativeDir string
}

func parseIgnoreFilePathToRules(filename string) (*ignore.Rules, error) {
ignoreFile, err := os.Open(filename)

if os.IsNotExist(err) {
log.Debugf("No ignore file found at %s, using empty ignore rules", filename)
return ignore.Empty(), nil
}

if err != nil {
return nil, fmt.Errorf("failed to open ignore file at %s: %s", filename, err)
}

ignoreRules, err := ignore.Parse(bufio.NewReader(ignoreFile))
if err != nil {
return nil, fmt.Errorf("failed to parse ignore rules from file %s: %s", filename, err)
}

return ignoreRules, nil
}

func NewIgnoreContext(ignoreFilename string) IgnoreContext {
gitRepositoryRoot, err := FindGitRepositoryRoot()

// If we got an error reading the repository root, then let's try for a ignore file in this directory
if err != nil {
ignoreRules, err := parseIgnoreFilePathToRules(ignoreFilename)

if err != nil {
log.Warnf("Using empty ignore rules due to error: %s", err)
return IgnoreContext{rules: ignore.Empty()}
}

absoluteWorkingDir, _ := filepath.Abs(".")
return IgnoreContext{rules: ignoreRules, relativeDir: absoluteWorkingDir}
}

// Otherwise, let's look for a ignore file at the repository root and parse it, storing that files are ignored relative
// to that directory
ignoreRules, err := parseIgnoreFilePathToRules(filepath.Join(gitRepositoryRoot, ignoreFilename))

if err != nil {
log.Warnf("Using empty ignore rules due to error: %s", err)
return IgnoreContext{rules: ignoreRules}
}

return IgnoreContext{rules: ignoreRules, relativeDir: gitRepositoryRoot}
}

func (i IgnoreContext) ShouldIgnore(path string, fi os.FileInfo) bool {
pathRelativeToIgnoreFile, err := filepath.Rel(i.relativeDir, path)

if err != nil {
return false
}

return i.rules.Ignore(pathRelativeToIgnoreFile, fi) || defaultIgnore[pathRelativeToIgnoreFile]
}

0 comments on commit c8635e6

Please sign in to comment.