upgrade.go 986 B

1234567891011121314151617181920212223242526272829303132333435
  1. package main
  2. import (
  3. "io/ioutil"
  4. "regexp"
  5. )
  6. var (
  7. _BmPattern = `(\w+)\s*(:?=)\s*bm\.Default\(\)((?:\n.*?)+)\s*\w+\.Serve\(.*?\,\s*c\..*?\);\s*(\w+)\s*!=\s*nil\s*{(\n*)(.*)\.Error\(.*\,\s*\w+\)`
  8. _BmReplace = `${1} ${2} bm.DefaultServer(c.BM)${3} ${1}.Start(); ${4} != nil {${5}${6}.Error("bm.DefaultServer error(%v)", ${4})`
  9. // _BmConfPattern = `type\sConfig\sstruct\s*{((?:.*?\n)+?)(?:(?:\s*BM\s+\*\w+\n((?:.*?\n)+?)^})|(?:(.*?)^}))`
  10. // _BmConfReplace = `type Config struct {\{1}\tBM *bm.ServerConfig\n\{2}}`
  11. // _confPath = "/../conf/conf.go"
  12. )
  13. func upBladeMaster(files []string) (err error) {
  14. for _, file := range files {
  15. var bs []byte
  16. if bs, err = ioutil.ReadFile(file); err != nil {
  17. return
  18. }
  19. var reg *regexp.Regexp
  20. if reg, err = regexp.Compile(_BmPattern); err != nil {
  21. return
  22. }
  23. if !reg.Match(bs) {
  24. continue
  25. }
  26. bs = reg.ReplaceAll(bs, []byte(_BmReplace))
  27. if err = ioutil.WriteFile(file, bs, 0644); err != nil {
  28. return
  29. }
  30. }
  31. return
  32. }