|
|
@ -1,11 +1,16 @@ |
|
|
|
package main |
|
|
|
|
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
"io/ioutil" |
|
|
|
"log" |
|
|
|
"os" |
|
|
|
"os/exec" |
|
|
|
"path/filepath" |
|
|
|
"strings" |
|
|
|
|
|
|
|
"github.com/fatih/color" |
|
|
|
"github.com/fsnotify/fsnotify" |
|
|
|
) |
|
|
|
|
|
|
|
func readFile(path string) string { |
|
|
@ -43,3 +48,65 @@ func copyRaw(original string, destination string) { |
|
|
|
_, err := exec.Command("cp", "-rf", original, destination).Output() |
|
|
|
check(err) |
|
|
|
} |
|
|
|
|
|
|
|
var watcherInputFiles, watcherPublic *fsnotify.Watcher |
|
|
|
|
|
|
|
func watch(dir string) { |
|
|
|
var err error |
|
|
|
var watcher *fsnotify.Watcher |
|
|
|
if dir == "./blogo-input" { |
|
|
|
watcherInputFiles, err = fsnotify.NewWatcher() |
|
|
|
if err != nil { |
|
|
|
log.Fatal(err) |
|
|
|
} |
|
|
|
watcher = watcherInputFiles |
|
|
|
} else { |
|
|
|
watcherPublic, err = fsnotify.NewWatcher() |
|
|
|
if err != nil { |
|
|
|
log.Fatal(err) |
|
|
|
} |
|
|
|
watcher = watcherPublic |
|
|
|
} |
|
|
|
|
|
|
|
defer watcher.Close() |
|
|
|
|
|
|
|
if dir == "./blogo-input" { |
|
|
|
if err := filepath.Walk(dir, watchInputFilesDir); err != nil { |
|
|
|
log.Fatal(err) |
|
|
|
} |
|
|
|
} else { |
|
|
|
if err := filepath.Walk(dir, watchPublicDir); err != nil { |
|
|
|
log.Fatal(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for { |
|
|
|
select { |
|
|
|
case event := <-watcher.Events: |
|
|
|
fmt.Printf("file system event: %#v\n", event) |
|
|
|
if dir == "./blogo-input" { |
|
|
|
generateHTML() |
|
|
|
} |
|
|
|
case err := <-watcher.Errors: |
|
|
|
log.Fatal("file system watcher error:", err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// watchInputFilesDir gets run as a walk func, searching for directories to add watchers to
|
|
|
|
func watchInputFilesDir(path string, fi os.FileInfo, err error) error { |
|
|
|
if fi.Mode().IsDir() { |
|
|
|
return watcherInputFiles.Add(path) |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// watchPublicDir gets run as a walk func, searching for directories to add watchers to
|
|
|
|
func watchPublicDir(path string, fi os.FileInfo, err error) error { |
|
|
|
if fi.Mode().IsDir() { |
|
|
|
return watcherPublic.Add(path) |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
} |