2018-09-10 08:23:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"gopkg.in/fsnotify/fsnotify.v1"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
Version = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
projectFolder string = "."
|
|
|
|
|
|
|
|
cfg *FileGirl
|
|
|
|
|
|
|
|
watcher *fsnotify.Watcher
|
|
|
|
|
2018-12-30 07:14:14 +00:00
|
|
|
taskMan *TaskMan
|
2018-09-10 08:23:27 +00:00
|
|
|
)
|
|
|
|
|
2019-01-03 01:56:26 +00:00
|
|
|
type changedFile struct {
|
2018-09-19 03:26:57 +00:00
|
|
|
Name string
|
2018-09-30 01:27:15 +00:00
|
|
|
Changed int64
|
2018-09-19 03:26:57 +00:00
|
|
|
Ext string
|
2018-09-10 08:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseConfig() {
|
|
|
|
cfg = new(FileGirl)
|
|
|
|
fc, err := ioutil.ReadFile(projectFolder + "/filegirl.yaml")
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln("read filegirl.yaml file err: ", err)
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(fc, cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln("parsed filegirl.yaml failed: ", err)
|
|
|
|
}
|
|
|
|
if cfg.Core.Version > Version {
|
|
|
|
log.Panicln("current fileboy support max version : ", Version)
|
|
|
|
}
|
|
|
|
log.Println(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func eventDispatcher(event fsnotify.Event) {
|
|
|
|
ext := path.Ext(event.Name)
|
|
|
|
if len(cfg.Monitor.Types) > 0 &&
|
|
|
|
cfg.Monitor.Types[0] != ".*" &&
|
|
|
|
!inStringArray(ext, cfg.Monitor.Types) {
|
|
|
|
//log.Println(ext, cfg.Monitor.Types, inStringArray(ext, cfg.Monitor.Types))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch event.Op {
|
2019-01-03 02:07:24 +00:00
|
|
|
case
|
|
|
|
fsnotify.Write,
|
|
|
|
fsnotify.Rename:
|
2019-01-03 02:13:08 +00:00
|
|
|
log.Println("EVENT", event.Op.String(), ":", event.Name)
|
2019-01-03 01:56:26 +00:00
|
|
|
taskMan.Put(&changedFile{
|
2018-09-19 03:26:57 +00:00
|
|
|
Name: relativePath(projectFolder, event.Name),
|
2018-09-30 01:27:15 +00:00
|
|
|
Changed: time.Now().UnixNano(),
|
2018-09-19 03:26:57 +00:00
|
|
|
Ext: ext,
|
|
|
|
})
|
2018-09-10 08:23:27 +00:00
|
|
|
case fsnotify.Remove:
|
2019-01-03 02:07:24 +00:00
|
|
|
case fsnotify.Create:
|
2018-09-10 08:23:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addWatcher() {
|
2018-09-19 02:03:42 +00:00
|
|
|
log.Println("collecting directory information...")
|
2018-09-10 08:23:27 +00:00
|
|
|
dirs := make([]string, 0)
|
|
|
|
for i := 0; i < len(cfg.Monitor.IncludeDirs); i++ {
|
|
|
|
darr := dirParse2Array(cfg.Monitor.IncludeDirs[i])
|
|
|
|
if len(darr) < 1 || len(darr) > 2 {
|
|
|
|
log.Fatalln("filegirl section monitor dirs is error. ", cfg.Monitor.IncludeDirs[i])
|
|
|
|
}
|
|
|
|
if darr[0] == "." {
|
|
|
|
if len(darr) == 2 && darr[1] == "*" {
|
|
|
|
dirs = make([]string, 0)
|
|
|
|
dirs = append(dirs, ".")
|
|
|
|
listFile(projectFolder, func(d string) {
|
|
|
|
dirs = arrayUniqueAdd(dirs, d)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
dirs = arrayUniqueAdd(dirs, projectFolder)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
} else {
|
2018-09-30 01:27:15 +00:00
|
|
|
md := projectFolder + "/" + darr[0]
|
2018-09-10 08:23:27 +00:00
|
|
|
if len(darr) == 2 && darr[1] == "*" {
|
2018-09-30 01:27:15 +00:00
|
|
|
dirs = arrayUniqueAdd(dirs, md)
|
|
|
|
listFile(md, func(d string) {
|
2018-09-10 08:23:27 +00:00
|
|
|
dirs = arrayUniqueAdd(dirs, d)
|
|
|
|
})
|
|
|
|
} else {
|
2018-09-30 01:27:15 +00:00
|
|
|
dirs = arrayUniqueAdd(dirs, md)
|
2018-09-10 08:23:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
for i := 0; i < len(cfg.Monitor.ExceptDirs); i++ {
|
|
|
|
p := projectFolder + "/" + cfg.Monitor.ExceptDirs[i]
|
|
|
|
dirs = arrayRemoveElement(dirs, p)
|
|
|
|
listFile(p, func(d string) {
|
|
|
|
dirs = arrayRemoveElement(dirs, d)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, dir := range dirs {
|
|
|
|
log.Println("watcher add -> ", dir)
|
|
|
|
err := watcher.Add(dir)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Println("fileboy is ready.")
|
|
|
|
}
|
|
|
|
|
|
|
|
func initWatcher() {
|
|
|
|
parseConfig()
|
|
|
|
var err error
|
|
|
|
watcher, err = fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
|
|
done := make(chan bool)
|
2019-01-02 11:07:52 +00:00
|
|
|
taskMan = newTaskMan(cfg.Command.DelayMillSecond, cfg.Notifier.CallUrl)
|
2018-09-10 08:23:27 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event, ok := <-watcher.Events:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
eventDispatcher(event)
|
|
|
|
case err, ok := <-watcher.Errors:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Println("error:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
addWatcher()
|
|
|
|
<-done
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseArgs() {
|
|
|
|
l := len(os.Args)
|
|
|
|
if l == 1 {
|
2018-09-10 10:00:28 +00:00
|
|
|
_, err := ioutil.ReadFile(projectFolder + "/filegirl.yaml")
|
|
|
|
if err != nil {
|
2018-09-19 03:26:57 +00:00
|
|
|
log.Println("the filegirl.yaml file does not exist! ", err)
|
2018-09-10 10:00:28 +00:00
|
|
|
fmt.Print(firstRunHelp)
|
|
|
|
fmt.Print(helpStr)
|
|
|
|
return
|
|
|
|
}
|
2018-09-10 08:23:27 +00:00
|
|
|
initWatcher()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if l == 2 {
|
|
|
|
c := os.Args[1]
|
|
|
|
switch c {
|
|
|
|
case "init":
|
|
|
|
err := ioutil.WriteFile(projectFolder+"/filegirl.yaml", []byte(exampleFileGirl), 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error create filegirl.yaml config! ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Println("create filegirl.yaml ok")
|
|
|
|
return
|
|
|
|
case "exec":
|
|
|
|
parseConfig()
|
2019-01-03 01:56:26 +00:00
|
|
|
newTaskMan(0, cfg.Notifier.CallUrl).run(new(changedFile))
|
2018-09-10 08:23:27 +00:00
|
|
|
return
|
2018-12-30 07:59:38 +00:00
|
|
|
case "version", "v", "-v", "--version":
|
|
|
|
fmt.Println(versionDesc)
|
2018-09-10 08:23:27 +00:00
|
|
|
default:
|
|
|
|
fmt.Print(helpStr)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func show() {
|
|
|
|
fmt.Print(logo)
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2018-12-30 07:59:38 +00:00
|
|
|
fmt.Println(englishSay[rand.Intn(len(englishSay))])
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println(statement)
|
2018-09-10 08:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
log.SetPrefix("[FileBoy]: ")
|
|
|
|
log.SetFlags(2)
|
|
|
|
show()
|
|
|
|
var err error
|
|
|
|
projectFolder, err = os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
parseArgs()
|
|
|
|
}
|