Add support for symbolic links for content, layout, static, theme
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 14 Feb 2016 11:16:03 +0000 (12:16 +0100)
committerSteve Francia <steve.francia@gmail.com>
Mon, 21 Mar 2016 23:13:29 +0000 (19:13 -0400)
Note: This is for the content roots only, but that should satisfy most needs.

Fixes #1855

commands/hugo.go
commands/import_jekyll.go
helpers/path.go
source/filesystem.go

index daf483923690b11b3e26829141a612d86eff1858..78a416d65a222c60346177403bef0d0fa8b85f2f 100644 (file)
@@ -17,6 +17,7 @@ package commands
 
 import (
        "fmt"
+       "github.com/spf13/hugo/hugofs"
        "io/ioutil"
        "net/http"
        "os"
@@ -35,7 +36,6 @@ import (
        "github.com/spf13/cobra"
        "github.com/spf13/fsync"
        "github.com/spf13/hugo/helpers"
-       "github.com/spf13/hugo/hugofs"
        "github.com/spf13/hugo/hugolib"
        "github.com/spf13/hugo/livereload"
        "github.com/spf13/hugo/utils"
@@ -651,12 +651,12 @@ func getDirList() []string {
                return nil
        }
 
-       filepath.Walk(dataDir, walker)
-       filepath.Walk(helpers.AbsPathify(viper.GetString("ContentDir")), walker)
-       filepath.Walk(helpers.AbsPathify(viper.GetString("LayoutDir")), walker)
-       filepath.Walk(helpers.AbsPathify(viper.GetString("StaticDir")), walker)
+       helpers.SymbolicWalk(hugofs.SourceFs, dataDir, walker)
+       helpers.SymbolicWalk(hugofs.SourceFs, helpers.AbsPathify(viper.GetString("ContentDir")), walker)
+       helpers.SymbolicWalk(hugofs.SourceFs, helpers.AbsPathify(viper.GetString("LayoutDir")), walker)
+       helpers.SymbolicWalk(hugofs.SourceFs, helpers.AbsPathify(viper.GetString("StaticDir")), walker)
        if helpers.ThemeSet() {
-               filepath.Walk(helpers.AbsPathify(viper.GetString("themesDir")+"/"+viper.GetString("theme")), walker)
+               helpers.SymbolicWalk(hugofs.SourceFs, helpers.AbsPathify(viper.GetString("themesDir")+"/"+viper.GetString("theme")), walker)
        }
 
        return a
@@ -769,7 +769,7 @@ func NewWatcher(port int) error {
                                        // When mkdir -p is used, only the top directory triggers an event (at least on OSX)
                                        if ev.Op&fsnotify.Create == fsnotify.Create {
                                                if s, err := hugofs.SourceFs.Stat(ev.Name); err == nil && s.Mode().IsDir() {
-                                                       afero.Walk(hugofs.SourceFs, ev.Name, walkAdder)
+                                                       helpers.SymbolicWalk(hugofs.SourceFs, ev.Name, walkAdder)
                                                }
                                        }
 
index 8209481745add7de5e8723811cded681be659304..40d243375034c26a931408db62d1b5372a3bd7c3 100644 (file)
@@ -124,7 +124,7 @@ func importFromJekyll(cmd *cobra.Command, args []string) error {
                return convertJekyllPost(path, relPath, targetDir, draft)
        }
 
-       err = filepath.Walk(jekyllRoot, callback)
+       err = helpers.SymbolicWalk(hugofs.OsFs, jekyllRoot, callback)
 
        if err != nil {
                return err
index f39815b6e5d2324ee71cc3a8bbf7a4ee5d4f2d57..1ac5a87d8d307959a24e5d99b4c5a8e4f74a3c8e 100644 (file)
@@ -428,6 +428,24 @@ func FindCWD() (string, error) {
        return path, nil
 }
 
+// SymbolicWalk is like filepath.Walk, but it supports the root being a
+// symbolic link. It will still not follow symbolic links deeper down in
+// the file structure
+func SymbolicWalk(fs afero.Fs, root string, walker filepath.WalkFunc) error {
+       rootContent, err := afero.ReadDir(fs, root)
+
+       if err != nil {
+               return walker(root, nil, err)
+       }
+
+       for _, fi := range rootContent {
+               afero.Walk(fs, filepath.Join(root, fi.Name()), walker)
+       }
+
+       return nil
+
+}
+
 // Same as WriteToDisk but checks to see if file/directory already exists.
 func SafeWriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
        return afero.SafeWriteReader(fs, inpath, r)
index 0f53584291795a4088b72f1664a28af924518350..66270c5890a5e734c14e778614928679b844f6f2 100644 (file)
@@ -14,6 +14,7 @@
 package source
 
 import (
+       "github.com/spf13/hugo/hugofs"
        "io"
        "os"
        "path/filepath"
@@ -92,7 +93,12 @@ func (f *Filesystem) captureFiles() {
                return err
        }
 
-       filepath.Walk(f.Base, walker)
+       err := helpers.SymbolicWalk(hugofs.SourceFs, f.Base, walker)
+
+       if err != nil {
+               jww.ERROR.Println(err)
+       }
+
 }
 
 func (f *Filesystem) shouldRead(filePath string, fi os.FileInfo) (bool, error) {