-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from norwoodj/improve-ignoring
feat: improves ignore feature, accepting an ignore file at the root o…
- Loading branch information
Showing
11 changed files
with
132 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,4 @@ test: | |
|
||
.PHONY: clean | ||
clean: | ||
rm helm-docs | ||
rm -f helm-docs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |