Skip to content

Commit 2ef71ca

Browse files
committed
feat: add PreDeleteCheckCount to FilterConfig and implement sync task functionality
1 parent b91efb8 commit 2ef71ca

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type GithubConfig struct {
4949

5050
type FilterConfig struct {
5151
UnmatchedRepoAction UnmatchedRepoAction `json:"unmatched_repo_action"`
52+
PreDeleteCheckCount int `json:"pre_delete_check_count"`
5253
AllowRule []string `json:"allow_rule"`
5354
DenyRule []string `json:"deny_rule"`
5455
}
@@ -77,6 +78,7 @@ func (c *GithubConfig) MergeDefault(defaultConf *DefaultConfig) {
7778
}
7879
if c.Filter.UnmatchedRepoAction == "" {
7980
c.Filter.UnmatchedRepoAction = defaultConf.Filter.UnmatchedRepoAction
81+
c.Filter.PreDeleteCheckCount = defaultConf.Filter.PreDeleteCheckCount
8082
if c.Filter.UnmatchedRepoAction == "" {
8183
c.Filter.UnmatchedRepoAction = UnmatchedRepoActionIgnore
8284
}

main.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,23 @@ func BuildBackupProvider(conf *BackupProviderConfig) (BackupProvider, error) {
4040
return nil, fmt.Errorf("unknown backup provider type: %s", conf.Type)
4141
}
4242

43-
func runBackupTask(conf *SyncConfig) {
44-
for _, target := range conf.Targets {
43+
type SyncTask struct {
44+
conf *SyncConfig
45+
counter map[string]int
46+
}
47+
48+
func NewTask(conf *SyncConfig) *SyncTask {
49+
return &SyncTask{
50+
conf: conf,
51+
counter: make(map[string]int, 100),
52+
}
53+
}
54+
55+
func (t *SyncTask) Run() {
56+
for _, target := range t.conf.Targets {
4557

4658
// merge default config
47-
target.MergeDefault(conf.DefaultConf)
59+
target.MergeDefault(t.conf.DefaultConf)
4860

4961
// load all github repos
5062
github := NewGithub(target.Token)
@@ -85,6 +97,7 @@ func runBackupTask(conf *SyncConfig) {
8597
}
8698

8799
// migrate repo
100+
delete(t.counter, repo.Name)
88101
s, e := provider.MigrateRepo(
89102
target.Owner, target.RepoOwner,
90103
target.IsOwnerOrg, target.IsRepoOwnerOrg,
@@ -112,6 +125,12 @@ func runBackupTask(conf *SyncConfig) {
112125
// delete unmatched repos
113126
for _, repo := range localRepos {
114127
if _, ok := handledRepos[repo]; !ok {
128+
if target.Filter.PreDeleteCheckCount > 0 {
129+
if t.counter[repo] < target.Filter.PreDeleteCheckCount {
130+
t.counter[repo]++
131+
continue
132+
}
133+
}
115134
s, e := provider.DeleteRepo(target.RepoOwner, repo)
116135
if e != nil {
117136
log.Printf("delete %s error: %s", repo, e.Error())
@@ -143,16 +162,15 @@ func main() {
143162
log.Fatalf("load config error: %s", err.Error())
144163
}
145164

165+
syncTask := NewTask(conf)
146166
if conf.Cron != "" {
147167
task := cron.New()
148-
_, e := task.AddFunc(conf.Cron, func() {
149-
runBackupTask(conf)
150-
})
168+
_, e := task.AddFunc(conf.Cron, syncTask.Run)
151169
if e != nil {
152170
log.Fatalf("add cron task error: %s", e.Error())
153171
}
154172
task.Run()
155173
} else {
156-
runBackupTask(conf)
174+
syncTask.Run()
157175
}
158176
}

0 commit comments

Comments
 (0)