file.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package pkg
  2. import (
  3. "bufio"
  4. "bytes"
  5. "context"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "log"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "time"
  14. )
  15. func PathCreate(dir string) error {
  16. return os.MkdirAll(dir, os.ModePerm)
  17. }
  18. // PathExist 判断目录是否存在
  19. func PathExist(addr string) bool {
  20. s, err := os.Stat(addr)
  21. if err != nil {
  22. log.Println(err)
  23. return false
  24. }
  25. return s.IsDir()
  26. }
  27. func FileCreate(content bytes.Buffer, name string) {
  28. file, err := os.Create(name)
  29. if err != nil {
  30. log.Println(err)
  31. }
  32. _, err = file.WriteString(content.String())
  33. if err != nil {
  34. log.Println(err)
  35. }
  36. file.Close()
  37. }
  38. type ReplaceHelper struct {
  39. Root string //路径
  40. OldText string //需要替换的文本
  41. NewText string //新的文本
  42. }
  43. func (h *ReplaceHelper) DoWrok() error {
  44. return filepath.Walk(h.Root, h.walkCallback)
  45. }
  46. func (h ReplaceHelper) walkCallback(path string, f os.FileInfo, err error) error {
  47. if err != nil {
  48. return err
  49. }
  50. if f == nil {
  51. return nil
  52. }
  53. if f.IsDir() {
  54. log.Println("DIR:", path)
  55. return nil
  56. }
  57. //文件类型需要进行过滤
  58. buf, err := ioutil.ReadFile(path)
  59. if err != nil {
  60. //err
  61. return err
  62. }
  63. content := string(buf)
  64. log.Printf("h.OldText: %s \n", h.OldText)
  65. log.Printf("h.NewText: %s \n", h.NewText)
  66. //替换
  67. newContent := strings.Replace(content, h.OldText, h.NewText, -1)
  68. //重新写入
  69. ioutil.WriteFile(path, []byte(newContent), 0)
  70. return err
  71. }
  72. func FileMonitoringById(ctx context.Context, filePth string, id string, group string, hookfn func(context.Context, string, string, []byte)) {
  73. f, err := os.Open(filePth)
  74. if err != nil {
  75. log.Fatalln(err)
  76. }
  77. defer f.Close()
  78. rd := bufio.NewReader(f)
  79. f.Seek(0, 2)
  80. for {
  81. if ctx.Err() != nil {
  82. break
  83. }
  84. line, err := rd.ReadBytes('\n')
  85. // 如果是文件末尾不返回
  86. if err == io.EOF {
  87. time.Sleep(500 * time.Millisecond)
  88. continue
  89. } else if err != nil {
  90. log.Fatalln(err)
  91. }
  92. go hookfn(ctx, id, group, line)
  93. }
  94. }
  95. // 获取文件大小
  96. func GetFileSize(filename string) int64 {
  97. var result int64
  98. filepath.Walk(filename, func(path string, f os.FileInfo, err error) error {
  99. result = f.Size()
  100. return nil
  101. })
  102. return result
  103. }
  104. //获取当前路径,比如:E:/abc/data/test
  105. func GetCurrentPath() string {
  106. dir, err := os.Getwd()
  107. if err != nil {
  108. fmt.Println(err)
  109. }
  110. return strings.Replace(dir, "\\", "/", -1)
  111. }