print changed file name

This commit is contained in:
alessio 2016-08-01 19:08:37 +02:00
parent 69bc07e3bc
commit 50e392aaff
1 changed files with 54 additions and 39 deletions

View File

@ -9,8 +9,11 @@ import (
"strings"
"sync"
"time"
"bytes"
)
var wg sync.WaitGroup
func InArray(str string, list []string) bool{
for _, v := range list {
if v == str {
@ -29,21 +32,25 @@ func Ignore(str string, list []string) bool{
return false
}
func (h *Config) Watch() error{
var current Watcher
var wg sync.WaitGroup
func Watching(val Project){
var current Project
var watcher *fsnotify.Watcher
watcher, _ = fsnotify.NewWatcher()
defer func(){
watcher.Close()
wg.Done()
}()
walk := func(path string, info os.FileInfo, err error) error{
if !Ignore(path,current.Ignore) {
if !Ignore(path,current.Watcher.Ignore) {
if info.IsDir() && len(filepath.Ext(path)) == 0 && !strings.Contains(path, "/.") {
fmt.Println(current.Name +": "+path)
if err = watcher.Add(path); err != nil {
return filepath.SkipDir
}
} else if InArray(filepath.Ext(path), current.Exts) {
} else if InArray(filepath.Ext(path), current.Watcher.Exts) {
fmt.Println(current.Name +": "+path)
if err = watcher.Add(path); err != nil {
return filepath.SkipDir
}
@ -52,21 +59,29 @@ func (h *Config) Watch() error{
return nil
}
watch := func(val Project){
watcher, _ = fsnotify.NewWatcher()
// run, bin, build
val.reload = time.Now().Truncate(time.Second)
for _, dir := range val.Watcher.Paths {
var base bytes.Buffer
path, _ := os.Getwd()
current = val.Watcher
// add dir of project
if err := filepath.Walk(path + dir, walk); err != nil {
current = val
split := strings.Split(val.Main, "/")
// get base path from mail field
for key, str := range split{
if(key < len(split)-1) {
base.WriteString("/" + str)
}
}
if err := filepath.Walk(path + base.String() + dir, walk); err != nil {
fmt.Println(err)
}
}
for {
select {
case event := <-watcher.Events:
@ -75,8 +90,12 @@ func (h *Config) Watch() error{
continue
}
if _, err := os.Stat(event.Name); err == nil {
log.Println("event:", event)
i := strings.Index(event.Name, filepath.Ext(event.Name))
log.Println("event:", event.Name[:i])
// run, bin, build
val.reload = time.Now().Truncate(time.Second)
}
}
@ -84,20 +103,16 @@ func (h *Config) Watch() error{
log.Println("error:", err)
}
}
watcher.Close()
wg.Done()
}
// add to watcher
func (h *Config) Watch() error{
if err := h.Read(); err == nil {
// loop projects
wg.Add(len(h.Projects))
for _, val := range h.Projects {
go watch(val)
go Watching(val)
}
wg.Wait()
return nil
}else{
return err