FileWatcher
Posted findumars
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FileWatcher相关的知识,希望对你有一定的参考价值。
package main import ( "bytes" "flag" "github.com/go-fsnotify/fsnotify" "log" "os/exec" "strings" ) var watch_path = flag.String("path", "", "监控的目录") var cmd = flag.String("cmd", "", "要执行的命令") var suffix = flag.String("suffix", ".mmd", "监控文件后缀") func main() { flag.Parse() watcher, err := fsnotify.NewWatcher() if err != nil { log.Fatal(err) } defer watcher.Close() done := make(chan bool) go func() { for { select { case event := <-watcher.Events: log.Println(event) if event.Op&fsnotify.Write == fsnotify.Write { if strings.HasSuffix(event.Name, *suffix) { log.Println("modified file: ", event.Name) //cmd_to_exec := "/usr/local/bin/mermaid -w 1200 -t" + watch_path + "/mermaid.css -g " + watch_path + "/config.json -o " + watch_path + "/output " + event.Name //cmd_to_exec := fmt.Sprintf(*cmd, event.Name) cmd_to_exec := strings.Replace(*cmd, "[filename]", event.Name, -1) cmd_to_exec = strings.Replace(cmd_to_exec, "[path]", *watch_path, -1) log.Println("cmd to execute: ", cmd_to_exec) cmd := exec.Command("sh", "-c", cmd_to_exec) var out_stdout bytes.Buffer var out_stderr bytes.Buffer cmd.Stderr = &out_stderr cmd.Stdout = &out_stdout err := cmd.Run() if err != nil { log.Println("command execute failed: ", err) } log.Println(out_stdout.String()) if out_stderr.Len() > 0 { log.Println("Error: ", out_stderr.String()) } } } case err := <-watcher.Errors: log.Println("error:", err) } } }() err = watcher.Add(*watch_path) if err != nil { log.Fatal(err) } log.Println("watching directory: " + *watch_path) <-done }
https://github.com/mylxsw/FileWatcher/blob/master/watcher.go
以上是关于FileWatcher的主要内容,如果未能解决你的问题,请参考以下文章
markdown Webstorm FileWatcher用于更漂亮的格式化
使用 PyCharmWebStormIDEA 的 File Watcher 插件实时更新文件