-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
158 lines (138 loc) · 3.69 KB
/
main.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"flag"
"fmt"
"github.com/robfig/cron/v3"
"log"
)
var (
BuildVersion = "dev"
)
type BackupProvider interface {
LoadRepos(owner string, isOrg bool) ([]string, error)
MigrateRepo(
owner, repoOwner string,
isOwnerOrg, isRepoOwnerOrg bool,
repoName, repoDesc string,
githubToken string,
) (string, error)
DeleteRepo(owner, repo string) (string, error)
}
func BuildBackupProvider(conf *BackupProviderConfig) (BackupProvider, error) {
switch conf.Type {
case BackupProviderConfigTypeGitea:
c, err := ConvertToBackupProviderConfig[GiteaConf](conf.Config)
if err != nil {
return nil, err
}
return NewGitea(c), nil
case BackupProviderConfigTypeFile:
c, err := ConvertToBackupProviderConfig[FileBackupConfig](conf.Config)
if err != nil {
log.Fatalf("convert file config error: %s", err.Error())
}
return NewFileBackup(c)
}
return nil, fmt.Errorf("unknown backup provider type: %s", conf.Type)
}
func runBackupTask(conf *SyncConfig) {
for _, target := range conf.Targets {
// merge default config
target.MergeDefault(conf.DefaultConf)
// load all github repos
github := NewGithub(target.Token)
repos, err := github.LoadAllRepos(target.Owner, target.IsOwnerOrg)
if err != nil {
log.Panicf("load %s repos error: %s", target.RepoOwner, err.Error())
}
// build backup provider
provider, err := BuildBackupProvider(target.Backup)
if err != nil {
log.Panicf("build backup provider error: %s", err.Error())
}
// handle repos set
handledRepos := make(map[string]struct{})
for _, repo := range repos {
// render repo identity
identity := RepoIdentity(target.Owner, repo.Name, repo.Private, repo.Fork, repo.Archived)
// check allow/deny rule
if target.Filter != nil {
if !IsMatchRepoIdentity(identity, target.Filter.AllowRule...) {
if IsMatchRepoIdentity(identity, target.Filter.DenyRule...) {
continue
}
}
}
githubToken := target.Token
// check specific GitHub token for this repo by regex
for k, v := range target.SpecificGithubToken {
if IsMatchRepoIdentity(identity, k) {
githubToken = v
break
}
}
// migrate repo
s, e := provider.MigrateRepo(
target.Owner, target.RepoOwner,
target.IsOwnerOrg, target.IsRepoOwnerOrg,
repo.Name, repo.Description,
githubToken,
)
if e != nil {
log.Printf("migrate %s error: %s", repo.Name, e.Error())
} else {
log.Printf("migrate %s %s", repo.Name, s)
}
handledRepos[repo.Name] = struct{}{}
}
// delete unmatched repos if needed
if target.Filter.UnmatchedRepoAction == UnmatchedRepoActionDelete {
// load local repos
localRepos, lErr := provider.LoadRepos(target.RepoOwner, target.IsRepoOwnerOrg)
if lErr != nil {
log.Panicf("load %s repos error: %s", target.RepoOwner, lErr.Error())
}
// delete unmatched repos
for _, repo := range localRepos {
if _, ok := handledRepos[repo]; !ok {
s, e := provider.DeleteRepo(target.RepoOwner, repo)
if e != nil {
log.Printf("delete %s error: %s", repo, e.Error())
} else {
log.Printf("delete %s %s", repo, s)
}
}
}
}
}
}
func main() {
c := flag.String("config", "config.json", "config file")
v := flag.Bool("version", false, "show version")
h := flag.Bool("help", false, "show help")
flag.Parse()
if *v {
fmt.Println(BuildVersion)
return
}
if *h {
flag.Usage()
return
}
conf, err := LoadConfig(*c)
if err != nil {
log.Fatalf("load config error: %s", err.Error())
}
if conf.Cron != "" {
task := cron.New()
_, e := task.AddFunc(conf.Cron, func() {
runBackupTask(conf)
})
if e != nil {
log.Fatalf("add cron task error: %s", e.Error())
}
task.Run()
} else {
runBackupTask(conf)
}
}