git_helper.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package backend
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "github.com/allanpk716/ChineseSubFinder/pkg/types/common"
  7. "github.com/allanpk716/ChineseSubFinder/cmd/GetCAPTCHA/backend/config"
  8. "github.com/allanpk716/ChineseSubFinder/pkg/my_util"
  9. "github.com/allanpk716/ChineseSubFinder/pkg/something_static"
  10. "github.com/go-git/go-git/v5"
  11. "github.com/go-git/go-git/v5/plumbing/object"
  12. "github.com/go-git/go-git/v5/plumbing/transport/ssh"
  13. "github.com/sirupsen/logrus"
  14. sshOrg "golang.org/x/crypto/ssh"
  15. )
  16. func GitProcess(log *logrus.Logger, config config.Config, enString string) error {
  17. log.Infoln("Now Time", time.Now().Format("2006-01-02 15:04:05"))
  18. var delFileNames []string
  19. nowTT := time.Now()
  20. nowTime := nowTT.Format("2006-01-02")
  21. nowTimeFileNamePrix := fmt.Sprintf("%d%d%d", nowTT.Year(), nowTT.Month(), nowTT.Day())
  22. // 实例化登录密钥
  23. publicKeys, err := ssh.NewPublicKeysFromFile("git", config.SSHKeyFullPath, config.SSHKeyPwd)
  24. if err != nil {
  25. return err
  26. }
  27. publicKeys.HostKeyCallback = sshOrg.InsecureIgnoreHostKey()
  28. var r *git.Repository
  29. var w *git.Worktree
  30. if my_util.IsDir(config.CloneProjectDesSaveDir) == true {
  31. // 需要 pull
  32. log.Infoln("Pull Start...")
  33. r, err = git.PlainOpen(config.CloneProjectDesSaveDir)
  34. if err != nil {
  35. return err
  36. }
  37. w, err = r.Worktree()
  38. if err != nil {
  39. return err
  40. }
  41. err = w.Pull(&git.PullOptions{Auth: publicKeys, RemoteName: "origin"})
  42. if err != nil {
  43. if err != git.NoErrAlreadyUpToDate {
  44. return err
  45. }
  46. }
  47. log.Infoln("Pull End")
  48. } else {
  49. // 需要 clone
  50. log.Infoln("PlainClone Start...")
  51. r, err = git.PlainClone(config.CloneProjectDesSaveDir, false, &git.CloneOptions{
  52. Auth: publicKeys,
  53. URL: config.GitProjectUrl,
  54. Progress: os.Stdout,
  55. })
  56. if err != nil {
  57. return err
  58. }
  59. log.Infoln("PlainClone End")
  60. }
  61. // 存储外部传入的字符串到文件
  62. bok, err := something_static.WriteFile(config.CloneProjectDesSaveDir, enString, nowTime, nowTimeFileNamePrix)
  63. if err != nil {
  64. return err
  65. }
  66. if bok == false {
  67. // 说明无需继续,因为文件没有变化
  68. log.Infoln("Code not change, Skip This Time")
  69. return nil
  70. }
  71. log.Infoln("Write File Done")
  72. w, err = r.Worktree()
  73. if err != nil {
  74. return err
  75. }
  76. // 遍历当前文件夹,仅仅保留当天的文件 nowTimeFileNamePrix + common.StaticFileName00
  77. delFileNames, err = delExpireFile(log, config.CloneProjectDesSaveDir, nowTimeFileNamePrix+common.StaticFileName00)
  78. if err != nil {
  79. return err
  80. }
  81. for _, delFileName := range delFileNames {
  82. _, err = w.Remove(delFileName)
  83. if err != nil {
  84. return err
  85. }
  86. }
  87. // 添加文件到 git 文件树
  88. _, err = w.Add(nowTimeFileNamePrix + common.StaticFileName00)
  89. if err != nil {
  90. return err
  91. }
  92. status, err := w.Status()
  93. log.Infoln("Status", status)
  94. commit, err := w.Commit("update", &git.CommitOptions{
  95. Author: &object.Signature{
  96. Name: "haha",
  97. Email: "[email protected]",
  98. When: time.Now(),
  99. },
  100. })
  101. if err != nil {
  102. return err
  103. }
  104. _, err = r.CommitObject(commit)
  105. if err != nil {
  106. return err
  107. }
  108. log.Infoln("Commit Done")
  109. err = r.Push(&git.PushOptions{
  110. Auth: publicKeys,
  111. })
  112. if err != nil {
  113. return err
  114. }
  115. log.Infoln("Push Done.")
  116. return nil
  117. }
  118. func delExpireFile(log *logrus.Logger, dir string, goldName string) ([]string, error) {
  119. delFileNames := make([]string, 0)
  120. pathSep := string(os.PathSeparator)
  121. files, err := os.ReadDir(dir)
  122. if err != nil {
  123. return nil, err
  124. }
  125. for _, curFile := range files {
  126. fullPath := dir + pathSep + curFile.Name()
  127. if curFile.IsDir() {
  128. continue
  129. } else {
  130. if "ReadMe.md" == curFile.Name() {
  131. continue
  132. }
  133. // 这里就是文件了
  134. if curFile.Name() != goldName {
  135. log.Infoln("Del Expire File:", fullPath)
  136. err = os.Remove(fullPath)
  137. if err != nil {
  138. return nil, err
  139. }
  140. delFileNames = append(delFileNames, curFile.Name())
  141. }
  142. }
  143. }
  144. return delFileNames, nil
  145. }