10秒一次,实时拉取最新代码;
package main
import (
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"fmt"
"io"
"os"
"time"
"github.com/go-git/go-git/v5"
)
func main() {
gitClone(context.Background())
gitPull(context.Background())
}
var Global_GIT *git.Repository
var GIT_TOKEN string = "https://username:token@gitlab.tencent.net/dba/job.git"
func gitPull(ctx context.Context) {
localTimer := time.NewTimer(10 * time.Second)
var err error
gp, err := Global_GIT.Worktree()
if err != nil {
panic(err)
}
for {
<-localTimer.C
err = gp.Pull(&git.PullOptions{
RemoteName: "origin",
Progress: os.Stdout,
})
if err != nil {
fmt.Println(err)
}
localTimer.Reset(10 * time.Second)
}
}
func gitClone(ctx context.Context) {
repo := GIT_TOKEN
gitDir := "/tmp/foo"
os.Remove(gitDir)
err := os.MkdirAll(gitDir, os.ModePerm)
if err != nil {
panic(err)
}
r, err := git.PlainClone(gitDir, false, &git.CloneOptions{
URL: repo,
Progress: os.Stdout,
})
if err != nil {
panic(err)
}
Global_GIT = r
fmt.Println("init git clone success!")
}