sync.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. for {
  47. err := sync(frompath, topath)
  48. if err != nil {
  49. println(err.Error())
  50. os.Exit(1)
  51. }
  52. time.Sleep(5 * time.Second)
  53. }
  54. default:
  55. PrintUsage()
  56. os.Exit(1)
  57. }
  58. }
  59. func sync(frompath, topath string) (err error) {
  60. fw, e := filewalker.NewFileWalker([]string{frompath}, `^[^\.].*`) // orderby: dirfirst, filefirst, fullpath
  61. if e != nil {
  62. return e
  63. }
  64. count := 0
  65. e = fw.List(func(basedir, fpath string) bool {
  66. if option.Debug {
  67. fmt.Println("basedir=", basedir, ", fpath=", fpath)
  68. }
  69. absbasedir, e := filepath.Abs(basedir)
  70. if e != nil {
  71. err = e
  72. return false
  73. }
  74. abstopath, e := filepath.Abs(topath)
  75. if e != nil {
  76. err = e
  77. return false
  78. }
  79. hiddenpath := fmt.Sprintf(`.*\%c\..*`, os.PathSeparator)
  80. if regexp.MustCompile(`^[^\.].*`).MatchString(fpath) && !regexp.MustCompile(hiddenpath).MatchString(fpath) {
  81. fromfile := filepath.Join(absbasedir, fpath)
  82. tofile := filepath.Join(abstopath, fpath)
  83. if option.Debug {
  84. fmt.Println(time.Now().Format("2006-01-02 15:04:05"), fromfile, " ==> ", tofile)
  85. }
  86. todir := filepath.Dir(tofile)
  87. e = os.MkdirAll(todir, os.ModePerm)
  88. if e != nil {
  89. err = fmt.Errorf("to dir %v", e)
  90. return false
  91. }
  92. fromfi, e := os.Stat(fromfile)
  93. if e != nil {
  94. err = fmt.Errorf("from file %v", e)
  95. return false
  96. }
  97. tofi, e := os.Stat(tofile)
  98. if e != nil && !os.IsNotExist(e) {
  99. err = fmt.Errorf("to file %v", e)
  100. return false
  101. }
  102. if tofi != nil && fromfi.Size() == tofi.Size() && fromfi.ModTime() == tofi.ModTime() {
  103. return true
  104. }
  105. fmt.Println(fromfile, " ==> ", tofile)
  106. frombs, e := os.ReadFile(fromfile)
  107. if e != nil {
  108. err = fmt.Errorf("from file %v", e)
  109. return false
  110. }
  111. e = os.WriteFile(tofile, frombs, os.ModePerm)
  112. if e != nil {
  113. err = fmt.Errorf("to file %v", e)
  114. return false
  115. }
  116. e = os.Chtimes(tofile, fromfi.ModTime(), fromfi.ModTime())
  117. if e != nil {
  118. err = fmt.Errorf("to file %v", e)
  119. return false
  120. }
  121. count++
  122. }
  123. return true
  124. })
  125. if e != nil {
  126. return e
  127. }
  128. if err != nil {
  129. return
  130. }
  131. if count > 0 || option.Debug {
  132. fmt.Println(time.Now().Format("2006-01-02 15:04:05"), count, "files copied")
  133. }
  134. return
  135. }