From d95f5c09df5215f854273bdbca7e374613259971 Mon Sep 17 00:00:00 2001 From: b4b4r07 Date: Thu, 5 May 2022 05:07:10 +0900 Subject: [PATCH] Improve resolveSymlink function --- pkg/config/config.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index ecb12f0..4bb83c4 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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) {