add watcher
Adds the ability to watch a directory for changes and run a command when changes are spit out
This commit is contained in:
parent
913e4bf460
commit
e6bcf25e65
6
glide.lock
generated
6
glide.lock
generated
@ -1,6 +1,8 @@
|
||||
hash: af0c2b0e7fd58c70a43dcf5c75b802f7a49c5d53ad6aec1975adfebe0acee206
|
||||
updated: 2017-02-04T09:25:07.785352554-07:00
|
||||
hash: aa963bb1783c6d48ae4d7001851c87e2abb46cfbcdfd8a4792dbe766b7fafcd3
|
||||
updated: 2017-02-27T10:22:14.549716708-07:00
|
||||
imports:
|
||||
- name: github.com/fsnotify/fsnotify
|
||||
version: 629574ca2a5df945712d3079857300b5e4da0236
|
||||
- name: github.com/gorilla/feeds
|
||||
version: 441264de03a8117ed530ae8e049d8f601a33a099
|
||||
- name: github.com/russross/blackfriday
|
||||
|
@ -4,3 +4,5 @@ import:
|
||||
version: ~1.4.0
|
||||
- package: github.com/ylih/extrasys
|
||||
- package: github.com/gorilla/feeds
|
||||
- package: github.com/fsnotify/fsnotify
|
||||
version: ^1.4.2
|
||||
|
43
main.go
43
main.go
@ -2,17 +2,20 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
. "github.com/gorilla/feeds"
|
||||
"github.com/russross/blackfriday"
|
||||
// "github.com/ylih/extrasys"
|
||||
@ -259,6 +262,13 @@ func main() {
|
||||
var err error
|
||||
// extrasys.Pledge("stdio wpath rpath cpath", nil)
|
||||
|
||||
var watch = flag.Bool("w", false, "Enable 'watch' mode. Requires 'wdir' and 'wcmd'.")
|
||||
var watchDir = flag.String("wdir", "", "watch a directory for changes, run command when change happens.")
|
||||
var watchCmd = flag.String("wcmd", "", "command to run when changes are detected in 'wdir'.")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if !*watch {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println("Wrong number of arguments")
|
||||
os.Exit(1)
|
||||
@ -362,4 +372,37 @@ func main() {
|
||||
|
||||
feed.WriteAtom(atomFile)
|
||||
feed.WriteRss(rssFile)
|
||||
} else {
|
||||
// Watch mode
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer watcher.Close()
|
||||
|
||||
done := make(chan bool)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case event := <-watcher.Events:
|
||||
if event.Op&fsnotify.Write == fsnotify.Write {
|
||||
log.Println("modified file:", event.Name)
|
||||
c := exec.Command(*watchCmd)
|
||||
|
||||
if err := c.Run(); err != nil {
|
||||
fmt.Println("Error: ", err)
|
||||
}
|
||||
}
|
||||
case err := <-watcher.Errors:
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
err = watcher.Add(*watchDir)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
<-done
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user