Skip to content

Commit 68e04f4

Browse files
committed
feat: 更新本地提供者配置,支持通过Action字段指定更新操作
1 parent 9bb8cb3 commit 68e04f4

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func main() {
3434
syncTask := NewTask(conf)
3535
if conf.Cron != "" {
3636
task := cron.New()
37-
_, e := task.AddFunc(conf.Cron, syncTask.Run)
37+
_, e := task.AddJob(conf.Cron, syncTask)
3838
if e != nil {
3939
log.Fatalf("add cron task error: %s", e.Error())
4040
}

provider/local/local.go

+17-12
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@ import (
99
"path/filepath"
1010
)
1111

12+
type UpdateAction string
13+
14+
const (
15+
UpdateActionPull = "pull"
16+
UpdateActionFetch = "fetch"
17+
)
18+
1219
type Config struct {
13-
Root string `json:"root"`
14-
Questions bool `json:"questions"`
15-
PullAfterFetch bool `json:"pull_after_fetch"`
20+
Root string `json:"root"`
21+
Questions bool `json:"questions"`
22+
Action UpdateAction `json:"action"`
1623
}
1724

1825
var _ provider.Provider = &Local{}
@@ -72,7 +79,7 @@ func (l *Local) MigrateRepo(from *provider.Owner, to *provider.Owner, repo *prov
7279
return "", err
7380
}
7481
}
75-
err = gitFetchAndTryPull(repoPath, l.conf.PullAfterFetch)
82+
err = gitUpdateLocal(repoPath, l.conf.Action)
7683
if err != nil {
7784
return "fail", err
7885
}
@@ -104,19 +111,17 @@ func gitClone(url, path string) error {
104111
return cmd.Run()
105112
}
106113

107-
func gitFetchAndTryPull(path string, pullAfterFetch bool) error {
108-
log.Printf("pulling %s", path)
109-
cmd := exec.Command("git", "fetch", "--all")
114+
func gitUpdateLocal(path string, action UpdateAction) error {
115+
log.Printf("action %s", path)
116+
if action != UpdateActionPull && action != UpdateActionFetch {
117+
return fmt.Errorf("unsupported action: %s", action)
118+
}
119+
cmd := exec.Command("git", string(action), "--all")
110120
cmd.Dir = path
111121
err := cmd.Run()
112122
if err != nil {
113123
return err
114124
}
115-
if pullAfterFetch {
116-
cmd = exec.Command("git", "pull")
117-
cmd.Dir = path
118-
return cmd.Run()
119-
}
120125
return nil
121126
}
122127

0 commit comments

Comments
 (0)