Walk symbolic link directories

Continue walking symlinks to be able to use them
for sub-charts. Useful when not specifying requirements
but still want to share charts locally.
pull/3113/head
Johnny Bergström 7 years ago
parent ab46779b88
commit 7cc610960f

@ -243,7 +243,7 @@ func LoadDir(dir string) (*chart.Chart, error) {
files := []*BufferedFile{} files := []*BufferedFile{}
topdir += string(filepath.Separator) topdir += string(filepath.Separator)
err = filepath.Walk(topdir, func(name string, fi os.FileInfo, err error) error { walk := func(name string, fi os.FileInfo, err error) error {
n := strings.TrimPrefix(name, topdir) n := strings.TrimPrefix(name, topdir)
if n == "" { if n == "" {
// No need to process top level. Avoid bug with helmignore .* matching // No need to process top level. Avoid bug with helmignore .* matching
@ -278,10 +278,45 @@ func LoadDir(dir string) (*chart.Chart, error) {
files = append(files, &BufferedFile{Name: n, Data: data}) files = append(files, &BufferedFile{Name: n, Data: data})
return nil return nil
}) }
if err != nil { if err = filepath.Walk(topdir, symWalk(topdir, "", walk)); err != nil {
return c, err return c, err
} }
return LoadFiles(files) return LoadFiles(files)
} }
// symWalk walks topdir with optional symbolic link dir, symdir. The symdir will
// be used as the path name sent to walkFn.
func symWalk(topdir, symdir string, walkFn filepath.WalkFunc) filepath.WalkFunc {
return func(name string, fi os.FileInfo, err error) error {
// Recover the symbolic path instead of the real path.
if symdir != "" {
relative, err := filepath.Rel(topdir, name)
if err != nil {
return err
}
name = filepath.Join(symdir, relative)
}
// Recursively walk symlinked directories.
if isSymlink(fi) {
resolved, err := filepath.EvalSymlinks(name)
if err != nil {
return fmt.Errorf("error evaluating symlink %s: %s", name, err)
}
if fi, err = os.Lstat(resolved); err != nil {
return err
}
if fi.IsDir() {
return filepath.Walk(resolved, symWalk(resolved, name, walkFn))
}
}
return walkFn(name, fi, err)
}
}
func isSymlink(fi os.FileInfo) bool {
return fi.Mode()&os.ModeSymlink != 0
}

@ -425,6 +425,19 @@ func TestDependentChartWithSubChartsHelmignore(t *testing.T) {
} }
} }
func TestDependentChartsWithSubChartsSymlink(t *testing.T) {
c, err := Load("testdata/joonix")
if err != nil {
t.Fatalf("Failed to load testdata: %s", err)
}
if c.Metadata.Name != "joonix" {
t.Fatalf("Unexpected chart name: %s", c.Metadata.Name)
}
if n := len(c.Dependencies); n != 1 {
t.Fatalf("Expected 1 dependency for this chart, but got %d", n)
}
}
func TestDependentChartsWithSubchartsAllSpecifiedInRequirements(t *testing.T) { func TestDependentChartsWithSubchartsAllSpecifiedInRequirements(t *testing.T) {
c, err := Load("testdata/dependent-chart-with-all-in-requirements-yaml") c, err := Load("testdata/dependent-chart-with-all-in-requirements-yaml")
if err != nil { if err != nil {

@ -0,0 +1,4 @@
apiVersion: v1
description: A Helm chart for Kubernetes
name: joonix
version: 1.2.3
Loading…
Cancel
Save