config.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package config
  2. import (
  3. "fmt"
  4. "regexp"
  5. "github.com/spf13/viper"
  6. )
  7. /*
  8. Config 配置结构体,包含导入和导出相关的配置
  9. */
  10. type Config struct {
  11. ImportConfig ImportConfig // 导入配置
  12. ExportConfig ExportConfig // 导出配置
  13. }
  14. /*
  15. ImportConfig 导入配置结构体
  16. 包含数据库配置、导入目录、分隔符、分页大小以及文件名匹配模式等配置
  17. */
  18. type ImportConfig struct {
  19. DBConfig DBConfig // 数据库配置
  20. DirPath string // 导入文件的目录路径
  21. Delimiter rune // 导入文件的分隔符
  22. PageSize int // 分页读取的大小
  23. FilenamePattern *regexp.Regexp // 文件名匹配的正则表达式
  24. }
  25. /*
  26. ExportConfig 导出配置结构体
  27. 包含数据库配置、导出表名、文件路径、分隔符、文件大小上限以及分页大小等配置
  28. */
  29. type ExportConfig struct {
  30. DBConfig DBConfig // 数据库配置
  31. TableName string // 要导出的表名
  32. BaseFilePath string // 导出文件的基本路径
  33. Delimiter rune // CSV 的分隔符
  34. MaxFileSize int64 // 单个文件的最大大小 (字节)
  35. PageSize int // 每次分页读取的行数
  36. }
  37. /*
  38. DBConfig 数据库配置结构体
  39. 包含数据库类型、名称、用户名、密码、IP 地址和端口等信息
  40. */
  41. type DBConfig struct {
  42. DBType string // 数据库类型 (如 MySQL, PostgreSQL 等)
  43. DBName string // 数据库名称
  44. User string // 数据库用户名
  45. Password string // 数据库密码
  46. IP string // 数据库 IP 地址
  47. Port int // 数据库端口号
  48. }
  49. func LoadConfig() (*Config, error) {
  50. viper.SetConfigName("config") // 配置文件名(不带扩展名)
  51. viper.SetConfigType("toml") // 配置文件类型
  52. viper.AddConfigPath(".") // 查找配置文件的路径
  53. if err := viper.ReadInConfig(); err != nil {
  54. return nil, fmt.Errorf("error reading config file: %v", err)
  55. }
  56. // 解析 Import 配置
  57. importPattern, err := regexp.Compile(viper.GetString("import.filename_pattern"))
  58. if err != nil {
  59. return nil, fmt.Errorf("error compiling filename pattern: %v", err)
  60. }
  61. importConfig := ImportConfig{
  62. DBConfig: DBConfig{
  63. DBType: viper.GetString("import.db_type"),
  64. DBName: viper.GetString("import.dbname"),
  65. User: viper.GetString("import.user"),
  66. Password: viper.GetString("import.password"),
  67. IP: viper.GetString("import.ip"),
  68. Port: viper.GetInt("import.port"),
  69. },
  70. DirPath: viper.GetString("import.dir_path"),
  71. Delimiter: rune(viper.GetString("import.delimiter")[0]),
  72. PageSize: viper.GetInt("import.page_size"),
  73. FilenamePattern: importPattern,
  74. }
  75. // 解析 Export 配置
  76. exportConfig := ExportConfig{
  77. DBConfig: DBConfig{
  78. DBType: viper.GetString("export.db_type"),
  79. DBName: viper.GetString("export.dbname"),
  80. User: viper.GetString("export.user"),
  81. Password: viper.GetString("export.password"),
  82. IP: viper.GetString("export.ip"),
  83. Port: viper.GetInt("export.port"),
  84. },
  85. TableName: viper.GetString("export.table_name"),
  86. BaseFilePath: viper.GetString("export.base_file_path"),
  87. Delimiter: rune(viper.GetString("export.delimiter")[0]),
  88. MaxFileSize: viper.GetInt64("export.max_file_size"),
  89. PageSize: viper.GetInt("export.page_size"),
  90. }
  91. return &Config{
  92. ImportConfig: importConfig,
  93. ExportConfig: exportConfig,
  94. }, nil
  95. }
  96. func (dbConfig *DBConfig) GetDSN() string {
  97. switch dbConfig.DBType {
  98. case "mysql":
  99. return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", dbConfig.User, dbConfig.Password, dbConfig.IP, dbConfig.Port, dbConfig.DBName)
  100. case "xugu":
  101. return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", dbConfig.User, dbConfig.Password, dbConfig.IP, dbConfig.Port, dbConfig.DBName)
  102. }
  103. return ""
  104. }