readFile.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package config
  2. import (
  3. "bufio"
  4. "os"
  5. "strings"
  6. "xgAutoTest/internal/global"
  7. )
  8. type ConfigInfo struct {
  9. DbInfo DbInfo
  10. CommandSql CommandSql
  11. }
  12. type DbInfo struct {
  13. Ip string
  14. Prot string
  15. Database string
  16. User string
  17. PassWord string
  18. }
  19. type CommandSql struct {
  20. Part []string
  21. }
  22. func (config *ConfigInfo) GetConfigInfo(fileLocal string) {
  23. configRet := config.ReadCofing(fileLocal)
  24. xuguRet := config.ParseConfigSql(configRet["xugu"])
  25. xuguMap := config.ParseGroupCase(xuguRet[0])
  26. dbInfo := DbInfo{
  27. Ip: xuguMap["ip"],
  28. Prot: xuguMap["port"],
  29. Database: xuguMap["database"],
  30. User: xuguMap["user"],
  31. PassWord: xuguMap["password"],
  32. }
  33. config.DbInfo = dbInfo
  34. sqlRet := config.ParseConfigSql(configRet["sql"])
  35. commandSql := CommandSql{
  36. Part: sqlRet,
  37. }
  38. config.CommandSql = commandSql
  39. }
  40. func (config *ConfigInfo) ReadCofing(fileName string) map[string]string {
  41. // 打开配置文件
  42. file, err := os.Open(fileName)
  43. if err != nil {
  44. global.Logs.Error("打开文件错误:", err)
  45. return nil
  46. }
  47. defer file.Close()
  48. ret := config.ParseConfigFile(file)
  49. return ret
  50. }
  51. // 读取配置文件,并解析每组,返回 键为组,值为文件原格式(包含空格换行)的map[string]string
  52. func (config *ConfigInfo) ParseConfigFile(file *os.File) map[string]string {
  53. result := make(map[string]string)
  54. var currentKey string
  55. var lines []string
  56. scanner := bufio.NewScanner(file)
  57. for scanner.Scan() {
  58. line := scanner.Text()
  59. // 处理注释部分
  60. // if strings.Contains(line, "#") {
  61. // line = strings.SplitN(line, "#", 2)[0]
  62. // }
  63. if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
  64. if currentKey != "" {
  65. result[currentKey] = strings.Join(lines, "\n")
  66. lines = nil
  67. }
  68. currentKey = line[1 : len(line)-1]
  69. } else {
  70. lines = append(lines, line)
  71. }
  72. }
  73. if currentKey != "" {
  74. result[currentKey] = strings.Join(lines, "\n")
  75. }
  76. if err := scanner.Err(); err != nil {
  77. global.Logs.Error("Error scanning file:", err)
  78. return nil
  79. }
  80. return result
  81. }
  82. // 将[ ]组下的单个片段中,以=号前为键,=号后为值,解析为map[string]string ,
  83. func (*ConfigInfo) ParseGroupCase(input string) map[string]string {
  84. sqlMap := make(map[string]string)
  85. scanner := bufio.NewScanner(strings.NewReader(input))
  86. for scanner.Scan() {
  87. line := scanner.Text()
  88. parts := strings.SplitN(line, "=", 2)
  89. if len(parts) != 2 {
  90. continue
  91. }
  92. key := strings.TrimSpace(parts[0])
  93. value := strings.TrimSpace(parts[1])
  94. if strings.HasPrefix(value, `"""`) {
  95. // Value continues on next lines until closing """
  96. var multiLineValue strings.Builder
  97. multiLineValue.WriteString(strings.TrimPrefix(value, `"""`))
  98. for scanner.Scan() {
  99. nextLine := scanner.Text()
  100. if strings.Contains(nextLine, `"""`) {
  101. multiLineValue.WriteString("\n")
  102. multiLineValue.WriteString(strings.TrimSuffix(nextLine, `"""`))
  103. break
  104. } else {
  105. multiLineValue.WriteString("\n")
  106. multiLineValue.WriteString(nextLine)
  107. }
  108. }
  109. //fmt.Printf("Key: %s, Value: %s\n", key, multiLineValue.String())
  110. sqlMap[key] = multiLineValue.String()
  111. } else {
  112. //fmt.Printf("Key: %s, Value: %s\n", key, value)
  113. sqlMap[key] = value
  114. }
  115. }
  116. if err := scanner.Err(); err != nil {
  117. global.Logs.Error("Error reading input:", err)
  118. os.Exit(1)
  119. }
  120. return sqlMap
  121. }
  122. // 解析[sql]组下的片段,一个用例名称到下一个用列名称前为一个片段
  123. func (*ConfigInfo) ParseConfigSql(input string) []string {
  124. lines := strings.Split(input, "\n")
  125. var testCases []string
  126. var currentTestCase strings.Builder
  127. for _, line := range lines {
  128. if strings.HasPrefix(line, "用例名称") && currentTestCase.Len() > 0 {
  129. testCases = append(testCases, currentTestCase.String())
  130. currentTestCase.Reset()
  131. }
  132. currentTestCase.WriteString(line + "\n")
  133. }
  134. if currentTestCase.Len() > 0 {
  135. testCases = append(testCases, currentTestCase.String())
  136. }
  137. return testCases
  138. }