Compare commits
53 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
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 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -2,3 +2,7 @@
|
|||||||
fileboy
|
fileboy
|
||||||
fileboy.exe
|
fileboy.exe
|
||||||
filegirl.yaml
|
filegirl.yaml
|
||||||
|
fileboy-darwin-amd64.bin
|
||||||
|
fileboy-linux-amd64.bin
|
||||||
|
fileboy-windows-amd64.exe
|
||||||
|
|
||||||
|
74
CHANGELOG.md
74
CHANGELOG.md
@@ -1,3 +1,77 @@
|
|||||||
|
### 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
|
### Release v1.2
|
||||||
|
|
||||||
2018.09.30
|
2018.09.30
|
||||||
|
190
README.md
190
README.md
@@ -1,12 +1,36 @@
|
|||||||
## 项目说明
|
## 项目说明
|
||||||
|
|
||||||
fileboy,文件变更监听通知系统,使用 Go 编写。
|
fileboy,文件变更监听通知系统,使用 Go 编写。
|
||||||
|
|
||||||
适用于 Hot Reload (典型的如开发go项目,无需每次手动执行 go build;又比如前端 node 打包) 或者 系统监控的场景。
|
适用于 Hot Reload (典型的如开发go项目,无需每次手动执行 go build;又比如前端 node 打包) 或者 系统监控的场景。
|
||||||
|
___
|
||||||
|
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.
|
||||||
|
|
||||||
|
|
||||||
|
## 特性
|
||||||
|
|
||||||
|
- 极简的用法和配置
|
||||||
|
- 支持多平台,Windows/Linux/MacOS
|
||||||
|
- 支持自定义文件监听范围,监听指定文件夹/不监听指定文件夹/指定后缀文件
|
||||||
|
- 支持设置多条命令
|
||||||
|
- 命令支持变量占位符
|
||||||
|
- 支持冗余任务丢弃,自定义冗余任务范围
|
||||||
|
- 支持 http 通知
|
||||||
|
- 更多...
|
||||||
|
___
|
||||||
|
- 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 setting up multiple commands
|
||||||
|
- Command support variable placeholders
|
||||||
|
- Supporting redundant task discarding and customizing redundant task scope
|
||||||
|
- Supporting HTTP notifications
|
||||||
|
- more...
|
||||||
|
|
||||||
|
|
||||||
## 编译环境
|
## 编译环境
|
||||||
|
|
||||||
go version >=1.10,推荐 1.11
|
go version 1.12
|
||||||
|
|
||||||
## 更新日志
|
## 更新日志
|
||||||
|
|
||||||
@@ -17,15 +41,16 @@ go version >=1.10,推荐 1.11
|
|||||||
|
|
||||||
### 下载二进制文件
|
### 下载二进制文件
|
||||||
|
|
||||||
Github: [正式版 v1.2](https://github.com/dengsgo/fileboy/releases)
|
Github: [download v1.10](https://github.com/dengsgo/fileboy/releases)
|
||||||
Gitee: [正式版 v1.2](https://gitee.com/dengsgo/fileboy/releases)
|
Gitee: [dowmload v1.10](https://gitee.com/dengsgo/fileboy/releases)
|
||||||
|
|
||||||
下载已经编译好的对应平台二进制文件,重命名为`fileboy`, 加入系统 Path 中即可。
|
下载已经编译好的对应平台二进制文件,重命名为`fileboy`, 加入系统 Path 中即可。
|
||||||
|
___
|
||||||
|
Download the compiled binary file of the corresponding platform, rename it `fileboy`, and add it to the system Path.
|
||||||
|
|
||||||
### 源码编译
|
### 源码编译
|
||||||
|
|
||||||
clone 该项目,进入主目录,运行命令:
|
clone 该项目,进入主目录,运行命令:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
## 安装依赖
|
## 安装依赖
|
||||||
go get -u gopkg.in/fsnotify/fsnotify.v1
|
go get -u gopkg.in/fsnotify/fsnotify.v1
|
||||||
@@ -35,22 +60,36 @@ go build
|
|||||||
## 运行
|
## 运行
|
||||||
./fileboy
|
./fileboy
|
||||||
```
|
```
|
||||||
|
___
|
||||||
|
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
|
||||||
|
```
|
||||||
## 使用
|
## 使用
|
||||||
|
|
||||||
fileboy 的正常运行依赖于 `filegirl.yaml` 配置,所以首次在项目中使用需要初始化 `filegirl.yaml`。
|
fileboy 的正常运行依赖于 `filegirl.yaml` 配置,所以首次在项目中使用需要初始化 `filegirl.yaml`。
|
||||||
|
|
||||||
- 进入你想要 hot reload 的项目主目录下;
|
- 进入你想要 hot reload 的项目主目录下;
|
||||||
|
|
||||||
- 运行 `fileboy init`,会在该目录下生成 `filegirl.yaml`文件;
|
- 运行 `fileboy init`,会在该目录下生成 `filegirl.yaml`文件;
|
||||||
|
|
||||||
- 查看 `filegirl.yaml`,修改为适合自己项目的配置项;
|
- 查看 `filegirl.yaml`,修改为适合自己项目的配置项;
|
||||||
|
|
||||||
- 运行 `fileboy`即可.
|
- 运行 `fileboy`即可.
|
||||||
|
|
||||||
如果你定义了 `command -> exec`命令,想事先确认是否能正常执行,可以运行 `fileboy exec`命令,系统会尝试运行你的自定义命令。
|
如果你定义了 `command -> exec`命令,想事先确认是否能正常执行,可以运行 `fileboy exec`命令,系统会尝试运行你的自定义命令。
|
||||||
|
|
||||||
你可以使用 `fileboy help`查看使用帮助。
|
你可以使用 `fileboy help`查看使用帮助。
|
||||||
|
___
|
||||||
|
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 配置文件说明
|
## filegirl.yaml 配置文件说明
|
||||||
|
|
||||||
@@ -99,7 +138,89 @@ command:
|
|||||||
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.10
|
||||||
|
# Body: {"project_folder":"/watcher-dirs","file":"test.go","changed":1546421173070433800,"ext":".go"}
|
||||||
|
# 例: http://example.com/notifier/fileboy-listener
|
||||||
|
# 不启用通知,请留空 ""
|
||||||
|
callUrl: ""
|
||||||
|
```
|
||||||
|
___
|
||||||
|
```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
|
||||||
|
|
||||||
|
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)
|
||||||
|
# {{changed}} local timestamp for file updated(nanosecond,e.g 1537326690523046400)
|
||||||
|
# variable placeholders e.g:cp {{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.10
|
||||||
|
# Body: {"project_folder":"/watcher-dirs","file":"test.go","changed":1546421173070433800,"ext":".go"}
|
||||||
|
# e.g: http://example.com/notifier/fileboy-listener
|
||||||
|
# no notice is enabled. Please leave it blank. ""
|
||||||
|
callUrl: ""
|
||||||
```
|
```
|
||||||
|
|
||||||
### TODO
|
### TODO
|
||||||
@@ -109,10 +230,18 @@ command:
|
|||||||
- [x] 支持监听指定文件夹
|
- [x] 支持监听指定文件夹
|
||||||
- [x] 支持不监听指定文件夹
|
- [x] 支持不监听指定文件夹
|
||||||
- [x] 支持监听指定后缀文件
|
- [x] 支持监听指定后缀文件
|
||||||
- [ ] 支持 http 通知
|
- [x] 支持 http 通知
|
||||||
- [ ] 支持冗余任务丢弃
|
- [x] 支持冗余任务丢弃
|
||||||
|
- [ ] 支持 http 合并任务的通知
|
||||||
|
___
|
||||||
|
- [x] command supports variable placeholders
|
||||||
|
- [x] Supports multiple commands
|
||||||
|
- [x] Supports listening for specified folders
|
||||||
|
- [x] Supports not listening to specified folders
|
||||||
|
- [x] Supports listening for specified suffix files
|
||||||
|
- [x] Supports HTTP notifications
|
||||||
|
- [x] Supports redundant task discarding
|
||||||
|
- [ ] Notification supporting HTTP merge tasks
|
||||||
|
|
||||||
## QA
|
## QA
|
||||||
|
|
||||||
@@ -123,36 +252,29 @@ command:
|
|||||||
#### fileboy 可以应用在那些具体的场景?
|
#### fileboy 可以应用在那些具体的场景?
|
||||||
|
|
||||||
在开发中,我们很需要一款可以帮助我们自动打包编译的工具,那 fileboy 就非常适合这样的场景。比如 go 项目的热编译,让我们可以边修改代码边运行得到反馈。又比如 PHP Swoole 框架,由于常驻进程的原因,无法更改代码立即reload,使用 fileboy 就可以辅助做到传统 PHP 开发的体验。
|
在开发中,我们很需要一款可以帮助我们自动打包编译的工具,那 fileboy 就非常适合这样的场景。比如 go 项目的热编译,让我们可以边修改代码边运行得到反馈。又比如 PHP Swoole 框架,由于常驻进程的原因,无法更改代码立即reload,使用 fileboy 就可以辅助做到传统 PHP 开发的体验。
|
||||||
|
|
||||||
对于一些需要监控文件日志或者配置变动的场景, fileboy 同样适合。你可以事先编写好相应的通知报警脚本,然后定义`filegirl.yaml`中的`command`命令,交由 fileboy 自动运行监控报警。
|
对于一些需要监控文件日志或者配置变动的场景, fileboy 同样适合。你可以事先编写好相应的通知报警脚本,然后定义`filegirl.yaml`中的`command`命令,交由 fileboy 自动运行监控报警。
|
||||||
|
|
||||||
|
#### 通知器在什么时候会发送 http 请求 ?
|
||||||
|
|
||||||
|
通知器发送 http 通知的前提是在配置文件中设置了 `callUrl` 参数(不为空即为已设置)。触发请求的时机和执行 command 命令是一致的,`command -> delayMillSecond` 参数对于触发器同样有效。请求超时默认15秒.
|
||||||
|
|
||||||
#### idea 下更改文件,为什么会执行两次或者多次 command ?
|
#### idea 下更改文件,为什么会执行两次或者多次 command ?
|
||||||
|
|
||||||
由于 idea 系列软件特殊的文件保存策略,他会自动创建一些临时文件,并且在需要时多次重写文件,所以有时反映在文件上就是有多次的更改,所以会出现这种情况。这个后续会做优化.
|
由于 idea 系列软件特殊的文件保存策略,他会自动创建一些临时文件,并且在需要时多次重写文件,所以有时反映在文件上就是有多次的更改,所以会出现这种情况。1.5之后的版本增加了 `delayMillSecond` 参数,可以解决这个问题。
|
||||||
|
|
||||||
#### filegirl.yaml 里面的 command 不支持复杂的命令吗?
|
#### filegirl.yaml 里面的 command 不支持复杂的命令吗?
|
||||||
|
|
||||||
对于“很复杂的命令”这种说法很难去定义,比如 `echo "hello world"`并不复杂,但是对于 fileboy 来讲,目前无法解析这种命令。
|
对于“很复杂的命令”这种说法很难去定义,比如 `echo "hello world"`并不复杂,但是对于 fileboy 来讲,目前无法解析这种命令。
|
||||||
|
|
||||||
fileboy 目前支持 `命令 + 参数`这种形式的 command,而且 参数中不能有""符号或者有空格。如:
|
fileboy 目前支持 `命令 + 参数`这种形式的 command,而且 参数中不能有""符号或者有空格。如:
|
||||||
|
|
||||||
`go build`:支持;
|
`go build`:支持;
|
||||||
|
|
||||||
`go env`:支持;
|
`go env`:支持;
|
||||||
|
|
||||||
`php swoole start --daemon`:支持
|
`php swoole start --daemon`:支持
|
||||||
|
|
||||||
`cat a.txt | grep "q" | wc -l`:不支持
|
`cat a.txt | grep "q" | wc -l`:不支持
|
||||||
|
|
||||||
对于不支持的命令,可以把它写到一个文件里,然后在 command 中执行这个文件来解决。
|
对于不支持的命令,可以把它写到一个文件里,然后在 command 中执行这个文件来解决。
|
||||||
|
|
||||||
#### 为什么起名为 fileboy,又把配置名叫做 filegirl ?
|
#### 为什么起名为 fileboy,又把配置名叫做 filegirl ?
|
||||||
|
|
||||||
因为爱情~~
|
因为爱情~~ (◡ᴗ◡✿)
|
||||||
|
|
||||||
#### 听说有彩蛋?
|
|
||||||
|
|
||||||
(◡ᴗ◡✿)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -160,7 +282,9 @@ fileboy 目前支持 `命令 + 参数`这种形式的 command,而且 参数中
|
|||||||
|
|
||||||
> 排名不分先后
|
> 排名不分先后
|
||||||
|
|
||||||
[@dengsgo](https://www.yoytang.com) <dengsgo@yoytang.com>
|
[@dengsgo](https://www.yoytang.com) <dengsgo@gmail.com>
|
||||||
|
|
||||||
[@itwesley](https://github.com/itwesley) <wcshen1126@gmail.com>
|
[@itwesley](https://github.com/itwesley) <wcshen1126@gmail.com>
|
||||||
|
|
||||||
|
[@jason-gao](https://github.com/jason-gao) <3048789891@qq.com>
|
||||||
|
|
||||||
|
193
fileboy.go
193
fileboy.go
@@ -1,39 +1,37 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"gopkg.in/fsnotify/fsnotify.v1"
|
"gopkg.in/fsnotify/fsnotify.v1"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version = 1
|
Version = 1
|
||||||
|
|
||||||
|
PreError = "ERROR:"
|
||||||
|
PreWarn = "Warn:"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
projectFolder string = "."
|
projectFolder = "."
|
||||||
|
|
||||||
cfg *FileGirl
|
cfg *FileGirl
|
||||||
|
|
||||||
watcher *fsnotify.Watcher
|
watcher *fsnotify.Watcher
|
||||||
|
|
||||||
cmd *exec.Cmd
|
taskMan *TaskMan
|
||||||
|
|
||||||
runLock sync.Mutex
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type changeFile struct {
|
type changedFile struct {
|
||||||
Name string
|
Name string
|
||||||
Changed int64
|
Changed int64
|
||||||
Ext string
|
Ext string
|
||||||
@@ -43,14 +41,24 @@ func parseConfig() {
|
|||||||
cfg = new(FileGirl)
|
cfg = new(FileGirl)
|
||||||
fc, err := ioutil.ReadFile(projectFolder + "/filegirl.yaml")
|
fc, err := ioutil.ReadFile(projectFolder + "/filegirl.yaml")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicln("read filegirl.yaml file err: ", err)
|
log.Println(PreError, "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(PreError, "parsed filegirl.yaml failed: ", err)
|
||||||
}
|
}
|
||||||
if cfg.Core.Version > Version {
|
if cfg.Core.Version > Version {
|
||||||
log.Panicln("current fileboy support max version : ", Version)
|
logAndExit(PreError, "current fileboy support max version : ", Version)
|
||||||
|
}
|
||||||
|
// init map
|
||||||
|
cfg.Monitor.TypesMap = map[string]bool{}
|
||||||
|
cfg.Monitor.IncludeDirsMap = map[string]bool{}
|
||||||
|
cfg.Monitor.ExceptDirsMap = map[string]bool{}
|
||||||
|
// convert to map
|
||||||
|
for _, v := range cfg.Monitor.Types {
|
||||||
|
cfg.Monitor.TypesMap[v] = true
|
||||||
}
|
}
|
||||||
log.Println(cfg)
|
log.Println(cfg)
|
||||||
}
|
}
|
||||||
@@ -58,131 +66,94 @@ func parseConfig() {
|
|||||||
func eventDispatcher(event fsnotify.Event) {
|
func eventDispatcher(event fsnotify.Event) {
|
||||||
ext := path.Ext(event.Name)
|
ext := path.Ext(event.Name)
|
||||||
if len(cfg.Monitor.Types) > 0 &&
|
if len(cfg.Monitor.Types) > 0 &&
|
||||||
cfg.Monitor.Types[0] != ".*" &&
|
!keyInMonitorTypesMap(".*", cfg) &&
|
||||||
!inStringArray(ext, cfg.Monitor.Types) {
|
!keyInMonitorTypesMap(ext, cfg) {
|
||||||
//log.Println(ext, cfg.Monitor.Types, inStringArray(ext, cfg.Monitor.Types))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch event.Op {
|
switch event.Op {
|
||||||
case fsnotify.Create:
|
case
|
||||||
case fsnotify.Write:
|
fsnotify.Write,
|
||||||
log.Println("event write : ", event.Name)
|
fsnotify.Rename:
|
||||||
if cmd != nil {
|
log.Println("EVENT", event.Op.String(), ":", event.Name)
|
||||||
err := cmd.Process.Kill()
|
taskMan.Put(&changedFile{
|
||||||
if err != nil {
|
|
||||||
log.Println("err: ", err)
|
|
||||||
}
|
|
||||||
log.Println("stop old process ")
|
|
||||||
}
|
|
||||||
go run(&changeFile{
|
|
||||||
Name: relativePath(projectFolder, event.Name),
|
Name: relativePath(projectFolder, event.Name),
|
||||||
Changed: time.Now().UnixNano(),
|
Changed: time.Now().UnixNano(),
|
||||||
Ext: ext,
|
Ext: ext,
|
||||||
})
|
})
|
||||||
case fsnotify.Remove:
|
case fsnotify.Remove:
|
||||||
case fsnotify.Rename:
|
case fsnotify.Create:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 ")
|
|
||||||
}
|
|
||||||
|
|
||||||
func addWatcher() {
|
func addWatcher() {
|
||||||
log.Println("collecting directory information...")
|
log.Println("collecting directory information...")
|
||||||
dirs := make([]string, 0)
|
dirsMap := map[string]bool{
|
||||||
for i := 0; i < len(cfg.Monitor.IncludeDirs); i++ {
|
projectFolder: true,
|
||||||
darr := dirParse2Array(cfg.Monitor.IncludeDirs[i])
|
}
|
||||||
|
for _, dir := range cfg.Monitor.IncludeDirs {
|
||||||
|
darr := dirParse2Array(dir)
|
||||||
if len(darr) < 1 || len(darr) > 2 {
|
if len(darr) < 1 || len(darr) > 2 {
|
||||||
log.Fatalln("filegirl section monitor dirs is error. ", cfg.Monitor.IncludeDirs[i])
|
logAndExit(PreError, "filegirl section monitor dirs is error. ", dir)
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(darr[0], "/") {
|
||||||
|
logAndExit(PreError, "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
|
||||||
})
|
})
|
||||||
|
break
|
||||||
} else {
|
} else {
|
||||||
dirs = arrayUniqueAdd(dirs, projectFolder)
|
dirsMap[projectFolder] = true
|
||||||
}
|
}
|
||||||
break
|
|
||||||
} else {
|
} else {
|
||||||
md := projectFolder + "/" + darr[0]
|
md := projectFolder + "/" + darr[0]
|
||||||
|
dirsMap[md] = true
|
||||||
if len(darr) == 2 && darr[1] == "*" {
|
if len(darr) == 2 && darr[1] == "*" {
|
||||||
dirs = arrayUniqueAdd(dirs, md)
|
|
||||||
listFile(md, func(d string) {
|
listFile(md, func(d string) {
|
||||||
dirs = arrayUniqueAdd(dirs, d)
|
dirsMap[d] = true
|
||||||
})
|
})
|
||||||
} else {
|
|
||||||
dirs = arrayUniqueAdd(dirs, md)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
for i := 0; i < len(cfg.Monitor.ExceptDirs); i++ {
|
for _, dir := range cfg.Monitor.ExceptDirs {
|
||||||
p := projectFolder + "/" + cfg.Monitor.ExceptDirs[i]
|
if dir == "." {
|
||||||
dirs = arrayRemoveElement(dirs, p)
|
logAndExit(PreError, "exceptDirs must is not project root path ! err path:", dir)
|
||||||
|
}
|
||||||
|
p := projectFolder + "/" + dir
|
||||||
|
delete(dirsMap, p)
|
||||||
listFile(p, func(d string) {
|
listFile(p, func(d string) {
|
||||||
dirs = arrayRemoveElement(dirs, d)
|
delete(dirsMap, d)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
for _, dir := range dirs {
|
for dir := range dirsMap {
|
||||||
log.Println("watcher add -> ", dir)
|
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("total monitored dirs: " + strconv.Itoa(len(dirsMap)))
|
||||||
log.Println("fileboy is ready.")
|
log.Println("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 {
|
||||||
@@ -195,58 +166,54 @@ func initWatcher() {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println("error:", err)
|
log.Println(PreError, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
addWatcher()
|
addWatcher()
|
||||||
<-done
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseArgs() {
|
func parseArgs() {
|
||||||
l := len(os.Args)
|
switch len(os.Args) {
|
||||||
if l == 1 {
|
case 1:
|
||||||
_, err := ioutil.ReadFile(projectFolder + "/filegirl.yaml")
|
parseConfig()
|
||||||
if err != nil {
|
done := make(chan bool)
|
||||||
log.Println("the filegirl.yaml file does not exist! ", err)
|
|
||||||
fmt.Print(firstRunHelp)
|
|
||||||
fmt.Print(helpStr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
initWatcher()
|
initWatcher()
|
||||||
|
defer watcher.Close()
|
||||||
|
<-done
|
||||||
return
|
return
|
||||||
}
|
case 2:
|
||||||
if l == 2 {
|
|
||||||
c := os.Args[1]
|
c := os.Args[1]
|
||||||
switch c {
|
switch c {
|
||||||
case "init":
|
case "init":
|
||||||
err := ioutil.WriteFile(projectFolder+"/filegirl.yaml", []byte(exampleFileGirl), 0644)
|
err := ioutil.WriteFile(projectFolder+"/filegirl.yaml", []byte(exampleFileGirl), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error create filegirl.yaml config! ", err)
|
log.Println(PreError, "error create filegirl.yaml config! ", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println("create filegirl.yaml ok")
|
log.Println("create filegirl.yaml ok")
|
||||||
return
|
return
|
||||||
case "exec":
|
case "exec":
|
||||||
parseConfig()
|
parseConfig()
|
||||||
run(new(changeFile))
|
newTaskMan(0, cfg.Notifier.CallUrl).run(new(changedFile))
|
||||||
return
|
return
|
||||||
case "version":
|
case "version", "v", "-v", "--version":
|
||||||
fallthrough
|
fmt.Println(versionDesc)
|
||||||
case "v":
|
|
||||||
fmt.Println("[VERSION] Release: v1.2 filegirl: " + strconv.Itoa(Version))
|
|
||||||
default:
|
default:
|
||||||
fmt.Print(helpStr)
|
fmt.Print(helpStr)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
default:
|
||||||
|
logAndExit("Unknown parameters, use `fileboy help` show help info.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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() {
|
||||||
@@ -256,7 +223,7 @@ func main() {
|
|||||||
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)
|
||||||
}
|
}
|
||||||
parseArgs()
|
parseArgs()
|
||||||
}
|
}
|
||||||
|
13
filegirl.go
13
filegirl.go
@@ -8,10 +8,17 @@ type FileGirl struct {
|
|||||||
Types []string `yaml:"types"`
|
Types []string `yaml:"types"`
|
||||||
IncludeDirs []string `yaml:"includeDirs"`
|
IncludeDirs []string `yaml:"includeDirs"`
|
||||||
ExceptDirs []string `yaml:"exceptDirs"`
|
ExceptDirs []string `yaml:"exceptDirs"`
|
||||||
|
// convert to
|
||||||
|
TypesMap map[string]bool `yaml:"-"`
|
||||||
|
IncludeDirsMap map[string]bool `yaml:"-"`
|
||||||
|
ExceptDirsMap map[string]bool `yaml:"-"`
|
||||||
|
DirsMap 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"`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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
|
||||||
|
|
78
notifer.go
Normal file
78
notifer.go
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type postParams struct {
|
||||||
|
ProjectFolder string `json:"project_folder"`
|
||||||
|
File string `json:"file"`
|
||||||
|
Changed int64 `json:"changed"`
|
||||||
|
Ext string `json:"ext"`
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
log.Println(PreWarn, "notifier call url ignore. ", n.CallUrl)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
n.dispatch(&postParams{
|
||||||
|
ProjectFolder: projectFolder,
|
||||||
|
File: cf.Name,
|
||||||
|
Changed: cf.Changed,
|
||||||
|
Ext: cf.Ext,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *NetNotifier) dispatch(params *postParams) {
|
||||||
|
b, err := json.Marshal(params)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(PreError, "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 {
|
||||||
|
log.Println(PreError, "http.NewRequest. ", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
|
||||||
|
req.Header.Set("User-Agent", "FileBoy Net Notifier v1.10")
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(PreError, "notifier call failed. err:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if resp != nil && resp.Body != nil {
|
||||||
|
_ = resp.Body.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
if resp.StatusCode >= 300 {
|
||||||
|
// todo retry???
|
||||||
|
}
|
||||||
|
log.Println("notifier done .")
|
||||||
|
}
|
41
raw.go
41
raw.go
@@ -1,5 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
var exampleFileGirl string = `# 主配置
|
var exampleFileGirl string = `# 主配置
|
||||||
core:
|
core:
|
||||||
# 配置版本号
|
# 配置版本号
|
||||||
@@ -36,7 +38,7 @@ command:
|
|||||||
# 可以有多条命令,会依次执行
|
# 可以有多条命令,会依次执行
|
||||||
# 如有多条命令,每条命令都会等待上一条命令执行完毕后才会执行
|
# 如有多条命令,每条命令都会等待上一条命令执行完毕后才会执行
|
||||||
# 如遇交互式命令,允许外部获取输入
|
# 如遇交互式命令,允许外部获取输入
|
||||||
# 支持变量占位符:
|
# 支持变量占位符,运行命令时会替换成实际值:
|
||||||
# {{file}} 文件名(如 a.txt 、test/test2/a.go)
|
# {{file}} 文件名(如 a.txt 、test/test2/a.go)
|
||||||
# {{ext}} 文件后缀(如 .go)
|
# {{ext}} 文件后缀(如 .go)
|
||||||
# {{changed}} 文件更新的本地时间戳(纳秒,如 1537326690523046400)
|
# {{changed}} 文件更新的本地时间戳(纳秒,如 1537326690523046400)
|
||||||
@@ -44,12 +46,31 @@ command:
|
|||||||
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.10
|
||||||
|
# Body: {"project_folder":"/watcher-dirs","file":"test.go","changed":1546421173070433800,"ext":".go"}
|
||||||
|
# 例: http://example.com/notifier/fileboy-listener
|
||||||
|
# 不启用通知,请留空 ""
|
||||||
|
callUrl: ""
|
||||||
`
|
`
|
||||||
|
|
||||||
var firstRunHelp = `第一次运行 fileboy ?
|
var firstRunHelp = `第一次运行 fileboy ?
|
||||||
你可能需要先执行 fileboy init 生成配置。
|
你可能需要先执行 fileboy init 生成配置。
|
||||||
更多信息查看帮助:
|
更多信息使用 fileboy help 查看帮助
|
||||||
`
|
`
|
||||||
|
|
||||||
var helpStr = `fileboy [option]
|
var helpStr = `fileboy [option]
|
||||||
@@ -61,7 +82,7 @@ Usage of fileboy:
|
|||||||
exec
|
exec
|
||||||
尝试运行定义的 command 命令
|
尝试运行定义的 command 命令
|
||||||
version
|
version
|
||||||
查看当前版本号
|
查看当前版本信息
|
||||||
`
|
`
|
||||||
|
|
||||||
var englishSay = []string{
|
var englishSay = []string{
|
||||||
@@ -83,5 +104,15 @@ var logo = `
|
|||||||
_____ _ | | _____ ____) ) | | | |___| |
|
_____ _ | | _____ ____) ) | | | |___| |
|
||||||
| ___) | | | | | ___) | __ (| | | |\_____/
|
| ___) | | | | | ___) | __ (| | | |\_____/
|
||||||
| | _| |_| |_____| |_____| |__) ) |___| | ___
|
| | _| |_| |_____| |_____| |__) ) |___| | ___
|
||||||
|_| (_____)_______)_______)______/ \_____/ (___)
|
|_| (_____)_______)_______)______/ \_____/ (___) V1.10
|
||||||
|
`
|
||||||
|
var statement = `Dengsgo [dengsgo@gmail.com] Open Source with MIT License`
|
||||||
|
|
||||||
|
var versionDesc = `
|
||||||
|
Version fileboy: v1.10 filegirl: v` + strconv.Itoa(Version) + `
|
||||||
|
Released 2019.6.4
|
||||||
|
Licence MIT
|
||||||
|
Author dengsgo [dengsgo@gmail.com]
|
||||||
|
Website https://github.com/dengsgo/fileboy
|
||||||
|
Blog https://www.yoytang.com
|
||||||
`
|
`
|
||||||
|
101
taskman.go
Normal file
101
taskman.go
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TaskMan struct {
|
||||||
|
lastTaskId int64
|
||||||
|
delay int
|
||||||
|
cmd *exec.Cmd
|
||||||
|
notifier *NetNotifier
|
||||||
|
putLock sync.Mutex
|
||||||
|
runLock sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTaskMan(delay int, callUrl string) *TaskMan {
|
||||||
|
return &TaskMan{
|
||||||
|
delay: delay,
|
||||||
|
notifier: newNetNotifier(callUrl),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TaskMan) Put(cf *changedFile) {
|
||||||
|
if t.delay < 1 {
|
||||||
|
t.preRun(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.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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
log.Println("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.Stderr = os.Stderr
|
||||||
|
t.cmd.Dir = projectFolder
|
||||||
|
t.cmd.Env = os.Environ()
|
||||||
|
stdout, err := t.cmd.StdoutPipe()
|
||||||
|
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
|
||||||
|
}
|
||||||
|
fmt.Print(line)
|
||||||
|
}
|
||||||
|
err = t.cmd.Wait()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(PreWarn, "cmd wait err ", err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if t.cmd.Process != nil {
|
||||||
|
if err = t.cmd.Process.Kill(); err != nil {
|
||||||
|
log.Println(PreWarn, "cmd cannot kill, reason:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("end ")
|
||||||
|
}
|
51
util.go
51
util.go
@@ -2,20 +2,18 @@ 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, cfg *FileGirl) bool {
|
||||||
for _, v := range arr {
|
_, ok := cfg.Monitor.TypesMap[k]
|
||||||
if value == v {
|
return ok
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmdParse2Array(s string, cf *changeFile) []string {
|
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,7 +24,7 @@ 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.Replace(
|
||||||
strings.Replace(
|
strings.Replace(
|
||||||
strings.Replace(s, "{{file}}", cf.Name, -1),
|
strings.Replace(s, "{{file}}", cf.Name, -1),
|
||||||
@@ -58,30 +56,15 @@ func listFile(folder string, fun func(string)) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func arrayUniqueAdd(a []string, add string) []string {
|
|
||||||
if inStringArray(add, a) {
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
return append(a, add)
|
|
||||||
}
|
|
||||||
|
|
||||||
func arrayRemoveElement(a []string, r string) []string {
|
|
||||||
i := -1
|
|
||||||
for k, v := range a {
|
|
||||||
if v == r {
|
|
||||||
i = k
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if i == -1 {
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
if len(a) == 1 && i == 0 {
|
|
||||||
return []string{}
|
|
||||||
}
|
|
||||||
return append(a[:i], a[i+1:]...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func relativePath(folder, p string) string {
|
func relativePath(folder, p string) string {
|
||||||
return strings.TrimPrefix(p, folder)
|
s := strings.ReplaceAll(strings.TrimPrefix(p, folder), "\\", "/")
|
||||||
|
if strings.HasPrefix(s, "/") && len(s) > 1 {
|
||||||
|
s = s[1:]
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func logAndExit(v ...interface{}) {
|
||||||
|
log.Println(v...)
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user