x
This commit is contained in:
parent
738ae34572
commit
23115aaee9
@ -9,19 +9,19 @@ import (
|
||||
"github.com/beego/beego/v2/core/logs"
|
||||
)
|
||||
|
||||
func cmd(str string, msgs ...interface{}) {
|
||||
func cmd(str string, msgs ...interface{}) string {
|
||||
cmd := exec.Command("sh", "-c", str)
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
stderr, err := cmd.StderrPipe()
|
||||
if err != nil {
|
||||
logs.Warn("cmd.StdoutPipe: ", err)
|
||||
return
|
||||
return err.Error()
|
||||
}
|
||||
cmd.Dir = ExecPath + "/scripts/"
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
logs.Warn("%v", err)
|
||||
return
|
||||
return err.Error()
|
||||
}
|
||||
go func() {
|
||||
msg := ""
|
||||
@ -57,4 +57,5 @@ func cmd(str string, msgs ...interface{}) {
|
||||
sendMessagee(msg, msgs...)
|
||||
}
|
||||
err = cmd.Wait()
|
||||
return msg
|
||||
}
|
||||
|
||||
@ -6,6 +6,9 @@ import (
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/beego/beego/v2/core/logs"
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
type Repo struct {
|
||||
@ -49,7 +52,9 @@ func initRepos() {
|
||||
func GitPullAll() {
|
||||
for i := range Config.Repos {
|
||||
if Config.Repos[i].exist() {
|
||||
Config.Repos[i].gitPull()
|
||||
if strings.Contains(Config.Repos[i].gitPull(), "changed") {
|
||||
Config.Repos[i].addTask()
|
||||
}
|
||||
Config.Repos[i].cpConfig()
|
||||
}
|
||||
}
|
||||
@ -67,8 +72,8 @@ func (rp *Repo) gitClone() {
|
||||
cmd(fmt.Sprintf("cd %s && git clone %s %s", reposPath, rp.Git, rp.filename))
|
||||
}
|
||||
|
||||
func (rp *Repo) gitPull() {
|
||||
cmd(fmt.Sprintf("cd %s && git stash && git pull", rp.path))
|
||||
func (rp *Repo) gitPull() string {
|
||||
return cmd(fmt.Sprintf("cd %s && git stash && git pull", rp.path))
|
||||
}
|
||||
|
||||
func (rp *Repo) cpConfig() {
|
||||
@ -80,6 +85,7 @@ func (rp *Repo) addTask() {
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
nts := []Task{}
|
||||
for _, v := range dir_list {
|
||||
if strings.Contains(v.Name(), ".js") {
|
||||
f, err := os.Open(rp.path + "/" + v.Name())
|
||||
@ -88,11 +94,32 @@ func (rp *Repo) addTask() {
|
||||
}
|
||||
data, _ := ioutil.ReadAll(f)
|
||||
f.Close()
|
||||
// fmt.Println(data)
|
||||
res := regexp.MustCompile(".*Env[(]['\"](\\S+)['\"][)]").FindStringSubmatch(string(data))
|
||||
res := regexp.MustCompile(`([\d\-,\*]+ [\d\-,\*]+ [\d\-,\*]+ [*]+ [*]+)[\s\S]+Env[(]['"]([^'"]+)['"][)]`).FindStringSubmatch(string(data))
|
||||
if len(res) > 0 {
|
||||
fmt.Println(res[1])
|
||||
nts = append(nts, Task{
|
||||
Cron: res[1],
|
||||
Name: v.Name(),
|
||||
Title: res[2],
|
||||
Git: rp.path,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
for i := range rp.Task {
|
||||
if rp.Task[i].ID != 0 {
|
||||
c.Remove(cron.EntryID(rp.Task[i].ID))
|
||||
}
|
||||
}
|
||||
rp.Task = nts
|
||||
for i := range rp.Task {
|
||||
task := &rp.Task[i]
|
||||
eid, err := c.AddFunc(task.Cron, func() {
|
||||
logs.Info("执行任务 %s %s ", task.Title, task.Cron)
|
||||
runTask(task)
|
||||
})
|
||||
if err == nil {
|
||||
logs.Info("添加任务 %s %s ", rp.Task[i].Title, rp.Task[i].Cron)
|
||||
rp.Task[i].ID = int(eid)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,6 +26,7 @@ type Task struct {
|
||||
Args string
|
||||
Hack bool
|
||||
Git string
|
||||
Title string
|
||||
}
|
||||
|
||||
type Env struct {
|
||||
@ -54,7 +55,10 @@ func createTask(task *Task) {
|
||||
}
|
||||
|
||||
func runTask(task *Task, msgs ...interface{}) string {
|
||||
if task.Name == "" {
|
||||
path := ""
|
||||
if task.Git != "" {
|
||||
path = task.Git + "/" + task.Name
|
||||
} else {
|
||||
slice := strings.Split(task.Path, "/")
|
||||
len := len(slice)
|
||||
if len == 0 {
|
||||
@ -62,40 +66,40 @@ func runTask(task *Task, msgs ...interface{}) string {
|
||||
return ""
|
||||
}
|
||||
task.Name = slice[len-1]
|
||||
}
|
||||
var path = ExecPath + "/scripts/" + task.Name
|
||||
if strings.Contains(task.Path, "http") {
|
||||
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
|
||||
if err != nil {
|
||||
logs.Warn("打开%s失败,", path, err)
|
||||
return ""
|
||||
}
|
||||
url := task.Path
|
||||
if strings.Contains(url, "raw.githubusercontent.com") {
|
||||
url = GhProxy + url
|
||||
}
|
||||
r, err := httplib.Get(url).Response()
|
||||
if err != nil {
|
||||
logs.Warn("下载%s失败,", task.Path, err)
|
||||
}
|
||||
io.Copy(f, r.Body)
|
||||
f.Close()
|
||||
} else {
|
||||
if path != task.Path && task.Name != task.Path {
|
||||
path = ExecPath + "/scripts/" + task.Name
|
||||
if strings.Contains(task.Path, "http") {
|
||||
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
|
||||
if err != nil {
|
||||
logs.Warn("打开%s失败,", path, err)
|
||||
return ""
|
||||
}
|
||||
f2, err := os.Open(task.Path)
|
||||
if err != nil {
|
||||
f.Close()
|
||||
logs.Warn("打开%s失败,", path, err)
|
||||
return ""
|
||||
url := task.Path
|
||||
if strings.Contains(url, "raw.githubusercontent.com") {
|
||||
url = GhProxy + url
|
||||
}
|
||||
io.Copy(f, f2)
|
||||
f2.Close()
|
||||
r, err := httplib.Get(url).Response()
|
||||
if err != nil {
|
||||
logs.Warn("下载%s失败,", task.Path, err)
|
||||
}
|
||||
io.Copy(f, r.Body)
|
||||
f.Close()
|
||||
} else {
|
||||
if path != task.Path && task.Name != task.Path {
|
||||
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
|
||||
if err != nil {
|
||||
logs.Warn("打开%s失败,", path, err)
|
||||
return ""
|
||||
}
|
||||
f2, err := os.Open(task.Path)
|
||||
if err != nil {
|
||||
f.Close()
|
||||
logs.Warn("打开%s失败,", path, err)
|
||||
return ""
|
||||
}
|
||||
io.Copy(f, f2)
|
||||
f2.Close()
|
||||
f.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
lan := Config.Node
|
||||
@ -118,7 +122,11 @@ func runTask(task *Task, msgs ...interface{}) string {
|
||||
logs.Warn("cmd.StdoutPipe: ", err)
|
||||
return ""
|
||||
}
|
||||
cmd.Dir = ExecPath + "/scripts/"
|
||||
if task.Git != "" {
|
||||
cmd.Dir = task.Git
|
||||
} else {
|
||||
cmd.Dir = ExecPath + "/scripts/"
|
||||
}
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
logs.Warn("%v", err)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user