sync.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "regexp"
  7. "strings"
  8. "time"
  9. ccfg "git.wecise.com/wecise/common/matrix/cfg"
  10. clog "git.wecise.com/wecise/common/matrix/logger"
  11. "git.wecise.com/wecise/util/filewalker"
  12. )
  13. var config = ccfg.MConfig()
  14. var logger = clog.New()
  15. // 同步指定文件夹中的内容
  16. func PrintUsage() {
  17. println(`Usage:
  18. sync <frompath> <topath>
  19. `)
  20. }
  21. var option = struct {
  22. Debug bool
  23. }{
  24. Debug: false,
  25. }
  26. func main() {
  27. var frompath string = ""
  28. var topath string = ""
  29. for _, arg := range os.Args[1:] {
  30. if strings.HasPrefix(arg, "-") {
  31. switch arg {
  32. case "-debug":
  33. option.Debug = true
  34. }
  35. } else if arg != "" {
  36. if frompath == "" {
  37. frompath = arg
  38. } else if topath == "" {
  39. topath = arg
  40. }
  41. }
  42. }
  43. switch {
  44. case frompath != "" && topath != "":
  45. fmt.Println("sync from", frompath, "to", topath)
  46. // err := filepath.Walk(frompath, func(path string, info fs.FileInfo, err error) error {
  47. // fmt.Println(path)
  48. // if err != nil {
  49. // fmt.Println(err)
  50. // }
  51. // if info != nil {
  52. // fmt.Println(path, info.Size(), info.ModTime())
  53. // }
  54. // return nil
  55. // })
  56. // if err != nil {
  57. // println(err.Error())
  58. // os.Exit(1)
  59. // }
  60. for {
  61. err := sync(frompath, topath)
  62. if err != nil {
  63. println(err.Error())
  64. os.Exit(1)
  65. }
  66. time.Sleep(5 * time.Second)
  67. }
  68. default:
  69. PrintUsage()
  70. os.Exit(1)
  71. }
  72. }
  73. func sync(frompath, topath string) (err error) {
  74. fw, e := filewalker.NewFileWalker([]string{frompath}, `^[^\.].*`) // orderby: dirfirst, filefirst, fullpath
  75. if e != nil {
  76. return e
  77. }
  78. count := 0
  79. e = fw.List(func(basedir, fpath string) bool {
  80. absbasedir, e := filepath.Abs(basedir)
  81. if e != nil {
  82. err = e
  83. return false
  84. }
  85. abstopath, e := filepath.Abs(topath)
  86. if e != nil {
  87. err = e
  88. return false
  89. }
  90. hiddenpath := fmt.Sprintf(`.*\%c\..*`, os.PathSeparator)
  91. if regexp.MustCompile(`^[^\.].*`).MatchString(fpath) && !regexp.MustCompile(hiddenpath).MatchString(fpath) {
  92. fromfile := filepath.Join(absbasedir, fpath)
  93. tofile := filepath.Join(abstopath, fpath)
  94. if option.Debug {
  95. fmt.Println(time.Now().Format("2006-01-02 15:04:05"), fromfile, " ==> ", tofile)
  96. }
  97. todir := filepath.Dir(tofile)
  98. e = os.MkdirAll(todir, os.ModePerm)
  99. if e != nil {
  100. err = fmt.Errorf("to dir %v", e)
  101. return false
  102. }
  103. fromfi, e := os.Stat(fromfile)
  104. if e != nil {
  105. err = fmt.Errorf("from file %v", e)
  106. return false
  107. }
  108. tofi, e := os.Stat(tofile)
  109. if e != nil && !os.IsNotExist(e) {
  110. err = fmt.Errorf("to file %v", e)
  111. return false
  112. }
  113. if tofi != nil && fromfi.Size() == tofi.Size() && fromfi.ModTime() == tofi.ModTime() {
  114. return true
  115. }
  116. fmt.Println(fromfile, " ==> ", tofile)
  117. frombs, e := os.ReadFile(fromfile)
  118. if e != nil {
  119. err = fmt.Errorf("from file %v", e)
  120. return false
  121. }
  122. e = os.WriteFile(tofile, frombs, os.ModePerm)
  123. if e != nil {
  124. err = fmt.Errorf("to file %v", e)
  125. return false
  126. }
  127. e = os.Chtimes(tofile, fromfi.ModTime(), fromfi.ModTime())
  128. if e != nil {
  129. err = fmt.Errorf("to file %v", e)
  130. return false
  131. }
  132. count++
  133. }
  134. return true
  135. })
  136. if e != nil {
  137. return e
  138. }
  139. if err != nil {
  140. return
  141. }
  142. if count > 0 || option.Debug {
  143. fmt.Println(time.Now().Format("2006-01-02 15:04:05"), count, "files copied")
  144. }
  145. return
  146. }