Compare commits
145 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
c221fbc347 | ||
|
ecf0676d67 | ||
|
b4ad1e0e96 | ||
|
195740fa3d | ||
|
4f3ad86984 | ||
|
4b3bf5e77c | ||
|
bf4f156714 | ||
|
6a59036070 | ||
|
8dc1ebe6d9 | ||
|
da54305188 | ||
|
9e651d3023 | ||
|
605914d1e1 | ||
|
86cf40564b | ||
|
819297118c | ||
|
a76d4cfe6c | ||
|
14ba79c2b3 | ||
|
c5f0649ab5 | ||
|
7c6f8f910b | ||
|
d07616bde9 | ||
|
4f81692954 | ||
|
588af5143f | ||
|
2d8924b7fe | ||
|
08fb7aabf8 | ||
|
9f0a51ce0e | ||
|
8a3a2d570b | ||
|
e5405f4fa9 | ||
|
9d9cce0f31 | ||
|
c0364a62bc | ||
|
a9758653ba | ||
|
b1c5f7da39 | ||
|
29fc62de90 | ||
|
2d409fbce0 | ||
|
550dc01dfb | ||
|
d0492bad8c | ||
|
55625d51bc | ||
|
8ddc124f89 | ||
|
8b7042411c | ||
|
b72caedcbb | ||
|
f989590d14 | ||
|
f2afa015bd | ||
|
dc7162b57d | ||
|
8828bcaed6 | ||
|
7d2e8dd34d | ||
|
bb80977213 | ||
|
1b4206a6b3 | ||
|
3351429515 | ||
|
00e34193aa | ||
|
4e0b9f54f6 | ||
|
9defc968ba | ||
|
66eee100f2 | ||
|
9e4642d0c2 | ||
|
44bb0dd65f | ||
|
eeaad58d1e | ||
|
44051b9184 | ||
|
a2daa98d08 | ||
|
97bdcb644c | ||
|
a0137807f3 | ||
|
b0a0e296d2 | ||
|
e63d200382 | ||
|
cb86b0dbe6 | ||
|
bead88c5e2 | ||
|
b8c709ac4c | ||
|
4d5e13daf5 | ||
|
46df7a206a | ||
|
04c46435d4 | ||
|
aa23628137 | ||
|
86ceb19acd | ||
|
a3f5af69ae | ||
|
4f044b8ebf | ||
|
b5f068dd55 | ||
|
f912ccb753 | ||
|
7dd5b44ab6 | ||
|
679ddba2d8 | ||
|
e4d9b74869 | ||
|
c61fac453d | ||
|
f4859b37d8 | ||
|
06fb9f6a97 | ||
|
3e47cd28ba | ||
|
753ff72f85 | ||
|
aa929ae838 | ||
|
a0af500678 | ||
|
3964324f43 | ||
|
3bc323154f | ||
|
08d421ebcb | ||
|
134f6e45f5 | ||
|
8552d33dcd | ||
|
58a3b27da9 | ||
|
7022e8bb04 | ||
|
a69cbeea2d | ||
|
c9aedb2d34 | ||
|
d2be0654e2 | ||
|
b6be235c6e | ||
|
30312d4b9c | ||
|
281a4f4d64 | ||
|
8181a8f0c5 | ||
|
9e73a0901c | ||
|
789caba260 | ||
|
86923ffdab | ||
|
72807c78a9 | ||
|
1f4d27491b | ||
|
758bd1e409 | ||
|
5c31d76fac | ||
|
bee37e29e6 | ||
|
5f43051ae8 | ||
|
9004aceb91 | ||
|
8d5c11d20d | ||
|
2eb994561d | ||
|
c8d8cfff72 | ||
|
0cd4beac62 | ||
|
4e9eecd0cc | ||
|
042038a60e | ||
|
aa27fa263a | ||
|
51d5c22a7c | ||
|
8e99507ff4 | ||
|
433ea136e4 | ||
|
30f7a2eead | ||
|
0c5cd4a468 | ||
|
4d0bc0886d | ||
|
2e717bc6ec | ||
|
e53bb7aadd | ||
|
2436e7b4e1 | ||
|
09b1de75b3 | ||
|
27f934d6fb | ||
|
92af817777 | ||
|
5f0b16cbdb | ||
|
4fbe75f828 | ||
|
4f6d1bd68c | ||
|
3ffa24d4fa | ||
|
87cde6a03a | ||
|
84aad1bfb8 | ||
|
a1cfbb0c73 | ||
|
7cb0231a1d | ||
|
c1affe1109 | ||
|
4ff6fce5d2 | ||
|
331f449733 | ||
|
7604ec07f7 | ||
|
afcceb7753 | ||
|
755a7e8c07 | ||
|
308ff29151 | ||
|
22072d1e49 | ||
|
19c5eaa819 | ||
|
3e31b5b375 | ||
|
a5fb3aea36 | ||
|
cae4bf355e | ||
|
22fca4f68e |
45
.github/workflows/go.yml
vendored
Normal file
45
.github/workflows/go.yml
vendored
Normal 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
|
5
.gitignore
vendored
5
.gitignore
vendored
@@ -2,3 +2,8 @@
|
|||||||
fileboy
|
fileboy
|
||||||
fileboy.exe
|
fileboy.exe
|
||||||
filegirl.yaml
|
filegirl.yaml
|
||||||
|
fileboy-darwin-amd64.bin
|
||||||
|
fileboy-linux-amd64.bin
|
||||||
|
fileboy-windows-amd64.exe
|
||||||
|
./bin/*
|
||||||
|
.fileboy.pid
|
||||||
|
12
.gitpod.Dockerfile
vendored
Normal file
12
.gitpod.Dockerfile
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
FROM gitpod/workspace-full
|
||||||
|
|
||||||
|
USER root
|
||||||
|
|
||||||
|
# Install custom tools, runtime, etc. using apt-get
|
||||||
|
# For example, the command below would install "bastet" - a command line tetris clone:
|
||||||
|
#
|
||||||
|
# RUN apt-get update \
|
||||||
|
# && apt-get install -y bastet \
|
||||||
|
# && apt-get clean && rm -rf /var/cache/apt/* && rm -rf /var/lib/apt/lists/* && rm -rf /tmp/*
|
||||||
|
#
|
||||||
|
# More information: https://www.gitpod.io/docs/42_config_docker/
|
7
.gitpod.yml
Normal file
7
.gitpod.yml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
image:
|
||||||
|
file: .gitpod.Dockerfile
|
||||||
|
|
||||||
|
# List the start up tasks. You can start them in parallel in multiple terminals. See https://www.gitpod.io/docs/44_config_start_tasks/
|
||||||
|
tasks:
|
||||||
|
- init: go get -d -v ./...
|
||||||
|
command: go build
|
27
.travis.yml
Normal file
27
.travis.yml
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
|
||||||
|
language: go
|
||||||
|
|
||||||
|
go:
|
||||||
|
- 1.13.x
|
||||||
|
- master
|
||||||
|
|
||||||
|
|
||||||
|
dist: bionic # ubuntu 18.04
|
||||||
|
|
||||||
|
|
||||||
|
script:
|
||||||
|
- make 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
|
||||||
|
- fileboy exec
|
||||||
|
- fileboy daemon
|
||||||
|
- ls -al .fileboy.pid
|
||||||
|
- ps aux | grep fileboy
|
||||||
|
- fileboy stop
|
||||||
|
|
139
CHANGELOG.md
139
CHANGELOG.md
@@ -1,3 +1,142 @@
|
|||||||
|
### 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
|
||||||
|
|
||||||
|
- 增加 自定义监控事件(write/rename/remove/create/chmod)
|
||||||
|
- 增加 {{event}} 事件名占位符 / event 网络通知字段
|
||||||
|
- 增加 文件(夹)变更动态添加/删除监听(beta)
|
||||||
|
- 优化 init 命令,如果已有`filegirl.yaml`现在提示错误,不会自动覆盖
|
||||||
|
- 优化 日志输出缓冲,可以通过 >> 将fileboy自身输出日志重定向到文件
|
||||||
|
- 修复 始终默认监听主目录的问题
|
||||||
|
- 升级 底层依赖
|
||||||
|
- PR Makefile 的支持 @jason-gao
|
||||||
|
|
||||||
|
|
||||||
|
### Release v1.10
|
||||||
|
|
||||||
|
2019.06.04
|
||||||
|
|
||||||
|
- 优化 log
|
||||||
|
- 优化 代码逻辑
|
||||||
|
- 修复 gin框架hotReload
|
||||||
|
|
||||||
|
|
||||||
|
### Release v1.9
|
||||||
|
|
||||||
|
2019.04.03
|
||||||
|
|
||||||
|
- 优化 文件夹监听效率,减少大量深层文件夹遍历的时间
|
||||||
|
- 优化 代码逻辑
|
||||||
|
- 增加 readme 英文说明
|
||||||
|
- 修复 偶现监听项目主目录无效的问题
|
||||||
|
|
||||||
|
|
||||||
|
### Release v1.8
|
||||||
|
|
||||||
|
2019.02.27
|
||||||
|
|
||||||
|
- 使用 go1.12 编译
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Release v1.7
|
||||||
|
|
||||||
|
2019.01.24
|
||||||
|
|
||||||
|
- 修复 time 内存
|
||||||
|
- 修复 某些情况下cmd异常导致进程挂掉的问题
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Release v1.6
|
||||||
|
|
||||||
|
2019.01.19
|
||||||
|
|
||||||
|
- 修复 http 通知失败导致进程崩溃
|
||||||
|
- 增加 includeDirs 参数规则验证
|
||||||
|
- 修改 delayMillSecond 默认值,2000
|
||||||
|
- 增加 贡献者 @jason-gao
|
||||||
|
- 优化 log
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Release v1.5
|
||||||
|
|
||||||
|
2019.01.03
|
||||||
|
|
||||||
|
- 增加 http 通知
|
||||||
|
- 增加 callUrl 参数
|
||||||
|
- 优化 command 稳定性
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
2019.01.02
|
||||||
|
|
||||||
|
- 增加 command -> delayMillSecond 参数
|
||||||
|
- 优化 文案
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
2018.12.30
|
||||||
|
|
||||||
|
- 增加 在指定时间内堆叠的任务自动丢弃
|
||||||
|
- 增加 version 信息
|
||||||
|
- 优化 代码逻辑
|
||||||
|
- 优化 提示文案
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### Release v1.2
|
||||||
|
|
||||||
|
2018.09.30
|
||||||
|
|
||||||
|
- 修复 递归监听会忽略主级目录的bug
|
||||||
|
- 增加 `fileboy version`命令
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Release v1.1
|
### Release v1.1
|
||||||
|
|
||||||
2018.09.19
|
2018.09.19
|
||||||
|
40
Makefile
Normal file
40
Makefile
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
# notice
|
||||||
|
# Make is very picky about spaces vs. tabs.
|
||||||
|
# Command lines absolutely must be indented with a single tab, and not spaces.
|
||||||
|
# You may need to adjust your editor to generate tab characters.
|
||||||
|
# http://blog.chinaunix.net/uid/28458801/sid-171170-list-1.html
|
||||||
|
|
||||||
|
# use:
|
||||||
|
# make build-mac 编译
|
||||||
|
# make start-mac 启动
|
||||||
|
|
||||||
|
# make build-start-mac 编译+启动
|
||||||
|
init:
|
||||||
|
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
|
||||||
|
|
||||||
|
build-linux: ;@echo "编译-linux版";
|
||||||
|
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags "-s -w" -o ./bin/fileboy-linux-amd64.bin
|
||||||
|
|
||||||
|
build-win: ;@echo "编译-windows版";
|
||||||
|
CGO_ENABLED=0 GOARCH=amd64 GOOS=windows go build -ldflags "-s -w" -o ./bin/fileboy-windows-amd64.exe
|
||||||
|
|
||||||
|
build-all: build-mac build-linux build-win
|
||||||
|
|
||||||
|
start-mac: ;@echo "启动服务";
|
||||||
|
./bin/fileboy-darwin-amd64.bin init
|
||||||
|
./bin/fileboy-darwin-amd64.bin
|
||||||
|
|
||||||
|
start-linux: ;@echo "启动服务";
|
||||||
|
./bin/fileboy-linux-amd64.bin init
|
||||||
|
./bin/fileboy-linux-amd64.bin
|
||||||
|
|
||||||
|
start-win: ;@echo "启动服务";
|
||||||
|
./bin/fileboy-windows-amd64.exe init
|
||||||
|
./bin/fileboy-windows-amd64.exe
|
||||||
|
|
||||||
|
build-start-mac: build-mac start-mac
|
||||||
|
|
||||||
|
.PHONY: build-mac build-linux build-win build-all start-linux travis-linux start-mac start-win build-start-mac
|
142
README.md
142
README.md
@@ -1,143 +1,3 @@
|
|||||||
## 项目说明
|
## 项目说明
|
||||||
|
|
||||||
fileboy,文件变更监听通知系统,使用 GO 编写。
|
基于 [fileboy](https://github.com/dengsgo/fileboy) 魔改版
|
||||||
|
|
||||||
适用于 Hot Reload (典型的如开发go项目,无需每次手动执行 go build;又比如前端 node 打包) 或者 系统监控的场景。
|
|
||||||
|
|
||||||
## 编译环境
|
|
||||||
|
|
||||||
go version >=1.10
|
|
||||||
|
|
||||||
## 更新日志
|
|
||||||
|
|
||||||
[CHANGELOG](CHANGELOG.md)
|
|
||||||
|
|
||||||
|
|
||||||
## 运行
|
|
||||||
|
|
||||||
### 下载二进制文件
|
|
||||||
|
|
||||||
Github: [正式版 v1.1](https://github.com/dengsgo/fileboy/releases)
|
|
||||||
Gitee [正式版 v1.1](https://gitee.com/dengsgo/fileboy/releases)
|
|
||||||
|
|
||||||
直接下载已经编译好的对应平台二进制文件,加入系统 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:
|
|
||||||
# 监听的文件有更改会执行的命令
|
|
||||||
# 可以有多条命令,会依次执行
|
|
||||||
# 如有多条命令,每条命令都会等待上一条命令执行完毕后才会执行
|
|
||||||
# 如遇交互式命令,允许外部获取输入
|
|
||||||
# 支持变量占位符,运行命令时会替换成实际值:
|
|
||||||
# {{file}} 文件名(如 a.txt 、test/test2/a.go)
|
|
||||||
# {{ext}} 文件后缀(如 .go)
|
|
||||||
# {{changed}} 文件更新的本地时间戳(纳秒,如 1537326690523046400)
|
|
||||||
# 变量占位符使用示例:cp {{file}} /root/sync -rf 、 myCommand --{{ext}} {{changed}}
|
|
||||||
exec:
|
|
||||||
- go version
|
|
||||||
- go env
|
|
||||||
- echo {{file}}
|
|
||||||
```
|
|
||||||
|
|
||||||
## QA
|
|
||||||
|
|
||||||
#### 很多框架都自带了 hot reload 的功能,为什么还要单独写个 fileboy 呢?
|
|
||||||
|
|
||||||
这个是一款通用的 hot reload 的软件,理论上适用于任何需要 hot reload 的场景,并不局限于语言层面上。只要灵活的配置 `filegirl.yaml`文件就行了。
|
|
||||||
|
|
||||||
#### fileboy 可以应用在那些具体的场景?
|
|
||||||
|
|
||||||
在开发中,我们很需要一款可以帮助我们自动打包编译的工具,那 fileboy 就非常适合这样的场景。比如 go 项目的热编译,让我们可以边修改代码边运行得到反馈。又比如 PHP Swoole 框架,由于常驻进程的原因,无法更改代码立即reload,使用 fileboy 就可以辅助做到传统 PHP 开发的体验。
|
|
||||||
|
|
||||||
对于一些需要监控文件日志或者配置变动的场景, fileboy 同样适合。你可以事先编写好相应的通知报警脚本,然后定义`filegirl.yaml`中的`command`命令,交由 fileboy 自动运行监控报警。
|
|
||||||
|
|
||||||
#### idea 下更改文件,为什么会执行两次或者多次 command ?
|
|
||||||
|
|
||||||
由于 idea 系列软件特殊的文件保存策略,他会自动创建一些临时文件,并且在需要时多次重写文件,所以有时反映在文件上就是有多次的更改,所以会出现这种情况。这个后续会做优化.
|
|
||||||
|
|
||||||
#### filegirl.yaml 里面的 command 不支持复杂的命令吗?
|
|
||||||
|
|
||||||
对于“很复杂的命令”这种说法很难去定义,比如 `echo "hello world"`并不复杂,但是对于 fileboy 来讲,目前无法解析这种命令。
|
|
||||||
|
|
||||||
fileboy 目前支持 `命令 + 参数`这种形式的 command,而且 参数中不能有""符号或者有空格。如:
|
|
||||||
|
|
||||||
`go build`:支持;
|
|
||||||
|
|
||||||
`go env`:支持;
|
|
||||||
|
|
||||||
`php swoole start --daemon`:支持
|
|
||||||
|
|
||||||
`cat a.txt | grep "q" | wc -l`:不支持
|
|
||||||
|
|
||||||
对于不支持的命令,可以把它写到一个文件里,然后在 command 中执行这个文件来解决。
|
|
||||||
|
|
||||||
#### 为什么起名为 fileboy,又把配置名叫做 filegirl ?
|
|
||||||
|
|
||||||
因为爱情~~
|
|
||||||
|
|
||||||
#### 听说有彩蛋?
|
|
||||||
|
|
||||||
(◡ᴗ◡✿)
|
|
@@ -3,6 +3,6 @@ echo build start...
|
|||||||
SET CGO_ENABLED=0
|
SET CGO_ENABLED=0
|
||||||
SET GOOS=darwin
|
SET GOOS=darwin
|
||||||
SET GOARCH=amd64
|
SET GOARCH=amd64
|
||||||
go build -ldflags "-s -w"
|
go build -ldflags "-s -w" -o fileboy-darwin-amd64.bin
|
||||||
echo build success
|
echo build success
|
||||||
pause
|
pause
|
@@ -3,6 +3,6 @@ echo build start...
|
|||||||
SET CGO_ENABLED=0
|
SET CGO_ENABLED=0
|
||||||
SET GOOS=linux
|
SET GOOS=linux
|
||||||
SET GOARCH=amd64
|
SET GOARCH=amd64
|
||||||
go build -ldflags "-s -w"
|
go build -ldflags "-s -w" -o fileboy-linux-amd64.bin
|
||||||
echo build success
|
echo build success
|
||||||
pause
|
pause
|
@@ -3,6 +3,6 @@ echo build start...
|
|||||||
SET CGO_ENABLED=0
|
SET CGO_ENABLED=0
|
||||||
SET GOOS=windows
|
SET GOOS=windows
|
||||||
SET GOARCH=amd64
|
SET GOARCH=amd64
|
||||||
go build -ldflags "-s -w"
|
go build -ldflags "-s -w" -o fileboy-windows-amd64.exe
|
||||||
echo build success
|
echo build success
|
||||||
pause
|
pause
|
56
daemon.go
Normal file
56
daemon.go
Normal 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()
|
||||||
|
}
|
387
fileboy.go
387
fileboy.go
@@ -1,185 +1,190 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"gopkg.in/fsnotify/fsnotify.v1"
|
|
||||||
"gopkg.in/yaml.v2"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/signal"
|
||||||
"path"
|
"path"
|
||||||
"sync"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gopkg.in/fsnotify/fsnotify.v1"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version = 1
|
Version = 1
|
||||||
|
|
||||||
|
InstExecWhenStart = "exec-when-start"
|
||||||
|
InstShouldFinish = "should-finish"
|
||||||
|
InstIgnoreWarn = "ignore-warn"
|
||||||
|
InstIgnoreInfo = "ignore-info"
|
||||||
|
InstIgnoreStdout = "ignore-stdout"
|
||||||
|
InstIgnoreExecError = "ignore-exec-error"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
projectFolder string = "."
|
projectFolder = "."
|
||||||
|
|
||||||
|
filegirlYamlName = "filegirl.yaml"
|
||||||
|
|
||||||
cfg *FileGirl
|
cfg *FileGirl
|
||||||
|
|
||||||
watcher *fsnotify.Watcher
|
watcher *fsnotify.Watcher
|
||||||
|
|
||||||
cmd *exec.Cmd
|
taskMan *TaskMan
|
||||||
|
|
||||||
runLock sync.Mutex
|
ioeventMapStr = map[fsnotify.Op]string{
|
||||||
|
fsnotify.Write: "write",
|
||||||
|
fsnotify.Rename: "rename",
|
||||||
|
fsnotify.Remove: "remove",
|
||||||
|
fsnotify.Create: "create",
|
||||||
|
fsnotify.Chmod: "chmod",
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
type changeFile struct {
|
type changedFile struct {
|
||||||
Name string
|
Name string
|
||||||
changed int64
|
Changed int64
|
||||||
Ext string
|
Ext string
|
||||||
|
Event string
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseConfig() {
|
func parseConfig(filegirlYamlPath string) {
|
||||||
cfg = new(FileGirl)
|
cfg = new(FileGirl)
|
||||||
fc, err := ioutil.ReadFile(projectFolder + "/filegirl.yaml")
|
fc, err := ioutil.ReadFile(filegirlYamlPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicln("read filegirl.yaml file err: ", err)
|
logError("the filegirl.yaml file in", projectFolder, "is not exist! ", err)
|
||||||
|
fmt.Print(firstRunHelp)
|
||||||
|
logAndExit("fileboy unable to run.")
|
||||||
}
|
}
|
||||||
err = yaml.Unmarshal(fc, cfg)
|
err = yaml.Unmarshal(fc, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicln("parsed filegirl.yaml failed: ", err)
|
logAndExit("parsed filegirl.yaml failed: ", err)
|
||||||
}
|
}
|
||||||
if cfg.Core.Version > Version {
|
if cfg.Core.Version > Version {
|
||||||
log.Panicln("current fileboy support max version : ", Version)
|
logAndExit("current fileboy support max version : ", Version)
|
||||||
}
|
}
|
||||||
log.Println(cfg)
|
// 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
|
||||||
|
}
|
||||||
|
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) {
|
func eventDispatcher(event fsnotify.Event) {
|
||||||
ext := path.Ext(event.Name)
|
if event.Name == getPidFile() {
|
||||||
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
|
return
|
||||||
}
|
}
|
||||||
switch event.Op {
|
ext := path.Ext(event.Name)
|
||||||
case fsnotify.Create:
|
if len(cfg.Monitor.Types) > 0 &&
|
||||||
case fsnotify.Write:
|
!keyInMonitorTypesMap(".*", cfg.Monitor.TypesMap) &&
|
||||||
log.Println("event write : ", event.Name)
|
!keyInMonitorTypesMap(ext, cfg.Monitor.TypesMap) {
|
||||||
if cmd != nil {
|
return
|
||||||
err := cmd.Process.Kill()
|
|
||||||
if err != nil {
|
|
||||||
log.Println("err: ", err)
|
|
||||||
}
|
|
||||||
log.Println("stop old process ")
|
|
||||||
}
|
|
||||||
go run(&changeFile{
|
|
||||||
Name: relativePath(projectFolder, event.Name),
|
|
||||||
changed: time.Now().UnixNano(),
|
|
||||||
Ext: ext,
|
|
||||||
})
|
|
||||||
case fsnotify.Remove:
|
|
||||||
case fsnotify.Rename:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func run(cf *changeFile) {
|
|
||||||
runLock.Lock()
|
|
||||||
defer runLock.Unlock()
|
|
||||||
for i := 0; i < len(cfg.Command.Exec); i++ {
|
|
||||||
carr := cmdParse2Array(cfg.Command.Exec[i], cf)
|
|
||||||
cmd = exec.Command(carr[0], carr[1:]...)
|
|
||||||
//cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: syscall.CREATE_UNICODE_ENVIRONMENT}
|
|
||||||
cmd.Stdin = os.Stdin
|
|
||||||
//cmd.Stdout = os.Stdout
|
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
cmd.Dir = projectFolder
|
|
||||||
cmd.Env = os.Environ()
|
|
||||||
stdout, err := cmd.StdoutPipe()
|
|
||||||
if err != nil {
|
|
||||||
log.Println("error=>", err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cmd.Start()
|
|
||||||
reader := bufio.NewReader(stdout)
|
|
||||||
for {
|
|
||||||
line, err2 := reader.ReadString('\n')
|
|
||||||
if err2 != nil || io.EOF == err2 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
log.Print(line)
|
|
||||||
}
|
|
||||||
err = cmd.Wait()
|
|
||||||
if err != nil {
|
|
||||||
log.Println("cmd wait err ", err)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
err = cmd.Process.Kill()
|
|
||||||
if err != nil {
|
|
||||||
log.Println("cmd cannot kill ", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("end ")
|
if len(cfg.Monitor.ExceptTypes) > 0 &&
|
||||||
|
keyInMonitorTypesMap(ext, cfg.Monitor.ExceptTypesMap) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
op := ioeventMapStr[event.Op]
|
||||||
|
if len(cfg.Monitor.Events) != 0 && !inStrArray(op, cfg.Monitor.Events) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Println("EVENT", event.Op.String(), ":", event.Name)
|
||||||
|
taskMan.Put(&changedFile{
|
||||||
|
Name: relativePath(projectFolder, event.Name),
|
||||||
|
Changed: time.Now().UnixNano(),
|
||||||
|
Ext: ext,
|
||||||
|
Event: op,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func addWatcher() {
|
func addWatcher() {
|
||||||
log.Println("collecting directory information...")
|
logInfo("collecting directory information...")
|
||||||
dirs := make([]string, 0)
|
dirsMap := map[string]bool{}
|
||||||
for i := 0; i < len(cfg.Monitor.IncludeDirs); i++ {
|
for _, dir := range cfg.Monitor.ExceptDirs {
|
||||||
darr := dirParse2Array(cfg.Monitor.IncludeDirs[i])
|
if dir == "." {
|
||||||
if len(darr) < 1 || len(darr) > 2 {
|
logAndExit("exceptDirs must is not project root path ! err path:", dir)
|
||||||
log.Fatalln("filegirl section monitor dirs is error. ", cfg.Monitor.IncludeDirs[i])
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
for _, dir := range cfg.Monitor.IncludeDirs {
|
||||||
|
darr := dirParse2Array(dir)
|
||||||
|
if len(darr) < 1 || len(darr) > 2 {
|
||||||
|
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 darr[0] == "." {
|
||||||
if len(darr) == 2 && darr[1] == "*" {
|
if len(darr) == 2 && darr[1] == "*" {
|
||||||
dirs = make([]string, 0)
|
// The highest priority
|
||||||
dirs = append(dirs, ".")
|
dirsMap = map[string]bool{
|
||||||
|
projectFolder: true,
|
||||||
|
}
|
||||||
listFile(projectFolder, func(d string) {
|
listFile(projectFolder, func(d string) {
|
||||||
dirs = arrayUniqueAdd(dirs, d)
|
dirsMap[d] = true
|
||||||
})
|
})
|
||||||
|
cfg.Monitor.IncludeDirsRec[projectFolder] = true
|
||||||
|
break
|
||||||
} else {
|
} else {
|
||||||
dirs = arrayUniqueAdd(dirs, projectFolder)
|
dirsMap[projectFolder] = true
|
||||||
}
|
}
|
||||||
break
|
|
||||||
} else {
|
} else {
|
||||||
|
//md := projectFolder + "/" + darr[0]
|
||||||
|
md := darr[0]
|
||||||
|
dirsMap[md] = true
|
||||||
if len(darr) == 2 && darr[1] == "*" {
|
if len(darr) == 2 && darr[1] == "*" {
|
||||||
listFile(projectFolder+"/"+darr[0], func(d string) {
|
listFile(md, func(d string) {
|
||||||
dirs = arrayUniqueAdd(dirs, d)
|
dirsMap[d] = true
|
||||||
})
|
})
|
||||||
} else {
|
cfg.Monitor.IncludeDirsRec[md] = true
|
||||||
dirs = arrayUniqueAdd(dirs, projectFolder+"/"+darr[0])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
for i := 0; i < len(cfg.Monitor.ExceptDirs); i++ {
|
|
||||||
p := projectFolder + "/" + cfg.Monitor.ExceptDirs[i]
|
for dir := range dirsMap {
|
||||||
dirs = arrayRemoveElement(dirs, p)
|
logInfo("watcher add -> ", dir)
|
||||||
listFile(p, func(d string) {
|
|
||||||
dirs = arrayRemoveElement(dirs, d)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
for _, dir := range dirs {
|
|
||||||
log.Println("watcher add -> ", dir)
|
|
||||||
err := watcher.Add(dir)
|
err := watcher.Add(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
logAndExit(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Println("fileboy is ready.")
|
logInfo("total monitored dirs: " + strconv.Itoa(len(dirsMap)))
|
||||||
|
logInfo("fileboy is ready.")
|
||||||
|
cfg.Monitor.DirsMap = dirsMap
|
||||||
}
|
}
|
||||||
|
|
||||||
func initWatcher() {
|
func initWatcher() {
|
||||||
parseConfig()
|
|
||||||
var err error
|
var err error
|
||||||
|
if watcher != nil {
|
||||||
|
_ = watcher.Close()
|
||||||
|
}
|
||||||
watcher, err = fsnotify.NewWatcher()
|
watcher, err = fsnotify.NewWatcher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
logAndExit(err)
|
||||||
}
|
}
|
||||||
defer watcher.Close()
|
taskMan = newTaskMan(cfg.Command.DelayMillSecond, cfg.Notifier.CallUrl)
|
||||||
|
|
||||||
done := make(chan bool)
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@@ -187,69 +192,183 @@ func initWatcher() {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// 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)
|
||||||
eventDispatcher(event)
|
eventDispatcher(event)
|
||||||
case err, ok := <-watcher.Errors:
|
case err, ok := <-watcher.Errors:
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println("error:", err)
|
logError(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
addWatcher()
|
addWatcher()
|
||||||
<-done
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
_, 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
|
||||||
|
if hitDirs(event.Name, &cfg.Monitor.ExceptDirs) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = watcher.Remove(event.Name)
|
||||||
|
err := watcher.Add(event.Name)
|
||||||
|
if err == nil {
|
||||||
|
do = true
|
||||||
|
logInfo("watcher add -> ", event.Name)
|
||||||
|
} else {
|
||||||
|
logWarn("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 {
|
||||||
|
logInfo("watcher add -> ", event.Name)
|
||||||
|
} else {
|
||||||
|
logWarn("watcher add faild:", event.Name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseArgs() {
|
func parseArgs() {
|
||||||
l := len(os.Args)
|
switch {
|
||||||
if l == 1 {
|
case len(os.Args) == 1:
|
||||||
_, err := ioutil.ReadFile(projectFolder + "/filegirl.yaml")
|
show()
|
||||||
if err != nil {
|
parseConfig(getFileGirlPath())
|
||||||
log.Println("the filegirl.yaml file does not exist! ", err)
|
done := make(chan bool)
|
||||||
fmt.Print(firstRunHelp)
|
|
||||||
fmt.Print(helpStr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
initWatcher()
|
initWatcher()
|
||||||
|
defer watcher.Close()
|
||||||
|
if keyInInstruction(InstExecWhenStart) {
|
||||||
|
taskMan.run(new(changedFile))
|
||||||
|
}
|
||||||
|
<-done
|
||||||
return
|
return
|
||||||
}
|
case len(os.Args) > 1:
|
||||||
if l == 2 {
|
|
||||||
c := os.Args[1]
|
c := os.Args[1]
|
||||||
switch c {
|
switch c {
|
||||||
case "init":
|
case "deamon", "daemon":
|
||||||
err := ioutil.WriteFile(projectFolder+"/filegirl.yaml", []byte(exampleFileGirl), 0644)
|
pid, err := runAsDaemon()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error create filegirl.yaml config! ", err)
|
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 {
|
||||||
|
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 {
|
||||||
|
logError("profile filegirl.yaml create failed! ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println("create filegirl.yaml ok")
|
logUInfo("profile filegirl.yaml created ok")
|
||||||
return
|
return
|
||||||
case "exec":
|
case "exec":
|
||||||
parseConfig()
|
parseConfig(getFileGirlPath())
|
||||||
run(new(changeFile))
|
newTaskMan(0, cfg.Notifier.CallUrl).run(new(changedFile))
|
||||||
return
|
return
|
||||||
default:
|
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)
|
||||||
|
case "help", "--help", "--h", "-h":
|
||||||
fmt.Print(helpStr)
|
fmt.Print(helpStr)
|
||||||
|
default:
|
||||||
|
logAndExit("unknown parameter, use 'fileboy help' to view available commands")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
default:
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
func show() {
|
func show() {
|
||||||
fmt.Print(logo)
|
fmt.Print(logo)
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
fmt.Println(englishSay[rand.Intn(len(englishSay))], "\r\n")
|
fmt.Println(englishSay[rand.Intn(len(englishSay))])
|
||||||
fmt.Println("Version: ", Version, " Author: dengsgo@yoytang.com")
|
fmt.Println("")
|
||||||
|
fmt.Println(statement)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.SetPrefix("[FileBoy]: ")
|
log.SetPrefix("[FileBoy]: ")
|
||||||
log.SetFlags(2)
|
log.SetFlags(2)
|
||||||
show()
|
log.SetOutput(os.Stdout)
|
||||||
|
// show()
|
||||||
var err error
|
var err error
|
||||||
projectFolder, err = os.Getwd()
|
projectFolder, err = os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
logAndExit(err)
|
||||||
}
|
}
|
||||||
|
signalHandler()
|
||||||
parseArgs()
|
parseArgs()
|
||||||
}
|
}
|
||||||
|
22
filegirl.go
22
filegirl.go
@@ -6,12 +6,28 @@ type FileGirl struct {
|
|||||||
}
|
}
|
||||||
Monitor struct {
|
Monitor struct {
|
||||||
Types []string `yaml:"types"`
|
Types []string `yaml:"types"`
|
||||||
|
ExceptTypes []string `yaml:"exceptTypes"`
|
||||||
IncludeDirs []string `yaml:"includeDirs"`
|
IncludeDirs []string `yaml:"includeDirs"`
|
||||||
ExceptDirs []string `yaml:"exceptDirs"`
|
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:"-"`
|
||||||
|
|
||||||
|
IncludeDirsRec map[string]bool `yaml:"-"`
|
||||||
}
|
}
|
||||||
Command struct {
|
Command struct {
|
||||||
//BeforeExec string `yaml:"beforeExec"`
|
Exec []string `yaml:"exec"`
|
||||||
Exec []string `yaml:"exec"`
|
DelayMillSecond int `yaml:"delayMillSecond"`
|
||||||
//AfterExec string `yaml:"afterExec"`
|
|
||||||
}
|
}
|
||||||
|
Notifier struct {
|
||||||
|
CallUrl string `yaml:"callUrl"`
|
||||||
|
}
|
||||||
|
Instruction []string `yaml:"instruction"`
|
||||||
|
|
||||||
|
// convert to
|
||||||
|
InstructionMap map[string]bool `yaml:"-"`
|
||||||
}
|
}
|
||||||
|
10
go.mod
Normal file
10
go.mod
Normal 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
11
go.sum
Normal 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=
|
12
mac-build-all
Executable file
12
mac-build-all
Executable file
@@ -0,0 +1,12 @@
|
|||||||
|
export CGO_ENABLED=0
|
||||||
|
export GOARCH=amd64
|
||||||
|
export GOOS=darwin
|
||||||
|
go build -ldflags "-s -w" -o fileboy-darwin-amd64.bin
|
||||||
|
echo darwin ok
|
||||||
|
export GOOS=linux
|
||||||
|
go build -ldflags "-s -w" -o fileboy-linux-amd64.bin
|
||||||
|
echo linux ok
|
||||||
|
export GOOS=windows
|
||||||
|
go build -ldflags "-s -w" -o fileboy-windows-amd64.exe
|
||||||
|
echo windows ok
|
||||||
|
|
7
mergeUpstream.sh
Executable file
7
mergeUpstream.sh
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# 合并上游-fork来源
|
||||||
|
git remote add upstream https://github.com/dengsgo/fileboy.git
|
||||||
|
git fetch upstream
|
||||||
|
git checkout master
|
||||||
|
git merge upstream/master
|
79
notifer.go
Normal file
79
notifer.go
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type postParams struct {
|
||||||
|
ProjectFolder string `json:"project_folder"`
|
||||||
|
File string `json:"file"`
|
||||||
|
Changed int64 `json:"changed"`
|
||||||
|
Ext string `json:"ext"`
|
||||||
|
Event string `json:"event"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type NetNotifier struct {
|
||||||
|
CallUrl string
|
||||||
|
CanPost bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func newNetNotifier(callUrl string) *NetNotifier {
|
||||||
|
callPost := true
|
||||||
|
if strings.TrimSpace(callUrl) == "" {
|
||||||
|
callPost = false
|
||||||
|
}
|
||||||
|
return &NetNotifier{
|
||||||
|
CallUrl: callUrl,
|
||||||
|
CanPost: callPost,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *NetNotifier) Put(cf *changedFile) {
|
||||||
|
if !n.CanPost {
|
||||||
|
logWarn("notifier call url ignore. ", n.CallUrl)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
n.dispatch(&postParams{
|
||||||
|
ProjectFolder: projectFolder,
|
||||||
|
File: cf.Name,
|
||||||
|
Changed: cf.Changed,
|
||||||
|
Ext: cf.Ext,
|
||||||
|
Event: cf.Event,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *NetNotifier) dispatch(params *postParams) {
|
||||||
|
b, err := json.Marshal(params)
|
||||||
|
if err != nil {
|
||||||
|
logError("json.Marshal n.params. ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
client := &http.Client{
|
||||||
|
Timeout: time.Second * 15,
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest("POST", n.CallUrl, bytes.NewBuffer(b))
|
||||||
|
if err != nil {
|
||||||
|
logError("http.NewRequest. ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
|
||||||
|
req.Header.Set("User-Agent", "FileBoy Net Notifier v1.16")
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
logError("notifier call failed. err:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if resp != nil && resp.Body != nil {
|
||||||
|
_ = resp.Body.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
if resp.StatusCode >= 300 {
|
||||||
|
// todo retry???
|
||||||
|
}
|
||||||
|
logInfo("notifier done .")
|
||||||
|
}
|
83
raw.go
83
raw.go
@@ -1,5 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
var exampleFileGirl string = `# 主配置
|
var exampleFileGirl string = `# 主配置
|
||||||
core:
|
core:
|
||||||
# 配置版本号
|
# 配置版本号
|
||||||
@@ -30,26 +32,81 @@ monitor:
|
|||||||
types:
|
types:
|
||||||
- .go
|
- .go
|
||||||
|
|
||||||
|
# 不监听文件的格式,此类文件更改不会执行 command 中的命令
|
||||||
|
# .DS_Store 后缀为 .DS_Store 的文件更改,不会执行 command 中的命令
|
||||||
|
exceptTypes:
|
||||||
|
- .DS_Store
|
||||||
|
|
||||||
|
# 监听的事件类型,发生此类事件才执行 command 中的命令
|
||||||
|
# 没有该配置默认监听所有事件
|
||||||
|
# write 写入文件事件
|
||||||
|
# rename 重命名文件事件
|
||||||
|
# remove 移除文件事件
|
||||||
|
# create 创建文件事件
|
||||||
|
# chmod 更新文件权限事件(类unix)
|
||||||
|
events:
|
||||||
|
- write
|
||||||
|
- rename
|
||||||
|
- remove
|
||||||
|
- create
|
||||||
|
- chmod
|
||||||
|
|
||||||
# 命令
|
# 命令
|
||||||
command:
|
command:
|
||||||
# 监听的文件有更改会执行的命令
|
# 监听的文件有更改会执行的命令
|
||||||
# 可以有多条命令,会依次执行
|
# 可以有多条命令,会依次执行
|
||||||
# 如有多条命令,每条命令都会等待上一条命令执行完毕后才会执行
|
# 如有多条命令,每条命令都会等待上一条命令执行完毕后才会执行
|
||||||
# 如遇交互式命令,允许外部获取输入
|
# 如遇交互式命令,允许外部获取输入
|
||||||
# 支持变量占位符:
|
# 支持变量占位符,运行命令时会替换成实际值:
|
||||||
# {{file}} 文件名(如 a.txt 、test/test2/a.go)
|
# {{file}} 文件名(如 a.txt 、test/test2/a.go)
|
||||||
# {{ext}} 文件后缀(如 .go)
|
# {{ext}} 文件后缀(如 .go)
|
||||||
|
# {{event}} 事件(上面的events, 如 write)
|
||||||
# {{changed}} 文件更新的本地时间戳(纳秒,如 1537326690523046400)
|
# {{changed}} 文件更新的本地时间戳(纳秒,如 1537326690523046400)
|
||||||
# 变量占位符使用示例:cp {{file}} /root/sync -rf 、 myCommand --{{ext}} {{changed}}
|
# 变量占位符使用示例:cp {{file}} /root/sync -rf 、 myCommand --{{ext}} {{changed}}
|
||||||
exec:
|
exec:
|
||||||
- go version
|
- go version
|
||||||
- go env
|
- go env
|
||||||
- echo {{file}}
|
|
||||||
|
# 文件变更后命令在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.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 ?
|
var firstRunHelp = `第一次运行 fileboy ?
|
||||||
你可能需要先执行 fileboy init 生成配置。
|
你可能需要先执行 fileboy init 生成配置。
|
||||||
更多信息查看帮助:
|
更多信息使用 fileboy help 查看帮助
|
||||||
`
|
`
|
||||||
|
|
||||||
var helpStr = `fileboy [option]
|
var helpStr = `fileboy [option]
|
||||||
@@ -60,6 +117,14 @@ Usage of fileboy:
|
|||||||
初始化 fileboy, 在当前目录生成 filegirl.yaml 配置文件
|
初始化 fileboy, 在当前目录生成 filegirl.yaml 配置文件
|
||||||
exec
|
exec
|
||||||
尝试运行定义的 command 命令
|
尝试运行定义的 command 命令
|
||||||
|
daemon
|
||||||
|
读取当前目录下的 filegirl.yaml 配置,以守护进程的方式运行在后台
|
||||||
|
stop
|
||||||
|
停止守护进程
|
||||||
|
profile
|
||||||
|
指定配置文件路径
|
||||||
|
version
|
||||||
|
查看当前版本信息
|
||||||
`
|
`
|
||||||
|
|
||||||
var englishSay = []string{
|
var englishSay = []string{
|
||||||
@@ -81,5 +146,15 @@ var logo = `
|
|||||||
_____ _ | | _____ ____) ) | | | |___| |
|
_____ _ | | _____ ____) ) | | | |___| |
|
||||||
| ___) | | | | | ___) | __ (| | | |\_____/
|
| ___) | | | | | ___) | __ (| | | |\_____/
|
||||||
| | _| |_| |_____| |_____| |__) ) |___| | ___
|
| | _| |_| |_____| |_____| |__) ) |___| | ___
|
||||||
|_| (_____)_______)_______)______/ \_____/ (___)
|
|_| (_____)_______)_______)______/ \_____/ (___) V1.16
|
||||||
|
`
|
||||||
|
var statement = `Dengsgo [dengsgo@gmail.com] Open Source with MIT License`
|
||||||
|
|
||||||
|
var versionDesc = `
|
||||||
|
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
|
||||||
|
Blog https://www.yoytang.com
|
||||||
`
|
`
|
||||||
|
BIN
resources/icon.png
Normal file
BIN
resources/icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.8 KiB |
BIN
resources/jetbrains.png
Normal file
BIN
resources/jetbrains.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 164 KiB |
8
snapcraft.yaml
Normal file
8
snapcraft.yaml
Normal 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
|
135
taskman.go
Normal file
135
taskman.go
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TaskMan struct {
|
||||||
|
lastTaskId int64
|
||||||
|
delay int
|
||||||
|
cmd *exec.Cmd
|
||||||
|
notifier *NetNotifier
|
||||||
|
putLock sync.Mutex
|
||||||
|
runLock sync.Mutex
|
||||||
|
|
||||||
|
waitChan chan bool
|
||||||
|
waitQueue []*changedFile
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTaskMan(delay int, callUrl string) *TaskMan {
|
||||||
|
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.dispatcher(cf)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.putLock.Lock()
|
||||||
|
defer t.putLock.Unlock()
|
||||||
|
t.lastTaskId = cf.Changed
|
||||||
|
go func() {
|
||||||
|
<-time.After(time.Millisecond * time.Duration(t.delay))
|
||||||
|
if t.lastTaskId > cf.Changed {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
if err := t.cmd.Process.Kill(); err != nil {
|
||||||
|
logInfo("stop old process ")
|
||||||
|
logWarn("stopped err, reason:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
go t.run(cf)
|
||||||
|
go t.notifier.Put(cf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TaskMan) run(cf *changedFile) {
|
||||||
|
t.runLock.Lock()
|
||||||
|
defer t.runLock.Unlock()
|
||||||
|
for i := 0; i < len(cfg.Command.Exec); i++ {
|
||||||
|
carr := cmdParse2Array(cfg.Command.Exec[i], cf)
|
||||||
|
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
|
||||||
|
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()
|
||||||
|
err := t.cmd.Start()
|
||||||
|
if err != nil {
|
||||||
|
logError("run command", carr, "error. ", err)
|
||||||
|
if keyInInstruction(InstIgnoreExecError) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
err = t.cmd.Wait()
|
||||||
|
if err != nil {
|
||||||
|
logError("command exec failed:", carr, err)
|
||||||
|
if keyInInstruction(InstIgnoreExecError) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if t.cmd.Process != nil {
|
||||||
|
err := t.cmd.Process.Kill()
|
||||||
|
logInfo(t.cmd.ProcessState)
|
||||||
|
if t.cmd.ProcessState != nil && !t.cmd.ProcessState.Exited() {
|
||||||
|
logError("command cannot stop!", carr, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if keyInInstruction(InstShouldFinish) {
|
||||||
|
t.cmd = nil
|
||||||
|
t.waitChan <- true
|
||||||
|
}
|
||||||
|
logInfo("EXEC end")
|
||||||
|
}
|
105
util.go
105
util.go
@@ -2,20 +2,23 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func inStringArray(value string, arr []string) bool {
|
func keyInMonitorTypesMap(k string, maps map[string]bool) bool {
|
||||||
for _, v := range arr {
|
_, ok := maps[k]
|
||||||
if value == v {
|
return ok
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmdParse2Array(s string, cf *changeFile) []string {
|
func keyInInstruction(k string) bool {
|
||||||
|
_, ok := cfg.InstructionMap[k]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func cmdParse2Array(s string, cf *changedFile) []string {
|
||||||
a := strings.Split(s, " ")
|
a := strings.Split(s, " ")
|
||||||
r := make([]string, 0)
|
r := make([]string, 0)
|
||||||
for i := 0; i < len(a); i++ {
|
for i := 0; i < len(a); i++ {
|
||||||
@@ -26,13 +29,16 @@ func cmdParse2Array(s string, cf *changeFile) []string {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func strParseRealStr(s string, cf *changeFile) string {
|
func strParseRealStr(s string, cf *changedFile) string {
|
||||||
return strings.Replace(
|
return strings.ReplaceAll(
|
||||||
strings.Replace(
|
strings.ReplaceAll(
|
||||||
strings.Replace(s, "{{file}}", cf.Name, -1),
|
strings.ReplaceAll(
|
||||||
"{{ext}}", cf.Ext, -1,
|
strings.ReplaceAll(s, "{{file}}", cf.Name),
|
||||||
|
"{{ext}}", cf.Ext,
|
||||||
|
),
|
||||||
|
"{{changed}}", strconv.FormatInt(cf.Changed, 10),
|
||||||
),
|
),
|
||||||
"{{changed}}", strconv.FormatInt(cf.changed, 10), -1,
|
"{{event}}", cf.Event,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,41 +53,74 @@ func dirParse2Array(s string) []string {
|
|||||||
return r
|
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)) {
|
func listFile(folder string, fun func(string)) {
|
||||||
files, _ := ioutil.ReadDir(folder)
|
files, _ := ioutil.ReadDir(folder)
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if file.IsDir() {
|
if file.IsDir() {
|
||||||
d := folder + "/" + file.Name()
|
d := folder + "/" + file.Name()
|
||||||
|
if hitDirs(d, &cfg.Monitor.ExceptDirs) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
fun(d)
|
fun(d)
|
||||||
listFile(d, fun)
|
listFile(d, fun)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func arrayUniqueAdd(a []string, add string) []string {
|
func relativePath(folder, p string) string {
|
||||||
if inStringArray(add, a) {
|
s := strings.ReplaceAll(strings.TrimPrefix(p, folder), "\\", "/")
|
||||||
return a
|
if strings.HasPrefix(s, "/") && len(s) > 1 {
|
||||||
|
s = s[1:]
|
||||||
}
|
}
|
||||||
return append(a, add)
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func arrayRemoveElement(a []string, r string) []string {
|
func inStrArray(s string, arr []string) bool {
|
||||||
i := -1
|
for _, v := range arr {
|
||||||
for k, v := range a {
|
if s == v {
|
||||||
if v == r {
|
return true
|
||||||
i = k
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if i == -1 {
|
return false
|
||||||
return a
|
|
||||||
}
|
|
||||||
if len(a) == 1 && i == 0 {
|
|
||||||
return []string{}
|
|
||||||
}
|
|
||||||
return append(a[:i], a[i+1:]...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func relativePath(folder, p string) string {
|
func logInfo(v ...interface{}) {
|
||||||
return strings.TrimPrefix(p, folder)
|
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)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user