Add support for dynamic reloading of config file when watching

This commit is contained in:
spf13
2015-11-09 23:31:52 -05:00
parent 488966dcb2
commit bccf957e36
3 changed files with 21 additions and 6 deletions

View File

@@ -17,7 +17,6 @@ package commands
import (
"fmt"
"github.com/spf13/hugo/parser"
"io/ioutil"
"net/http"
"os"
@@ -27,6 +26,8 @@ import (
"sync"
"time"
"github.com/spf13/hugo/parser"
"github.com/spf13/cobra"
"github.com/spf13/fsync"
"github.com/spf13/hugo/helpers"
@@ -53,6 +54,7 @@ built with love by spf13 and friends in Go.
Complete documentation is available at http://gohugo.io/.`,
Run: func(cmd *cobra.Command, args []string) {
InitializeConfig()
watchConfig()
build()
},
}
@@ -312,6 +314,18 @@ func InitializeConfig() {
}
}
func watchConfig() {
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
utils.CheckErr(buildSite(true))
if !viper.GetBool("DisableLiveReload") {
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initalized
livereload.ForceRefresh()
}
})
}
func build(watches ...bool) {
utils.CheckErr(copyStatic(), fmt.Sprintf("Error copying static files to %s", helpers.AbsPathify(viper.GetString("PublishDir"))))
watch := false

View File

@@ -93,6 +93,7 @@ func server(cmd *cobra.Command, args []string) {
if viper.GetBool("watch") {
serverWatch = true
watchConfig()
}
l, err := net.Listen("tcp", net.JoinHostPort(serverInterface, strconv.Itoa(serverPort)))