diff --git a/go.mod b/go.mod index e7dadfe..cbcfe06 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/ema/pets go 1.19 -require github.com/hashicorp/logutils v1.0.0 // indirect +require ( + github.com/facebookgo/symwalk v0.0.0-20150726040526-42004b9f3222 // indirect + github.com/hashicorp/logutils v1.0.0 // indirect +) diff --git a/go.sum b/go.sum index 635dc62..f84571d 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ +github.com/facebookgo/symwalk v0.0.0-20150726040526-42004b9f3222 h1:ivxAxcE9py2xLAqpcEwN7sN711aLfEWgh3cY0aha7uY= +github.com/facebookgo/symwalk v0.0.0-20150726040526-42004b9f3222/go.mod h1:PgrCjL2+FgkITqxQI+erRTONtAv4JkpOzun5ozKW/Jg= github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= diff --git a/parser.go b/parser.go index 3ee6a04..badffdf 100644 --- a/parser.go +++ b/parser.go @@ -13,6 +13,8 @@ import ( "path/filepath" "regexp" "strings" + + "github.com/facebookgo/symwalk" ) // Because it is important to know when enough is enough. @@ -126,7 +128,7 @@ func ParseFiles(directory string) ([]*PetsFile, error) { log.Printf("[DEBUG] using configuration directory '%s'\n", directory) - err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error { + err := symwalk.Walk(directory, func(path string, info os.FileInfo, err error) error { // This function is called once for each file in the Pets configuration // directory if err != nil { @@ -159,10 +161,17 @@ func ParseFiles(directory string) ([]*PetsFile, error) { // is the source path. Every long journey begins with a single step! pf := NewPetsFile() + // Evaluate the symlinks, to simplify checking whether a symlink + // we created points to the file it should + direct, err := filepath.EvalSymlinks(path) + if err != nil { + return err + } + // Get absolute path to the source. Technically we would be fine with a - // relative path too, but it's good to remove abiguity. Plus absolute + // relative path too, but it's good to remove ambiguity. Plus absolute // paths make things easier in case we have to create a symlink. - abs, err := filepath.Abs(path) + abs, err := filepath.Abs(direct) if err != nil { return err }