fileboy/fileboy.go

216 lines
4.4 KiB
Go
Raw Permalink Normal View History

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"
2019-01-08 07:49:21 +00:00
"strings"
2018-09-10 08:23:27 +00:00
"time"
)
const (
Version = 1
2019-01-19 09:10:07 +00:00
PreError = "ERROR:"
PreWarn = "Warn:"
2018-09-10 08:23:27 +00:00
)
var (
2019-01-03 02:27:04 +00:00
projectFolder = "."
2018-09-10 08:23:27 +00:00
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
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 {
2019-01-19 09:10:07 +00:00
log.Panicln(PreError, "read filegirl.yaml file err: ", err)
2018-09-10 08:23:27 +00:00
}
err = yaml.Unmarshal(fc, cfg)
if err != nil {
2019-01-19 09:10:07 +00:00
log.Panicln(PreError, "parsed filegirl.yaml failed: ", err)
2018-09-10 08:23:27 +00:00
}
if cfg.Core.Version > Version {
2019-01-19 09:10:07 +00:00
log.Panicln(PreError, "current fileboy support max version : ", Version)
2018-09-10 08:23:27 +00:00
}
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),
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 {
2019-01-19 09:10:07 +00:00
log.Fatalln(PreError, "filegirl section monitor dirs is error. ", cfg.Monitor.IncludeDirs[i])
2018-09-10 08:23:27 +00:00
}
2019-01-08 07:49:21 +00:00
if strings.HasPrefix(darr[0], "/") {
2019-01-19 09:10:07 +00:00
log.Fatalln(PreError, "dirs must be relative paths ! err path:", cfg.Monitor.IncludeDirs[i])
2019-01-08 07:49:21 +00:00
}
2018-09-10 08:23:27 +00:00
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 {
md := projectFolder + "/" + darr[0]
2018-09-10 08:23:27 +00:00
if len(darr) == 2 && darr[1] == "*" {
dirs = arrayUniqueAdd(dirs, md)
listFile(md, func(d string) {
2018-09-10 08:23:27 +00:00
dirs = arrayUniqueAdd(dirs, d)
})
} else {
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
}
2019-01-19 09:10:07 +00:00
log.Println(PreError, err)
2018-09-10 08:23:27 +00:00
}
}
}()
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 {
2019-01-19 09:10:07 +00:00
log.Println(PreError, "the filegirl.yaml file does not exist! ", err)
2018-09-10 10:00:28 +00:00
fmt.Print(firstRunHelp)
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 {
2019-01-19 09:10:07 +00:00
log.Println(PreError, "error create filegirl.yaml config! ", err)
2018-09-10 08:23:27 +00:00
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()
}