Skip to content

Commit

Permalink
Improve resolveSymlink function
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed May 4, 2022
1 parent 054f74c commit d95f5c0
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,29 +108,33 @@ func visitYAML(files *[]string) filepath.WalkFunc {
}
}

func resolveSymlink(path string) (string, error) {
func resolvePath(path string) (string, bool, error) {
fi, err := os.Lstat(path)
if err != nil {
return path, err
return path, false, err
}

isDir := fi.IsDir()

if filepath.IsAbs(path) {
return path, isDir, nil
}

if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
return os.Readlink(path)
path, err = os.Readlink(path)
}
return path, nil

return path, isDir, err
}

// WalkDir walks given directory path and returns full-path of all yaml files
func WalkDir(path string) ([]string, error) {
var files []string
path, err := resolveSymlink(path)
if err != nil {
return files, err
}
fi, err := os.Stat(path)
path, isDir, err := resolvePath(path)
if err != nil {
return files, err
}
if fi.IsDir() {
if isDir {
return files, filepath.Walk(path, visitYAML(&files))
}
switch filepath.Ext(path) {
Expand Down

0 comments on commit d95f5c0

Please sign in to comment.