fileboy/fileboy.go

293 lines
6.2 KiB
Go
Raw 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-04-01 08:44:16 +00:00
"strconv"
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
2019-12-04 03:50:11 +00:00
ioeventMapStr = map[fsnotify.Op]string{
fsnotify.Write: "write",
fsnotify.Rename: "rename",
fsnotify.Remove: "remove",
fsnotify.Create: "create",
fsnotify.Chmod: "chmod",
}
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
2019-12-04 03:50:11 +00:00
Event 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-04-03 10:04:07 +00:00
log.Println(PreError, "the filegirl.yaml file in", projectFolder, "is not exist! ", err)
fmt.Print(firstRunHelp)
2019-06-04 03:12:41 +00:00
logAndExit("fileboy unable to run.")
2018-09-10 08:23:27 +00:00
}
err = yaml.Unmarshal(fc, cfg)
if err != nil {
2019-06-04 03:12:41 +00:00
logAndExit(PreError, "parsed filegirl.yaml failed: ", err)
2018-09-10 08:23:27 +00:00
}
if cfg.Core.Version > Version {
2019-06-04 03:12:41 +00:00
logAndExit(PreError, "current fileboy support max version : ", Version)
2018-09-10 08:23:27 +00:00
}
2019-04-01 08:18:01 +00:00
// init map
2019-04-01 07:32:27 +00:00
cfg.Monitor.TypesMap = map[string]bool{}
2019-04-01 08:18:01 +00:00
cfg.Monitor.IncludeDirsMap = map[string]bool{}
cfg.Monitor.ExceptDirsMap = map[string]bool{}
2019-12-06 03:51:28 +00:00
cfg.Monitor.IncludeDirsRec = map[string]bool{}
2019-04-01 08:18:01 +00:00
// convert to map
2019-04-01 07:32:27 +00:00
for _, v := range cfg.Monitor.Types {
cfg.Monitor.TypesMap[v] = true
}
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 &&
2019-04-01 07:32:27 +00:00
!keyInMonitorTypesMap(".*", cfg) &&
!keyInMonitorTypesMap(ext, cfg) {
2018-09-10 08:23:27 +00:00
return
}
2019-12-04 03:50:11 +00:00
op := ioeventMapStr[event.Op]
if len(cfg.Monitor.Events) != 0 && !inStrArray(op, cfg.Monitor.Events) {
return
2018-09-10 08:23:27 +00:00
}
2019-12-04 03:50:11 +00:00
log.Println("EVENT", event.Op.String(), ":", event.Name)
taskMan.Put(&changedFile{
Name: relativePath(projectFolder, event.Name),
Changed: time.Now().UnixNano(),
Ext: ext,
Event: op,
})
2018-09-10 08:23:27 +00:00
}
func addWatcher() {
2018-09-19 02:03:42 +00:00
log.Println("collecting directory information...")
dirsMap := map[string]bool{}
2019-04-01 08:18:01 +00:00
for _, dir := range cfg.Monitor.IncludeDirs {
darr := dirParse2Array(dir)
2018-09-10 08:23:27 +00:00
if len(darr) < 1 || len(darr) > 2 {
2019-06-04 03:12:41 +00:00
logAndExit(PreError, "filegirl section monitor dirs is error. ", dir)
2018-09-10 08:23:27 +00:00
}
2019-01-08 07:49:21 +00:00
if strings.HasPrefix(darr[0], "/") {
2019-06-04 03:12:41 +00:00
logAndExit(PreError, "dirs must be relative paths ! err path:", dir)
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] == "*" {
2019-04-01 08:18:01 +00:00
// The highest priority
dirsMap = map[string]bool{
projectFolder: true,
}
2018-09-10 08:23:27 +00:00
listFile(projectFolder, func(d string) {
2019-04-01 08:18:01 +00:00
dirsMap[d] = true
2018-09-10 08:23:27 +00:00
})
2019-12-06 03:51:28 +00:00
cfg.Monitor.IncludeDirsRec[projectFolder] = true
2019-04-01 08:18:01 +00:00
break
2018-09-10 08:23:27 +00:00
} else {
2019-04-01 08:18:01 +00:00
dirsMap[projectFolder] = true
2018-09-10 08:23:27 +00:00
}
} else {
md := projectFolder + "/" + darr[0]
2019-04-01 08:18:01 +00:00
dirsMap[md] = true
2018-09-10 08:23:27 +00:00
if len(darr) == 2 && darr[1] == "*" {
listFile(md, func(d string) {
2019-04-01 08:18:01 +00:00
dirsMap[d] = true
2018-09-10 08:23:27 +00:00
})
2019-12-06 03:51:28 +00:00
cfg.Monitor.IncludeDirsRec[md] = true
2018-09-10 08:23:27 +00:00
}
}
}
2019-04-01 08:18:01 +00:00
for _, dir := range cfg.Monitor.ExceptDirs {
if dir == "." {
2019-06-04 03:12:41 +00:00
logAndExit(PreError, "exceptDirs must is not project root path ! err path:", dir)
2019-04-01 08:18:01 +00:00
}
p := projectFolder + "/" + dir
delete(dirsMap, p)
2018-09-10 08:23:27 +00:00
listFile(p, func(d string) {
2019-04-01 08:18:01 +00:00
delete(dirsMap, d)
2018-09-10 08:23:27 +00:00
})
}
2019-04-01 08:18:01 +00:00
for dir := range dirsMap {
2018-09-10 08:23:27 +00:00
log.Println("watcher add -> ", dir)
err := watcher.Add(dir)
if err != nil {
2019-12-06 08:26:41 +00:00
logAndExit(PreError, err)
2018-09-10 08:23:27 +00:00
}
}
2019-04-01 08:44:16 +00:00
log.Println("total monitored dirs: " + strconv.Itoa(len(dirsMap)))
2018-09-10 08:23:27 +00:00
log.Println("fileboy is ready.")
2019-04-02 10:37:46 +00:00
cfg.Monitor.DirsMap = dirsMap
2018-09-10 08:23:27 +00:00
}
func initWatcher() {
var err error
2019-04-03 10:04:07 +00:00
if watcher != nil {
_ = watcher.Close()
}
2018-09-10 08:23:27 +00:00
watcher, err = fsnotify.NewWatcher()
if err != nil {
2019-06-04 03:12:41 +00:00
logAndExit(err)
2018-09-10 08:23:27 +00:00
}
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
}
2019-12-06 03:51:28 +00:00
// directory structure changes, dynamically add, delete and monitor according to rules
// TODO // this method cannot be triggered when the parent folder of the change folder is not monitored
go watchChangeHandler(event)
2018-09-10 08:23:27 +00:00
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()
}
2019-12-06 03:51:28 +00:00
func watchChangeHandler(event fsnotify.Event) {
if event.Op != fsnotify.Create && event.Op != fsnotify.Rename {
return
}
_, err := ioutil.ReadDir(event.Name)
if err != nil {
return
}
do := false
for rec := range cfg.Monitor.IncludeDirsRec {
if !strings.HasPrefix(event.Name, rec) {
continue
}
// check exceptDirs
has := false
for _, v := range cfg.Monitor.ExceptDirs {
if strings.HasPrefix(event.Name, projectFolder+"/"+v) {
has = true
}
}
if has {
continue
}
_ = watcher.Remove(event.Name)
err := watcher.Add(event.Name)
if err == nil {
do = true
log.Println("watcher add -> ", event.Name)
} else {
log.Println(PreWarn, "watcher add faild:", event.Name, err)
}
}
if do {
return
}
// check map
if _, ok := cfg.Monitor.DirsMap[event.Name]; ok {
_ = watcher.Remove(event.Name)
err := watcher.Add(event.Name)
if err == nil {
log.Println("watcher add -> ", event.Name)
} else {
log.Println(PreWarn, "watcher add faild:", event.Name, err)
}
}
}
2018-09-10 08:23:27 +00:00
func parseArgs() {
2019-04-03 10:04:07 +00:00
switch len(os.Args) {
case 1:
parseConfig()
done := make(chan bool)
2018-09-10 08:23:27 +00:00
initWatcher()
2019-04-03 10:04:07 +00:00
defer watcher.Close()
<-done
2018-09-10 08:23:27 +00:00
return
2019-04-03 10:04:07 +00:00
case 2:
2018-09-10 08:23:27 +00:00
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
2019-04-03 10:04:07 +00:00
default:
2019-06-04 03:12:41 +00:00
logAndExit("Unknown parameters, use `fileboy help` show help info.")
2018-09-10 08:23:27 +00:00
}
}
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)
2019-12-04 02:31:49 +00:00
log.SetOutput(os.Stdout)
2018-09-10 08:23:27 +00:00
show()
var err error
projectFolder, err = os.Getwd()
if err != nil {
2019-06-04 03:12:41 +00:00
logAndExit(err)
2018-09-10 08:23:27 +00:00
}
parseArgs()
}