Compare commits

...

61 Commits
v1.12 ... main

Author SHA1 Message Date
ehlxr c221fbc347 Merge branch 'master' into dev 2020-11-16 17:43:20 +08:00
ehlxr ecf0676d67 fix bug in should-finish instruction 2020-11-16 16:58:56 +08:00
ehlxr b4ad1e0e96 fix bug in should-finish instruction 2020-11-16 16:55:15 +08:00
ehlxr 195740fa3d support absolute dir 2020-11-11 15:19:32 +08:00
ehlxr 4f3ad86984 support absolute dir 2020-11-11 15:17:48 +08:00
ehlxr 4b3bf5e77c add profile config 2020-11-11 15:01:36 +08:00
ehlxr bf4f156714 add ignore suffix of the listener file 2020-11-10 17:33:45 +08:00
ehlxr 6a59036070 add ignore suffix of the listener file 2020-11-10 17:24:14 +08:00
ehlxr 8dc1ebe6d9 add ignore suffix of the listener file 2020-11-10 17:13:49 +08:00
dengsgo da54305188 增加 1.16 2020-10-17 10:29:33 +08:00
dengsgo 9e651d3023 优化 代码 2020-10-17 10:24:37 +08:00
dengsgo 605914d1e1 增加 1.16 2020-08-23 16:22:08 +08:00
dengsgo 86cf40564b 优化 文件扫描性能 2020-08-23 16:17:18 +08:00
dengsgo 819297118c 更新 changelog 2020-07-19 18:10:34 +08:00
dengsgo a76d4cfe6c 增加 对信号的处理 2020-07-19 18:06:24 +08:00
dengsgo 14ba79c2b3 更新 README 2020-06-27 15:15:29 +08:00
dengsgo c5f0649ab5 增加 pid文件进程感知 2020-06-27 15:11:55 +08:00
dengsgo 7c6f8f910b 增加 snap config 2020-04-22 15:40:09 +08:00
dengsgo d07616bde9 rename 2020-03-16 11:19:13 +08:00
dengsgo 4f81692954 typo deamon->daemon 2020-03-16 11:16:14 +08:00
Deng.Liu 588af5143f
Update README.md 2020-03-15 21:10:11 +08:00
Deng.Liu 2d8924b7fe
Update go.yml 2020-03-15 21:02:32 +08:00
Deng.Liu 08fb7aabf8
Update go.yml 2020-03-15 20:56:56 +08:00
Deng.Liu 9f0a51ce0e
Update go.yml 2020-03-15 20:55:31 +08:00
Deng.Liu 8a3a2d570b
Update go.yml 2020-03-15 20:53:42 +08:00
Deng.Liu e5405f4fa9
Update go.yml 2020-03-15 20:46:58 +08:00
Deng.Liu 9d9cce0f31
Create go.yml 2020-03-15 20:43:56 +08:00
dengsgo c0364a62bc 发行 v1.15 2020-03-08 13:26:38 +08:00
dengsgo a9758653ba 修改 changelog 2020-03-08 13:24:49 +08:00
dengsgo b1c5f7da39 修改 readme mod 2020-03-05 15:37:36 +08:00
dengsgo 29fc62de90 更改 mod管理依赖 2020-03-05 15:31:47 +08:00
dengsgo 2d409fbce0 修改 log 2020-03-04 13:41:02 +08:00
dengsgo 550dc01dfb 修改 readme 2020-03-04 11:18:21 +08:00
dengsgo d0492bad8c 更新 readme 2020-03-03 15:01:08 +08:00
dengsgo 55625d51bc 增加 支持 2020-03-03 14:57:35 +08:00
dengsgo 8ddc124f89 增加 支持 2020-03-03 14:54:12 +08:00
dengsgo 8b7042411c 修改 rekeased 2020-03-03 11:39:15 +08:00
dengsgo b72caedcbb 重构 指令 2020-03-03 11:37:20 +08:00
dengsgo f989590d14 修改 文案 2020-03-03 11:12:08 +08:00
dengsgo f2afa015bd 修改 版本 2020-01-05 14:47:46 +08:00
dengsgo dc7162b57d 修改 配置 2020-01-05 14:44:56 +08:00
dengsgo 8828bcaed6 修复 错误 2020-01-02 18:32:16 +08:00
dengsgo 7d2e8dd34d 修复 panic 2020-01-02 18:22:16 +08:00
dengsgo bb80977213 更新 changelog 2020-01-02 17:42:02 +08:00
dengsgo 1b4206a6b3 增加 ignore*指令支持 2020-01-02 17:38:52 +08:00
dengsgo 3351429515 重构 log 2020-01-02 17:25:02 +08:00
dengsgo 00e34193aa 更新 changelog 2020-01-02 16:58:01 +08:00
dengsgo 4e0b9f54f6 增加 指令支持 2020-01-02 16:38:26 +08:00
dengsgo 9defc968ba 优化 taskman 2019-12-27 18:09:56 +08:00
dengsgo 66eee100f2 忽略 pid 2019-12-27 16:46:36 +08:00
dengsgo 9e4642d0c2 更新 logo出现的时机 2019-12-27 16:43:49 +08:00
dengsgo 44bb0dd65f 增加 未知参数提醒 2019-12-27 16:31:00 +08:00
dengsgo eeaad58d1e 更新 .travis 2019-12-24 11:50:21 +08:00
dengsgo 44051b9184 更新 .travis 2019-12-24 11:47:21 +08:00
dengsgo a2daa98d08 更新 .travis 2019-12-24 11:35:51 +08:00
dengsgo 97bdcb644c 忽略 pid监听 2019-12-24 11:27:38 +08:00
dengsgo a0137807f3 update echo PATH 2019-12-23 16:26:20 +08:00
dengsgo b0a0e296d2 update echo PATH 2019-12-23 16:23:28 +08:00
dengsgo e63d200382 更新 .travis 2019-12-23 16:18:03 +08:00
dengsgo cb86b0dbe6 增加 守护进程 2019-12-23 16:13:31 +08:00
dengsgo bead88c5e2 更新 travis script 2019-12-17 19:28:46 +08:00
19 changed files with 470 additions and 460 deletions

45
.github/workflows/go.yml vendored Normal file
View File

@ -0,0 +1,45 @@
name: Go
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.13
uses: actions/setup-go@v1
with:
go-version: 1.13
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Get dependencies
run: |
export GO111MODULE=on
go env -w GOPROXY=https://goproxy.io,direct
go get
- name: Build
run: go build
- name: Use
run: |
export PATH=/home/runner/work/fileboy/fileboy:$PATH
fileboy version
fileboy help
fileboy init
cat filegirl.yaml
fileboy exec
fileboy daemon
ls -al .fileboy.pid
ps aux | grep fileboy
fileboy stop

1
.gitignore vendored
View File

@ -6,3 +6,4 @@ fileboy-darwin-amd64.bin
fileboy-linux-amd64.bin
fileboy-windows-amd64.exe
./bin/*
.fileboy.pid

View File

@ -11,11 +11,17 @@ dist: bionic # ubuntu 18.04
script:
- make init
- make build-linux
- ls bin
- ./bin/fileboy-linux-amd64.bin version
- ./bin/fileboy-linux-amd64.bin help
- ./bin/fileboy-linux-amd64.bin init
- make build-all
- ls -al bin
- echo $PATH
- cp bin/fileboy-linux-amd64.bin /home/travis/bin/fileboy
- fileboy version
- fileboy help
- fileboy init
- cat filegirl.yaml
- ./bin/fileboy-linux-amd64.bin exec
- fileboy exec
- fileboy daemon
- ls -al .fileboy.pid
- ps aux | grep fileboy
- fileboy stop

View File

@ -1,3 +1,45 @@
### Release v1.16
2020.08.23
- 优化 文件扫描性能
2020.07.19
- 增加 pid 文件处理
- 增加 信息处理
2020.03.16
- typo deamon->daemon
### Release v1.15
2020.03.08
- 优化 指令模式
- 使用 mod 管理依赖
- go version >= 1.13
- 优化 一些细节
2020.01.02
- 增加 指令配置项 `instruction`, 可以通过预定义的指令来控制 command 的行为
- 增加 `should-finish` 指令
- 增加 `exec-when-start` 指令
- 增加 `ignore-warn` 指令
- 增加 `ignore-info` 指令
- 增加 `ignore-stdout` 指令
- 增加 `ignore-exec-error` 指令
2019.12.28
- 增加 `deamon`命令,支持以守护进程的方式运行在后台 **Unix only**
- 增加 `stop`命令,用来停止 deamon 进程 **Unix only**
- 优化 exec stdout
### Release v1.12
2019.12.18

View File

@ -10,8 +10,7 @@
# make build-start-mac 编译+启动
init:
go get -u gopkg.in/yaml.v2
go get -u gopkg.in/fsnotify/fsnotify.v1
go get -u
build-mac: ;@echo "编译-mac版";
CGO_ENABLED=0 GOARCH=amd64 GOOS=darwin go build -ldflags "-s -w" -o ./bin/fileboy-darwin-amd64.bin

201
README.md
View File

@ -1,202 +1,3 @@
## 项目说明
[![Build Status](https://travis-ci.org/dengsgo/fileboy.svg?branch=master)](https://travis-ci.org/dengsgo/fileboy) [![Go Report Card](https://goreportcard.com/badge/github.com/dengsgo/fileboy)](https://goreportcard.com/report/github.com/dengsgo/fileboy)
[简体中文](README.md) | [ENGLISH](README_EN.md)
fileboy文件变更监听通知系统使用 Go 编写。
适用于 Hot Reload 典型的如开发go项目无需每次手动执行 go build又比如前端 node 打包) 或者 系统监控的场景。
## 特性
- 极简的用法和配置
- 支持多平台Windows/Linux/MacOS
- 支持自定义文件监听范围,监听指定文件夹/不监听指定文件夹/指定后缀文件
- 支持自定义监控事件write/rename/remove/create/chmod
- 支持设置多条命令
- 命令支持变量占位符
- 支持冗余任务丢弃,自定义冗余任务范围
- 支持 http 通知
- 更多...
## 编译环境
go version 1.13
## 更新日志
[CHANGELOG](CHANGELOG.md)
## 运行
### 下载二进制文件
Github: [download v1.12](https://github.com/dengsgo/fileboy/releases)
Gitee: [dowmload v1.12](https://gitee.com/dengsgo/fileboy/releases)
下载已经编译好的对应平台二进制文件,重命名为`fileboy`, 加入系统 Path 中即可。
### 源码编译
clone 该项目,进入主目录,运行命令:
```shell
## 安装依赖
go get -u gopkg.in/fsnotify/fsnotify.v1
go get -u gopkg.in/yaml.v2
## 编译
go build
## 运行
./fileboy
```
## 使用
fileboy 的正常运行依赖于 `filegirl.yaml` 配置文件,因此首次在项目中使用需要初始化 `filegirl.yaml`
- 进入你想要 hot reload 的项目主目录下;
- 运行 `fileboy init`,会在该目录下生成 `filegirl.yaml`文件;
- 查看 `filegirl.yaml`,修改为适合自己项目的配置项;
- 运行 `fileboy`即可.
如果你定义了 `command -> exec`命令,想事先确认是否能正常执行,可以运行 `fileboy exec`命令,系统会尝试运行你的自定义命令。
你可以使用 `fileboy help`查看使用帮助。
## filegirl.yaml 配置文件说明
```yaml
# 主配置
core:
# 配置版本号
version: 1
# 监控配置
monitor:
# 要监听的目录
# test1 监听当前目录下 test1 目录
# test1/test2 监听当前目录下 test1/test2 目录
# test1,* 监听当前目录下 test1 目录及其所有子目录(递归)
# .,* 监听当前目录及其所有子目录(递归)
includeDirs:
- .,*
# 不监听的目录
# .idea 忽略.idea目录及其所有子目录的监听
exceptDirs:
- .idea
- .git
- .vscode
- node_modules
- vendor
# 监听文件的格式,此类文件更改会执行 command 中的命令
# .go 后缀为 .go 的文件更改,会执行 command 中的命令
# .* 所有的文件更改都会执行 command 中的命令
types:
- .go
# 监听的事件类型,发生此类事件才执行 command 中的命令
# 没有该配置默认监听所有事件
# write 写入文件事件
# rename 重命名文件事件
# remove 移除文件事件
# create 创建文件事件
# chmod 更新文件权限事件(类unix)
events:
- write
- rename
- remove
- create
- chmod
# 命令
command:
# 监听的文件有更改会执行的命令
# 可以有多条命令,会依次执行
# 如有多条命令,每条命令都会等待上一条命令执行完毕后才会执行
# 如遇交互式命令,允许外部获取输入
# 支持变量占位符,运行命令时会替换成实际值:
# {{file}} 文件名(如 a.txt 、test/test2/a.go)
# {{ext}} 文件后缀(如 .go)
# {{event}} 事件(上面的events, 如 write)
# {{changed}} 文件更新的本地时间戳(纳秒,如 1537326690523046400)
# 变量占位符使用示例cp {{file}} /root/sync -rf 、 myCommand --{{ext}} {{changed}}
exec:
- go version
- go env
# 文件变更后命令在xx毫秒后才会执行单位为毫秒
# 一个变更事件(A)如果在定义的延迟时间(t)内,又有新的文件变更事件(B)那么A会取消执行。
# B及以后的事件均依次类推直到事件Z在t内没有新事件产生Z 会执行
# 合理设置延迟时间,将有效减少冗余和重复任务的执行
# 如果不需要该特性,设置为 0
delayMillSecond: 2000
# 通知器
notifier:
# 文件更改会向该 url 发送请求POST 一段 json 文本数据)
# 触发请求的时机和执行 command 命令是一致的
# 请求超时 15 秒
# POST 格式:
# Content-Type: application/json;charset=UTF-8
# User-Agent: FileBoy Net Notifier v1.12
# Body: {"project_folder":"/project/path","file":"main.go","changed":1576567861913824940,"ext":".go","event":"write"}
# 例: http://example.com/notifier/fileboy-listener
# 不启用通知,请留空 ""
callUrl: ""
```
### TODO
- [x] 命令支持变量占位符
- [x] 支持多命令
- [x] 支持监听指定文件夹
- [x] 支持不监听指定文件夹
- [x] 支持监听指定后缀文件
- [x] 支持自定事件监听
- [x] 支持 http 通知
- [x] 支持冗余任务丢弃
- [ ] 支持 http 合并任务的通知
## QA
#### 很多框架都自带了 hot reload 的功能,为什么还要单独写个 fileboy 呢?
这个是一款通用的 hot reload 的软件,理论上适用于任何需要 hot reload 的场景,并不局限于语言层面上。只要灵活的配置 `filegirl.yaml`文件就行了。
#### fileboy 可以应用在那些具体的场景?
在开发中,我们很需要一款可以帮助我们自动打包编译的工具,那 fileboy 就非常适合这样的场景。比如 go 项目的热编译,让我们可以边修改代码边运行得到反馈。又比如 PHP Swoole 框架由于常驻进程的原因无法更改代码立即reload使用 fileboy 就可以辅助做到传统 PHP 开发的体验。
对于一些需要监控文件日志或者配置变动的场景, fileboy 同样适合。你可以事先编写好相应的通知报警脚本,然后定义`filegirl.yaml`中的`command`命令,交由 fileboy 自动运行监控报警。
#### 通知器在什么时候会发送 http 请求 ?
通知器发送 http 通知的前提是在配置文件中设置了 `callUrl` 参数(不为空即为已设置)。触发请求的时机和执行 command 命令是一致的,`command -> delayMillSecond` 参数对于触发器同样有效。请求超时默认15秒.
#### idea 下更改文件,为什么会执行两次或者多次 command ?
由于 idea 系列软件特殊的文件保存策略他会自动创建一些临时文件并且在需要时多次重写文件所以有时反映在文件上就是有多次的更改所以会出现这种情况。1.5之后的版本增加了 `delayMillSecond` 参数,可以解决这个问题。
#### filegirl.yaml 里面的 command 如何配置复杂命令?
fileboy 目前支持 `命令 + 参数`这种形式的 command而且 参数中不能有""符号或者有空格。如:
`go build`:支持;
`go env`:支持;
`php swoole start --daemon`:支持
`cat a.txt | grep "q" | wc -l`:不支持
对于不支持的命令,可以把它写到一个文件里,然后在 command 中执行这个文件来解决。
#### 为什么起名为 fileboy又把配置名叫做 filegirl
因为爱情~~ (◡ᴗ◡✿)
### 贡献者
> 排名不分先后
[@dengsgo](https://www.yoytang.com) <dengsgo@gmail.com>
[@itwesley](https://github.com/itwesley) <wcshen1126@gmail.com>
[@jason-gao](https://github.com/jason-gao) <3048789891@qq.com>
基于 [fileboy](https://github.com/dengsgo/fileboy) 魔改版

View File

@ -1,150 +0,0 @@
## FILEBOY
[![Build Status](https://travis-ci.org/dengsgo/fileboy.svg?branch=master)](https://travis-ci.org/dengsgo/fileboy) [![Go Report Card](https://goreportcard.com/badge/github.com/dengsgo/fileboy)](https://goreportcard.com/report/github.com/dengsgo/fileboy)
[简体中文](README.md) | [ENGLISH](README_EN.md)
Fileboy, File Change Monitoring Notification System, written with Go.
For Hot Reload scenarios (typically for developing go projects without having to perform go build manually every time; for example, front-end node packaging) or system monitoring.
## FEATURES
- Minimalist usage and configuration
- Support multiple platforms, Windows/Linux/MacOS
- Supports custom file listening scope, listening for specified folders/not listening for specified folders/specified suffix files
- Support for custom monitoring events (write / rename / remove / create / chmod)
- Support for setting up multiple commands
- Command support variable placeholders
- Supporting redundant task discarding and customizing redundant task scope
- Supporting HTTP notifications
- more...
## COMPILE
go version 1.13
## CHANGELOG
[CHANGELOG](CHANGELOG.md)
## RUN
### BINARIES
Github: [download v1.12](https://github.com/dengsgo/fileboy/releases)
Gitee: [dowmload v1.12](https://gitee.com/dengsgo/fileboy/releases)
Download the compiled binary file of the corresponding platform, rename it `fileboy`, and add it to the system Path.
### SOURCE
Clone project, enter the project directory, run the command:
```shell
## installation dependency
go get-u gopkg.in/fsnotify/fsnotify.v1
go get-u gopkg.in/yaml.v2
## compile
go build
## run
./fileboy
```
## USAGE
The normal operation of fileboy depends on the `filegirl.yaml` configuration, so for the first time in a project, `filegirl.yaml` needs to be initialized.
- Enter the project home directory where you want hot reload;
- Running `fileboy init` will generate `filegirl. yaml` files in this directory.
- View `filegirl. yaml` and modify it to a configuration item suitable for your project.
- Run `fileboy`.
If you define the `command-> exec` command, you can run the `fileboy exec` command to confirm whether it can be executed properly in advance. The system will try to run your custom command.
You can use `fileboy help` to see help info.
## filegirl.yaml
```yaml
core:
# config version code
version: 1
# monitor section
monitor:
# directories to monitor
# test1 listen for the test1 directory in the project directory
# test1/test2 listen for the test1/test2 directory in the project directory
# test1,* listen for the test1 directory in the project directory and all its subdirectories (recursion)
# .,* listen for the project directory and all its subdirectories (recursion)
includeDirs:
- .,*
# Unmonitored directories
# .idea ignore listening to .idea directory and all its subdirectories
exceptDirs:
- .idea
- .git
- .vscode
- node_modules
- vendor
# the suffix of the listener file, which changes the file to execute commands
# .go file changes suffixed with .go execute commands
# .* all file changes execute commands in the command
types:
- .go
# the type of event to listen to. Only when such an event occurs can the command in command be executed
# without this configuration, all events will be monitored by default
# write write file event
# rename rename file event
# remove remove remove file event
# create create file event
# chmod update file permission event (UNIX like)
events:
- write
- rename
- remove
- create
- chmod
command:
# the files monitored have commands that change to be executed
# there can be multiple commands that will be executed in turn
# in case of interactive commands, allow external access to input
# variable placeholders are supported, and the actual values are replaced when the command is run:
# {{file}} (e.g: a.txt 、test/test2/a.go)
# {{ext}} (e.g: .go)
# {{event}} event(e.g: write)
# {{changed}} local timestamp for file updated(nanosecond,e.g 1537326690523046400)
# variable placeholders e.gcp {{file}} /root/sync -rf 、 myCommand --{{ext}} {{changed}}
exec:
- go version
- go env
# the command will not execute until XX milliseconds after the file changes
# a change event (A) cancels execution if there is a new file change event (B) within the defined delay time (t).
# B and subsequent events are analogized in turn until event Z does not produce new events within t, and Z executes.
# reasonable setting of delay time will effectively reduce redundancy and duplicate task execution.
# If this feature is not required, set to 0
delayMillSecond: 2000
notifier:
# file changes send requests to the URL (POST JSON text data)
# the timing of triggering the request is consistent with executing the command command
# timeout 15 second
# POST :
# Content-Type: application/json;charset=UTF-8
# User-Agent: FileBoy Net Notifier v1.12
# Body: {"project_folder":"/project/path","file":"main.go","changed":1576567861913824940,"ext":".go","event":"write"}
# e.g: http://example.com/notifier/fileboy-listener
# no notice is enabled. Please leave it blank. ""
callUrl: ""
```
### CONTRIBUTOR
[@dengsgo](https://www.yoytang.com) <dengsgo@gmail.com>
[@itwesley](https://github.com/itwesley) <wcshen1126@gmail.com>
[@jason-gao](https://github.com/jason-gao) <3048789891@qq.com>

56
daemon.go Normal file
View File

@ -0,0 +1,56 @@
package main
import (
"io/ioutil"
"os"
"os/exec"
"runtime"
"strconv"
)
func getPidFile() string {
return projectFolder + "/.fileboy.pid"
}
func runAsDaemon() (int, error) {
if runtime.GOOS == "windows" {
logAndExit("daemons mode cannot run on windows.")
}
err := stopDaemon()
if err != nil {
logAndExit(err)
}
_, err = exec.LookPath("fileboy")
if err != nil {
logAndExit("cannot found `fileboy` command in the PATH")
}
daemon := exec.Command("fileboy")
daemon.Dir = projectFolder
daemon.Env = os.Environ()
daemon.Stdout = os.Stdout
err = daemon.Start()
if err != nil {
logAndExit(err)
}
pid := daemon.Process.Pid
if pid != 0 {
ioutil.WriteFile(getPidFile(), []byte(strconv.Itoa(pid)), 0644)
}
return pid, nil
}
func stopDaemon() error {
bs, err := ioutil.ReadFile(getPidFile())
if err != nil {
return nil
}
_ = exec.Command("kill", string(bs)).Run()
os.Remove(getPidFile())
return nil
}
func stopSelf() {
pid := os.Getpid()
os.Remove(getPidFile())
_ = exec.Command("kill", strconv.Itoa(pid)).Run()
}

View File

@ -2,23 +2,30 @@ package main
import (
"fmt"
"gopkg.in/fsnotify/fsnotify.v1"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"math/rand"
"os"
"os/signal"
"path"
"strconv"
"strings"
"syscall"
"time"
"gopkg.in/fsnotify/fsnotify.v1"
"gopkg.in/yaml.v2"
)
const (
Version = 1
PreError = "ERROR:"
PreWarn = "Warn:"
InstExecWhenStart = "exec-when-start"
InstShouldFinish = "should-finish"
InstIgnoreWarn = "ignore-warn"
InstIgnoreInfo = "ignore-info"
InstIgnoreStdout = "ignore-stdout"
InstIgnoreExecError = "ignore-exec-error"
)
var (
@ -48,38 +55,54 @@ type changedFile struct {
Event string
}
func parseConfig() {
func parseConfig(filegirlYamlPath string) {
cfg = new(FileGirl)
fc, err := ioutil.ReadFile(getFileGirlPath())
fc, err := ioutil.ReadFile(filegirlYamlPath)
if err != nil {
log.Println(PreError, "The filegirl.yaml file in", projectFolder, "is not exist! ", err)
logError("the filegirl.yaml file in", projectFolder, "is not exist! ", err)
fmt.Print(firstRunHelp)
logAndExit("Fileboy unable to run.")
logAndExit("fileboy unable to run.")
}
err = yaml.Unmarshal(fc, cfg)
if err != nil {
logAndExit(PreError, "Parsed filegirl.yaml failed: ", err)
logAndExit("parsed filegirl.yaml failed: ", err)
}
if cfg.Core.Version > Version {
logAndExit(PreError, "Current fileboy support max version : ", Version)
logAndExit("current fileboy support max version : ", Version)
}
// init map
cfg.Monitor.TypesMap = map[string]bool{}
cfg.Monitor.ExceptTypesMap = map[string]bool{}
cfg.Monitor.IncludeDirsMap = map[string]bool{}
cfg.Monitor.ExceptDirsMap = map[string]bool{}
cfg.Monitor.IncludeDirsRec = map[string]bool{}
cfg.InstructionMap = map[string]bool{}
// convert to map
for _, v := range cfg.Monitor.Types {
cfg.Monitor.TypesMap[v] = true
}
log.Println(cfg)
for _, v := range cfg.Instruction {
cfg.InstructionMap[v] = true
}
for _, v := range cfg.Monitor.ExceptTypes {
cfg.Monitor.ExceptTypesMap[v] = true
}
log.Printf("%+v", cfg)
}
func eventDispatcher(event fsnotify.Event) {
if event.Name == getPidFile() {
return
}
ext := path.Ext(event.Name)
if len(cfg.Monitor.Types) > 0 &&
!keyInMonitorTypesMap(".*", cfg) &&
!keyInMonitorTypesMap(ext, cfg) {
!keyInMonitorTypesMap(".*", cfg.Monitor.TypesMap) &&
!keyInMonitorTypesMap(ext, cfg.Monitor.TypesMap) {
return
}
if len(cfg.Monitor.ExceptTypes) > 0 &&
keyInMonitorTypesMap(ext, cfg.Monitor.ExceptTypesMap) {
return
}
@ -97,16 +120,21 @@ func eventDispatcher(event fsnotify.Event) {
}
func addWatcher() {
log.Println("collecting directory information...")
logInfo("collecting directory information...")
dirsMap := map[string]bool{}
for _, dir := range cfg.Monitor.ExceptDirs {
if dir == "." {
logAndExit("exceptDirs must is not project root path ! err path:", dir)
}
}
for _, dir := range cfg.Monitor.IncludeDirs {
darr := dirParse2Array(dir)
if len(darr) < 1 || len(darr) > 2 {
logAndExit(PreError, "filegirl section monitor dirs is error. ", dir)
}
if strings.HasPrefix(darr[0], "/") {
logAndExit(PreError, "dirs must be relative paths ! err path:", dir)
logAndExit("filegirl section monitor dirs is error. ", dir)
}
//if strings.HasPrefix(darr[0], "/") {
// logAndExit("dirs must be relative paths ! err path:", dir)
//}
if darr[0] == "." {
if len(darr) == 2 && darr[1] == "*" {
// The highest priority
@ -122,7 +150,8 @@ func addWatcher() {
dirsMap[projectFolder] = true
}
} else {
md := projectFolder + "/" + darr[0]
//md := projectFolder + "/" + darr[0]
md := darr[0]
dirsMap[md] = true
if len(darr) == 2 && darr[1] == "*" {
listFile(md, func(d string) {
@ -133,25 +162,16 @@ func addWatcher() {
}
}
for _, dir := range cfg.Monitor.ExceptDirs {
if dir == "." {
logAndExit(PreError, "exceptDirs must is not project root path ! err path:", dir)
}
p := projectFolder + "/" + dir
delete(dirsMap, p)
listFile(p, func(d string) {
delete(dirsMap, d)
})
}
for dir := range dirsMap {
log.Println("watcher add -> ", dir)
logInfo("watcher add -> ", dir)
err := watcher.Add(dir)
if err != nil {
logAndExit(PreError, err)
logAndExit(err)
}
}
log.Println("total monitored dirs: " + strconv.Itoa(len(dirsMap)))
log.Println("fileboy is ready.")
logInfo("total monitored dirs: " + strconv.Itoa(len(dirsMap)))
logInfo("fileboy is ready.")
cfg.Monitor.DirsMap = dirsMap
}
@ -180,7 +200,7 @@ func initWatcher() {
if !ok {
return
}
log.Println(PreError, err)
logError(err)
}
}
}()
@ -188,6 +208,15 @@ func initWatcher() {
}
func watchChangeHandler(event fsnotify.Event) {
// stop the fileboy daemon process when the .fileboy.pid file is changed
if event.Name == getPidFile() &&
(event.Op == fsnotify.Remove ||
event.Op == fsnotify.Write ||
event.Op == fsnotify.Rename) {
logUInfo("exit daemon process")
stopSelf()
return
}
if event.Op != fsnotify.Create && event.Op != fsnotify.Rename {
return
}
@ -201,13 +230,7 @@ func watchChangeHandler(event fsnotify.Event) {
continue
}
// check exceptDirs
has := false
for _, v := range cfg.Monitor.ExceptDirs {
if strings.HasPrefix(event.Name, projectFolder+"/"+v) {
has = true
}
}
if has {
if hitDirs(event.Name, &cfg.Monitor.ExceptDirs) {
continue
}
@ -215,9 +238,9 @@ func watchChangeHandler(event fsnotify.Event) {
err := watcher.Add(event.Name)
if err == nil {
do = true
log.Println("watcher add -> ", event.Name)
logInfo("watcher add -> ", event.Name)
} else {
log.Println(PreWarn, "watcher add faild:", event.Name, err)
logWarn("watcher add faild:", event.Name, err)
}
}
@ -230,53 +253,100 @@ func watchChangeHandler(event fsnotify.Event) {
_ = watcher.Remove(event.Name)
err := watcher.Add(event.Name)
if err == nil {
log.Println("watcher add -> ", event.Name)
logInfo("watcher add -> ", event.Name)
} else {
log.Println(PreWarn, "watcher add faild:", event.Name, err)
logWarn("watcher add faild:", event.Name, err)
}
}
}
func parseArgs() {
switch len(os.Args) {
case 1:
parseConfig()
switch {
case len(os.Args) == 1:
show()
parseConfig(getFileGirlPath())
done := make(chan bool)
initWatcher()
defer watcher.Close()
if keyInInstruction(InstExecWhenStart) {
taskMan.run(new(changedFile))
}
<-done
return
case 2:
case len(os.Args) > 1:
c := os.Args[1]
switch c {
case "deamon", "daemon":
pid, err := runAsDaemon()
if err != nil {
logAndExit(err)
}
logUInfo("PID:", pid)
logUInfo("fileboy is ready. the main process will run as a daemons")
return
case "stop":
err := stopDaemon()
if err != nil {
logAndExit(err)
}
logUInfo("fileboy daemon is stoped.")
return
case "init":
_, err := ioutil.ReadFile(getFileGirlPath())
if err == nil {
log.Println(PreError, "Profile filegirl.yaml already exists.")
logAndExit("If you want to regenerate filegirl.yaml, delete it first")
logError("profile filegirl.yaml already exists.")
logAndExit("delete it first when you want to regenerate filegirl.yaml")
}
err = ioutil.WriteFile(getFileGirlPath(), []byte(exampleFileGirl), 0644)
if err != nil {
log.Println(PreError, "Profile filegirl.yaml create failed! ", err)
logError("profile filegirl.yaml create failed! ", err)
return
}
log.Println("Profile filegirl.yaml created ok")
logUInfo("profile filegirl.yaml created ok")
return
case "exec":
parseConfig()
parseConfig(getFileGirlPath())
newTaskMan(0, cfg.Notifier.CallUrl).run(new(changedFile))
return
case "profile":
filegirlYamlPath := os.Args[2]
show()
parseConfig(filegirlYamlPath)
done := make(chan bool)
initWatcher()
defer watcher.Close()
if keyInInstruction(InstExecWhenStart) {
taskMan.run(new(changedFile))
}
<-done
return
case "version", "v", "-v", "--version":
fmt.Println(versionDesc)
default:
case "help", "--help", "--h", "-h":
fmt.Print(helpStr)
default:
logAndExit("unknown parameter, use 'fileboy help' to view available commands")
}
return
default:
logAndExit("Unknown parameters, use `fileboy help` show help info.")
logAndExit("unknown parameters, use `fileboy help` show help info.")
}
}
func signalHandler() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
if taskMan != nil && taskMan.cmd != nil && taskMan.cmd.Process != nil {
if err := taskMan.cmd.Process.Kill(); err != nil {
logError("stopping the process failed: PID:", taskMan.cmd.ProcessState.Pid(), ":", err)
}
}
os.Exit(0)
}()
}
func getFileGirlPath() string {
return projectFolder + "/" + filegirlYamlName
}
@ -293,11 +363,12 @@ func main() {
log.SetPrefix("[FileBoy]: ")
log.SetFlags(2)
log.SetOutput(os.Stdout)
show()
// show()
var err error
projectFolder, err = os.Getwd()
if err != nil {
logAndExit(err)
}
signalHandler()
parseArgs()
}

View File

@ -6,11 +6,13 @@ type FileGirl struct {
}
Monitor struct {
Types []string `yaml:"types"`
ExceptTypes []string `yaml:"exceptTypes"`
IncludeDirs []string `yaml:"includeDirs"`
ExceptDirs []string `yaml:"exceptDirs"`
Events []string `yaml:"events"`
// convert to
TypesMap map[string]bool `yaml:"-"`
ExceptTypesMap map[string]bool `yaml:"-"`
IncludeDirsMap map[string]bool `yaml:"-"`
ExceptDirsMap map[string]bool `yaml:"-"`
DirsMap map[string]bool `yaml:"-"`
@ -24,4 +26,8 @@ type FileGirl struct {
Notifier struct {
CallUrl string `yaml:"callUrl"`
}
Instruction []string `yaml:"instruction"`
// convert to
InstructionMap map[string]bool `yaml:"-"`
}

10
go.mod Normal file
View File

@ -0,0 +1,10 @@
module fileboy
go 1.13
require (
github.com/fsnotify/fsnotify v1.4.9 // indirect
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211 // indirect
gopkg.in/fsnotify/fsnotify.v1 v1.4.7
gopkg.in/yaml.v2 v2.3.0
)

11
go.sum Normal file
View File

@ -0,0 +1,11 @@
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211 h1:9UQO31fZ+0aKQOFldThf7BKPMJTiBfWycGh/u3UoO88=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify/fsnotify.v1 v1.4.7 h1:XNNYLJHt73EyYiCZi6+xjupS9CpvmiDgjPTAjrBlQbo=
gopkg.in/fsnotify/fsnotify.v1 v1.4.7/go.mod h1:Fyux9zXlo4rWoMSIzpn9fDAYjalPqJ/K1qJ27s+7ltE=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -3,7 +3,6 @@ package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
"strings"
"time"
@ -35,7 +34,7 @@ func newNetNotifier(callUrl string) *NetNotifier {
func (n *NetNotifier) Put(cf *changedFile) {
if !n.CanPost {
log.Println(PreWarn, "notifier call url ignore. ", n.CallUrl)
logWarn("notifier call url ignore. ", n.CallUrl)
return
}
n.dispatch(&postParams{
@ -50,7 +49,7 @@ func (n *NetNotifier) Put(cf *changedFile) {
func (n *NetNotifier) dispatch(params *postParams) {
b, err := json.Marshal(params)
if err != nil {
log.Println(PreError, "json.Marshal n.params. ", err)
logError("json.Marshal n.params. ", err)
return
}
client := &http.Client{
@ -58,14 +57,14 @@ func (n *NetNotifier) dispatch(params *postParams) {
}
req, err := http.NewRequest("POST", n.CallUrl, bytes.NewBuffer(b))
if err != nil {
log.Println(PreError, "http.NewRequest. ", err)
logError("http.NewRequest. ", err)
return
}
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
req.Header.Set("User-Agent", "FileBoy Net Notifier v1.12")
req.Header.Set("User-Agent", "FileBoy Net Notifier v1.16")
resp, err := client.Do(req)
if err != nil {
log.Println(PreError, "notifier call failed. err:", err)
logError("notifier call failed. err:", err)
return
}
defer func() {
@ -76,5 +75,5 @@ func (n *NetNotifier) dispatch(params *postParams) {
if resp.StatusCode >= 300 {
// todo retry???
}
log.Println("notifier done .")
logInfo("notifier done .")
}

35
raw.go
View File

@ -32,6 +32,11 @@ monitor:
types:
- .go
# command
# .DS_Store .DS_Store command
exceptTypes:
- .DS_Store
# command
#
# write
@ -76,11 +81,27 @@ notifier:
# 15
# POST :
# Content-Type: application/json;charset=UTF-8
# User-Agent: FileBoy Net Notifier v1.12
# User-Agent: FileBoy Net Notifier v1.16
# Body: {"project_folder":"/project/path","file":"main.go","changed":1576567861913824940,"ext":".go","event":"write"}
# : http://example.com/notifier/fileboy-listener
# ""
callUrl: ""
#
instruction:
# command
#
# exec-when-start fileboy 'exec'
# should-finish 'exec' (C)(L)退 L 退 kill L exit code
# L 退(N) C N
# ignore-stdout 'exec' stdout
# ignore-warn fileboy warn
# ignore-info fileboy info
# ignore-exec-error 'exec' 退
#- should-finish
#- exec-when-start
- ignore-warn
`
var firstRunHelp = ` fileboy ?
@ -96,6 +117,12 @@ Usage of fileboy:
fileboy, filegirl.yaml
exec
command
daemon
filegirl.yaml
stop
profile
version
`
@ -119,13 +146,13 @@ var logo = `
_____ _ | | _____ ____) ) | | | |___| |
| ___) | | | | | ___) | __ (| | | |\_____/
| | _| |_| |_____| |_____| |__) ) |___| | ___
|_| (_____)_______)_______)______/ \_____/ (___) V1.12
|_| (_____)_______)_______)______/ \_____/ (___) V1.16
`
var statement = `Dengsgo [dengsgo@gmail.com] Open Source with MIT License`
var versionDesc = `
Version fileboy: v1.12 filegirl: v` + strconv.Itoa(Version) + `
Released 2019.12.18
Version fileboy: v1.16 filegirl: v` + strconv.Itoa(Version) + `
Released 2020.10.17
Licence MIT
Author dengsgo [dengsgo@gmail.com]
Website https://github.com/dengsgo/fileboy

BIN
resources/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

BIN
resources/jetbrains.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 KiB

8
snapcraft.yaml Normal file
View File

@ -0,0 +1,8 @@
name: fileboy
vendor: dengsgo, https://github.com/dengsgo, <dengsgo@yoytang.com>
summary: File Change Monitoring Notification Tools.
description: File Change Monitoring Notification Tools. Please Visit https://github.com/dengsgo/fileboy
version: 1.15
icon: resources/icon.png
base: core18
grade: stable

View File

@ -1,10 +1,6 @@
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"os/exec"
"sync"
@ -18,18 +14,40 @@ type TaskMan struct {
notifier *NetNotifier
putLock sync.Mutex
runLock sync.Mutex
waitChan chan bool
waitQueue []*changedFile
}
func newTaskMan(delay int, callUrl string) *TaskMan {
return &TaskMan{
delay: delay,
notifier: newNetNotifier(callUrl),
t := &TaskMan{
delay: delay,
notifier: newNetNotifier(callUrl),
waitChan: make(chan bool, 1),
waitQueue: []*changedFile{},
}
if keyInInstruction(InstShouldFinish) {
go func() {
for {
<-t.waitChan
if len(t.waitQueue) >= 1 {
cf := t.waitQueue[len(t.waitQueue)-1]
if len(t.waitQueue) > 1 {
logInfo("redundant tasks dropped:", len(t.waitQueue)-1)
}
t.waitQueue = []*changedFile{}
go t.preRun(cf)
}
}
}()
}
return t
}
func (t *TaskMan) Put(cf *changedFile) {
if t.delay < 1 {
t.preRun(cf)
t.dispatcher(cf)
return
}
t.putLock.Lock()
@ -40,15 +58,29 @@ func (t *TaskMan) Put(cf *changedFile) {
if t.lastTaskId > cf.Changed {
return
}
t.preRun(cf)
t.dispatcher(cf)
}()
}
func (t *TaskMan) dispatcher(cf *changedFile) {
if keyInInstruction(InstShouldFinish) {
t.waitQueue = append(t.waitQueue, cf)
if t.cmd == nil {
t.waitChan <- true
return
}
logInfo("waitting for the last task to finish")
logInfo("waiting tasks:", len(t.waitQueue))
} else {
t.preRun(cf)
}
}
func (t *TaskMan) preRun(cf *changedFile) {
if t.cmd != nil && t.cmd.Process != nil {
log.Println("stop old process ")
if err := t.cmd.Process.Kill(); err != nil {
log.Println(PreWarn, "stopped err, reason:", err)
logInfo("stop old process ")
logWarn("stopped err, reason:", err)
}
}
go t.run(cf)
@ -60,42 +92,44 @@ func (t *TaskMan) run(cf *changedFile) {
defer t.runLock.Unlock()
for i := 0; i < len(cfg.Command.Exec); i++ {
carr := cmdParse2Array(cfg.Command.Exec[i], cf)
log.Println("EXEC", carr)
logInfo("EXEC", carr)
t.cmd = exec.Command(carr[0], carr[1:]...)
//cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: syscall.CREATE_UNICODE_ENVIRONMENT}
t.cmd.Stdin = os.Stdin
//cmd.Stdout = os.Stdout
t.cmd.Stdout = os.Stdout
if keyInInstruction(InstIgnoreStdout) {
t.cmd.Stdout = nil
}
t.cmd.Stderr = os.Stderr
t.cmd.Dir = projectFolder
t.cmd.Env = os.Environ()
stdout, err := t.cmd.StdoutPipe()
err := t.cmd.Start()
if err != nil {
log.Println(PreError, err.Error())
return
}
err = t.cmd.Start()
if err != nil {
log.Println(PreError, "run command", carr, "error. ", err)
}
reader := bufio.NewReader(stdout)
for {
line, err2 := reader.ReadString('\n')
if err2 != nil || io.EOF == err2 {
break
logError("run command", carr, "error. ", err)
if keyInInstruction(InstIgnoreExecError) {
continue
}
fmt.Print(line)
break
}
err = t.cmd.Wait()
if err != nil {
log.Println(PreWarn, "cmd wait err ", err)
logError("command exec failed:", carr, err)
if keyInInstruction(InstIgnoreExecError) {
continue
}
break
}
if t.cmd.Process != nil {
if err = t.cmd.Process.Kill(); err != nil {
log.Println(PreWarn, "cmd cannot kill, reason:", err)
err := t.cmd.Process.Kill()
logInfo(t.cmd.ProcessState)
if t.cmd.ProcessState != nil && !t.cmd.ProcessState.Exited() {
logError("command cannot stop!", carr, err)
}
}
}
log.Println("end ")
if keyInInstruction(InstShouldFinish) {
t.cmd = nil
t.waitChan <- true
}
logInfo("EXEC end")
}

48
util.go
View File

@ -8,8 +8,13 @@ import (
"strings"
)
func keyInMonitorTypesMap(k string, cfg *FileGirl) bool {
_, ok := cfg.Monitor.TypesMap[k]
func keyInMonitorTypesMap(k string, maps map[string]bool) bool {
_, ok := maps[k]
return ok
}
func keyInInstruction(k string) bool {
_, ok := cfg.InstructionMap[k]
return ok
}
@ -48,11 +53,24 @@ func dirParse2Array(s string) []string {
return r
}
func hitDirs(d string, dirs *[]string) bool {
d += "/"
for _, v := range *dirs {
if strings.HasPrefix(d, projectFolder+"/"+v+"/") {
return true
}
}
return false
}
func listFile(folder string, fun func(string)) {
files, _ := ioutil.ReadDir(folder)
for _, file := range files {
if file.IsDir() {
d := folder + "/" + file.Name()
if hitDirs(d, &cfg.Monitor.ExceptDirs) {
continue
}
fun(d)
listFile(d, fun)
}
@ -76,7 +94,33 @@ func inStrArray(s string, arr []string) bool {
return false
}
func logInfo(v ...interface{}) {
if keyInInstruction(InstIgnoreInfo) {
return
}
logUInfo(v...)
}
func logUInfo(v ...interface{}) {
v = append([]interface{}{"I:"}, v...)
log.Println(v...)
}
func logWarn(v ...interface{}) {
if keyInInstruction(InstIgnoreWarn) {
return
}
v = append([]interface{}{"W:"}, v...)
log.Println(v...)
}
func logError(v ...interface{}) {
v = append([]interface{}{"E:"}, v...)
log.Println(v...)
}
func logAndExit(v ...interface{}) {
v = append([]interface{}{"O:"}, v...)
log.Println(v...)
os.Exit(15)
}