golang监控文件变化,git自动提交代码

代码如下:如果文件有变动,且10分钟内,没有再次变动,则提交代码

package main

import (
	"fmt"
	_ "fmt"
	"github.com/fsnotify/fsnotify"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"time"
)

//if the conditions are met, execute the shell script
func execCmd() {
	cmd := exec.Command("/root/nfs_bak_pro/nfs.git.sh")
	err := cmd.Run()
	if err != nil {
		fmt.Println("Execute Command failed:" + err.Error())
		return
	}
	fmt.Println("Execute Command finished.")
}

//handle folder files changed event
func watchFiles(watcher *fsnotify.Watcher, ch chan int64) {
	for {
		select {
			case ev := <-watcher.Events: {
				isNotify := false

				if ev.Op & fsnotify.Create == fsnotify.Create {
					log.Println("create : ", ev.Name)
					isNotify = true

					file, err := os.Stat(ev.Name)
					if err == nil && file.IsDir() {
						watcher.Add(ev.Name)
						fmt.Println("add watch : ", ev.Name)
					}
				}

				if ev.Op & fsnotify.Remove == fsnotify.Remove {
					log.Println("delete : ", ev.Name)
					isNotify = true
					err := watcher.Remove(ev.Name)
					fmt.Printf("remove watch: %s, err: %v\n", ev.Name, err)
				}

				if ev.Op & fsnotify.Rename == fsnotify.Rename {
					log.Println("rename : ", ev.Name)
					if "" != ev.Name {
						isNotify = true
						err := watcher.Remove(ev.Name)
						fmt.Printf("remove watch: %s, err: %v\n", ev.Name, err)
					}
				}

				if isNotify {
					ch <- time.Now().Unix()
				}
			}
			case err := <-watcher.Errors: {
				log.Println("watcher error : ", err)
				return
			}
		}
	}
}

//if folder event met, execute the shell script after 10minutes
func watchTime(ch chan int64) {
	var timer *time.Timer
	for {
		select {
			case <- ch:{
				if nil != timer {
					log.Printf("reset timer")
					timer.Stop()
				}
				timer = time.NewTimer(10 * 60 * time.Second)
				go func() {
					<-timer.C
					execCmd()
				}()
			}
		}
	}
}

//watch the folder and sub folders
func WatchDir(watcher *fsnotify.Watcher, dir string) {
	filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
		if info.IsDir() {
			path, err := filepath.Abs(path)
			if err != nil {
				return err
			}
			err = watcher.Add(path)
			if err != nil {
				return err
			}
		}
		return nil
	})
}


func main() {
	notifyCh := make(chan int64)
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	WatchDir(watcher, "/data/nfs")
	go watchFiles(watcher, notifyCh)
	go watchTime(notifyCh)
	select {}
}

shell 脚本如下

#!/bin/bash

cd /root/nfs_bak_pro/nfs.git
log_file=/root/nfs_bak_pro/nfs_git_`date +"%Y%m%d"`.log

git add --all . >> $log_file
git commit  -a -m "`date +"%Y-%m-%d %H:%M:%S"`" >> $log_file
git push origin master >> $log_file

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值