-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
111 lines (102 loc) · 2.19 KB
/
Copy pathmain.go
File metadata and controls
111 lines (102 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"context"
"io/ioutil"
"path/filepath"
"time"
"github.com/jinzhu/configor"
cron "github.com/robfig/cron/v3"
"golang.org/x/crypto/ssh"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing/object"
gitssh "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
)
const (
configPath = "config.toml"
)
var (
repository *git.Repository
worktree *git.Worktree
userStorage UserStorage
)
func main() {
var err error
err = configor.Load(Config, configPath)
if err != nil {
panic("配置加载失败")
}
repository, err = git.PlainOpen(Config.Work.GitPath)
if err != nil {
panic(err)
}
worktree, err = repository.Worktree()
if err != nil {
panic(err)
}
userStorage = NewMapStorage()
StartWork()
// 启动api接口
StartApi()
}
func StartWork() {
c := cron.New(cron.WithSeconds())
c.AddFunc("1 0 0,12 * * ?", func() {
for _, user := range userStorage.List() {
err := GitWork(user)
if err != nil {
user.ErrCount++
}
if user.ErrCount > Config.Work.MaxErrCount {
userStorage.Del(user.Sign())
} else {
userStorage.Set(user)
}
}
})
c.Start()
}
func GitWork(user *User) error {
now := time.Now()
user.LastRunTime = now
day := now.Format("2006-01-02")
ioutil.WriteFile(filepath.Join(Config.Work.GitPath, day), []byte(now.String()), 0666)
worktree.Add("./")
_, err := worktree.Commit("打卡器", &git.CommitOptions{
All: true,
Author: &object.Signature{
Name: user.User,
Email: user.Email,
When: now,
},
})
if err != nil {
return err
}
signer, err := ssh.ParsePrivateKey(user.DeployKeys)
if err != nil {
return err
}
auth := &gitssh.PublicKeys{
User: "git",
Signer: signer,
HostKeyCallbackHelper: gitssh.HostKeyCallbackHelper{
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
},
}
repository.DeleteRemote("origin")
remote, err := repository.CreateRemote(&config.RemoteConfig{
Name: "origin",
URLs: []string{user.RemoteURL},
})
if err != nil {
return err
}
ctx, cancelFunc := context.WithTimeout(context.Background(), 30*time.Second)
defer cancelFunc()
err = remote.PushContext(ctx, &git.PushOptions{
RemoteName: "origin",
Auth: auth,
})
return err
}