Skip to content

Commit

Permalink
feat: 更新本地提供者配置,支持通过Action字段指定更新操作
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Jan 25, 2025
1 parent 9bb8cb3 commit 68e04f4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
syncTask := NewTask(conf)
if conf.Cron != "" {
task := cron.New()
_, e := task.AddFunc(conf.Cron, syncTask.Run)
_, e := task.AddJob(conf.Cron, syncTask)
if e != nil {
log.Fatalf("add cron task error: %s", e.Error())
}
Expand Down
29 changes: 17 additions & 12 deletions provider/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ import (
"path/filepath"
)

type UpdateAction string

const (
UpdateActionPull = "pull"
UpdateActionFetch = "fetch"
)

type Config struct {
Root string `json:"root"`
Questions bool `json:"questions"`
PullAfterFetch bool `json:"pull_after_fetch"`
Root string `json:"root"`
Questions bool `json:"questions"`
Action UpdateAction `json:"action"`
}

var _ provider.Provider = &Local{}
Expand Down Expand Up @@ -72,7 +79,7 @@ func (l *Local) MigrateRepo(from *provider.Owner, to *provider.Owner, repo *prov
return "", err
}
}
err = gitFetchAndTryPull(repoPath, l.conf.PullAfterFetch)
err = gitUpdateLocal(repoPath, l.conf.Action)
if err != nil {
return "fail", err
}
Expand Down Expand Up @@ -104,19 +111,17 @@ func gitClone(url, path string) error {
return cmd.Run()
}

func gitFetchAndTryPull(path string, pullAfterFetch bool) error {
log.Printf("pulling %s", path)
cmd := exec.Command("git", "fetch", "--all")
func gitUpdateLocal(path string, action UpdateAction) error {
log.Printf("action %s", path)
if action != UpdateActionPull && action != UpdateActionFetch {
return fmt.Errorf("unsupported action: %s", action)
}
cmd := exec.Command("git", string(action), "--all")
cmd.Dir = path
err := cmd.Run()
if err != nil {
return err
}
if pullAfterFetch {
cmd = exec.Command("git", "pull")
cmd.Dir = path
return cmd.Run()
}
return nil
}

Expand Down

0 comments on commit 68e04f4

Please sign in to comment.