123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- package config
- import (
- "bufio"
- "os"
- "strings"
- "xgAutoTest/internal/global"
- )
- type ConfigInfo struct {
- DbInfo DbInfo
- CommandSql CommandSql
- }
- type DbInfo struct {
- Ip string
- Prot string
- Database string
- User string
- PassWord string
- }
- type CommandSql struct {
- Part []string
- }
- func (config *ConfigInfo) GetConfigInfo(fileLocal string) {
- configRet := config.ReadCofing(fileLocal)
- xuguRet := config.ParseConfigSql(configRet["xugu"])
- xuguMap := config.ParseGroupCase(xuguRet[0])
- dbInfo := DbInfo{
- Ip: xuguMap["ip"],
- Prot: xuguMap["port"],
- Database: xuguMap["database"],
- User: xuguMap["user"],
- PassWord: xuguMap["password"],
- }
- config.DbInfo = dbInfo
- sqlRet := config.ParseConfigSql(configRet["sql"])
- commandSql := CommandSql{
- Part: sqlRet,
- }
- config.CommandSql = commandSql
- }
- func (config *ConfigInfo) ReadCofing(fileName string) map[string]string {
- // 打开配置文件
- file, err := os.Open(fileName)
- if err != nil {
- global.Logs.Error("打开文件错误:", err)
- return nil
- }
- defer file.Close()
- ret := config.ParseConfigFile(file)
- return ret
- }
- // 读取配置文件,并解析每组,返回 键为组,值为文件原格式(包含空格换行)的map[string]string
- func (config *ConfigInfo) ParseConfigFile(file *os.File) map[string]string {
- result := make(map[string]string)
- var currentKey string
- var lines []string
- scanner := bufio.NewScanner(file)
- for scanner.Scan() {
- line := scanner.Text()
- // 处理注释部分
- // if strings.Contains(line, "#") {
- // line = strings.SplitN(line, "#", 2)[0]
- // }
- if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
- if currentKey != "" {
- result[currentKey] = strings.Join(lines, "\n")
- lines = nil
- }
- currentKey = line[1 : len(line)-1]
- } else {
- lines = append(lines, line)
- }
- }
- if currentKey != "" {
- result[currentKey] = strings.Join(lines, "\n")
- }
- if err := scanner.Err(); err != nil {
- global.Logs.Error("Error scanning file:", err)
- return nil
- }
- return result
- }
- // 将[ ]组下的单个片段中,以=号前为键,=号后为值,解析为map[string]string ,
- func (*ConfigInfo) ParseGroupCase(input string) map[string]string {
- sqlMap := make(map[string]string)
- scanner := bufio.NewScanner(strings.NewReader(input))
- for scanner.Scan() {
- line := scanner.Text()
- parts := strings.SplitN(line, "=", 2)
- if len(parts) != 2 {
- continue
- }
- key := strings.TrimSpace(parts[0])
- value := strings.TrimSpace(parts[1])
- if strings.HasPrefix(value, `"""`) {
- // Value continues on next lines until closing """
- var multiLineValue strings.Builder
- multiLineValue.WriteString(strings.TrimPrefix(value, `"""`))
- for scanner.Scan() {
- nextLine := scanner.Text()
- if strings.Contains(nextLine, `"""`) {
- multiLineValue.WriteString("\n")
- multiLineValue.WriteString(strings.TrimSuffix(nextLine, `"""`))
- break
- } else {
- multiLineValue.WriteString("\n")
- multiLineValue.WriteString(nextLine)
- }
- }
- //fmt.Printf("Key: %s, Value: %s\n", key, multiLineValue.String())
- sqlMap[key] = multiLineValue.String()
- } else {
- //fmt.Printf("Key: %s, Value: %s\n", key, value)
- sqlMap[key] = value
- }
- }
- if err := scanner.Err(); err != nil {
- global.Logs.Error("Error reading input:", err)
- os.Exit(1)
- }
- return sqlMap
- }
- // 解析[sql]组下的片段,一个用例名称到下一个用列名称前为一个片段
- func (*ConfigInfo) ParseConfigSql(input string) []string {
- lines := strings.Split(input, "\n")
- var testCases []string
- var currentTestCase strings.Builder
- for _, line := range lines {
- if strings.HasPrefix(line, "用例名称") && currentTestCase.Len() > 0 {
- testCases = append(testCases, currentTestCase.String())
- currentTestCase.Reset()
- }
- currentTestCase.WriteString(line + "\n")
- }
- if currentTestCase.Len() > 0 {
- testCases = append(testCases, currentTestCase.String())
- }
- return testCases
- }
|