paths and files to watcher

This commit is contained in:
alessio 2016-07-31 23:39:56 +02:00
parent 0ebcfebf9d
commit c332ed7db8

View File

@ -2,38 +2,97 @@ package realize
import ( import (
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"fmt"
"path/filepath" "path/filepath"
"os" "os"
"log"
"strings"
) )
func InArray(str string, list []string) bool{
for _, v := range list {
if v == str {
return true
}
}
return false
}
func Ignore(str string, list []string) bool{
for _, v := range list {
if strings.Contains(str, v) {
return true
}
}
return false
}
func (h *Config) Watch() error{ func (h *Config) Watch() error{
var current Watcher
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil{ if err != nil{
panic(err) fmt.Println(err)
} }
defer watcher.Close()
walking := func(path string, info os.FileInfo, err error) error{ walk := func(path string, info os.FileInfo, err error) error{
if info.IsDir() { if !Ignore(path,current.Ignore) {
if err = watcher.Add(path); err != nil { if info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.Contains(path, "/.") {
return filepath.SkipDir if err = watcher.Add(path); err != nil {
return filepath.SkipDir
}
} else if InArray(filepath.Ext(path), current.Exts) {
if err = watcher.Add(path); err != nil {
return filepath.SkipDir
}
} }
} }
return nil return nil
} }
// check file watch := func(){
for {
select {
case event := <-watcher.Events:
log.Println("event:", event)
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("modified file:", event.Name)
}
case err := <-watcher.Errors:
log.Println("error:", err)
}
}
}
defer func(){
watcher.Close()
// kill process
}()
// add to watcher
if err := h.Read(); err == nil { if err := h.Read(); err == nil {
// loop projects // loop projects
for _, val := range h.Projects { for _, val := range h.Projects {
// add paths to watcher // add paths
for _, path := range val.Watcher.Paths { for _, path := range val.Watcher.Paths {
if err := filepath.Walk(path, walking); err != nil{ p, _ := os.Getwd()
panic(err) current = val.Watcher
if err := filepath.Walk(p+path, walk); err != nil{
fmt.Println(err)
} }
} }
} }
// watch changes
watch()
// build
// install
// run
return nil return nil
}else{ }else{
return err return err