Handle potential error from os.Readlink

This commit is contained in:
Justin Li
2014-03-08 16:35:20 -05:00
parent 13df5509a9
commit a11342d6e7

View File

@@ -66,19 +66,22 @@ func mustChmod(filename string, mode os.FileMode) {
// Additionally, the trailing ".template" is stripped from the file name. // Additionally, the trailing ".template" is stripped from the file name.
// Also, dot files and dot directories are skipped. // Also, dot files and dot directories are skipped.
func mustCopyDir(destDir, srcDir string, data map[string]interface{}) error { func mustCopyDir(destDir, srcDir string, data map[string]interface{}) error {
var realSrcDir string var fullSrcDir string
// handle symlinked source-direcories // Handle symlinked directories.
f, err := os.Lstat(srcDir) f, err := os.Lstat(srcDir)
if err == nil && f.Mode()&os.ModeSymlink == os.ModeSymlink { if err == nil && f.Mode()&os.ModeSymlink == os.ModeSymlink {
realSrcDir, _ = os.Readlink(srcDir) fullSrcDir, err = os.Readlink(srcDir)
if err != nil {
panic(err)
}
} else { } else {
realSrcDir = srcDir fullSrcDir = srcDir
} }
return filepath.Walk(realSrcDir, func(srcPath string, info os.FileInfo, err error) error { return filepath.Walk(fullSrcDir, func(srcPath string, info os.FileInfo, err error) error {
// Get the relative path from the source base, and the corresponding path in // Get the relative path from the source base, and the corresponding path in
// the dest directory. // the dest directory.
relSrcPath := strings.TrimLeft(srcPath[len(realSrcDir):], string(os.PathSeparator)) relSrcPath := strings.TrimLeft(srcPath[len(fullSrcDir):], string(os.PathSeparator))
destPath := path.Join(destDir, relSrcPath) destPath := path.Join(destDir, relSrcPath)
// Skip dot files and dot directories. // Skip dot files and dot directories.