|
@@ -6,51 +6,125 @@ package config
|
|
|
|
|
|
import (
|
|
import (
|
|
"github.com/SongZihuan/BackendServerTemplate/src/config/configerror"
|
|
"github.com/SongZihuan/BackendServerTemplate/src/config/configerror"
|
|
|
|
+ "github.com/SongZihuan/BackendServerTemplate/src/config/configoutputer"
|
|
"github.com/SongZihuan/BackendServerTemplate/src/config/configparser"
|
|
"github.com/SongZihuan/BackendServerTemplate/src/config/configparser"
|
|
"github.com/SongZihuan/BackendServerTemplate/src/logger"
|
|
"github.com/SongZihuan/BackendServerTemplate/src/logger"
|
|
"github.com/SongZihuan/BackendServerTemplate/src/utils/filesystemutils"
|
|
"github.com/SongZihuan/BackendServerTemplate/src/utils/filesystemutils"
|
|
|
|
+ "os"
|
|
|
|
+ "path"
|
|
)
|
|
)
|
|
|
|
|
|
type configInfo struct {
|
|
type configInfo struct {
|
|
data *ConfigData
|
|
data *ConfigData
|
|
|
|
|
|
- ready bool
|
|
|
|
|
|
+ ready bool
|
|
|
|
+ hasOutput bool
|
|
|
|
+
|
|
inputFile string
|
|
inputFile string
|
|
outputFile string
|
|
outputFile string
|
|
- provider configparser.ConfigParserProvider
|
|
|
|
|
|
+
|
|
|
|
+ parserProvider configparser.ConfigParserProvider
|
|
|
|
+ outputProvider configoutputer.ConfigOutputProvider
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type ConfigOption struct {
|
|
|
|
+ ConfigFilePath string
|
|
|
|
+ OutputFilePath string
|
|
|
|
+ ParserProvider configparser.ConfigParserProvider
|
|
|
|
+ OutputProvider configoutputer.ConfigOutputProvider
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (opt *ConfigOption) setDefault() (err error) {
|
|
|
|
+ if opt.ConfigFilePath == "" {
|
|
|
|
+ wd, err := os.Getwd()
|
|
|
|
+ if err != nil {
|
|
|
|
+ logger.Errorf("can not get work directory: %s", err.Error())
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ opt.ConfigFilePath = path.Join(wd, "config.yaml")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if opt.ParserProvider == nil {
|
|
|
|
+ opt.ParserProvider, err = configparser.NewConfigParserProvider(opt.ConfigFilePath, nil)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if opt.OutputFilePath != "" && opt.OutputProvider == nil {
|
|
|
|
+ opt.OutputProvider, err = configoutputer.NewConfigOutputProvider(opt.OutputFilePath, nil)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func newConfig(inputFilePath string, outputFilePath string, provider configparser.ConfigParserProvider) (*configInfo, configerror.Error) {
|
|
|
|
- if inputFilePath == "" {
|
|
|
|
|
|
+func newConfig(opt *ConfigOption) (*configInfo, configerror.Error) {
|
|
|
|
+ if opt == nil {
|
|
|
|
+ opt = new(ConfigOption)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ err := opt.setDefault()
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, configerror.NewErrorf("new config system error: %s", err)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ configFilePath := opt.ConfigFilePath
|
|
|
|
+ parserProvider := opt.ParserProvider
|
|
|
|
+ outputFilePath := opt.OutputFilePath
|
|
|
|
+ outputProvider := opt.OutputProvider
|
|
|
|
+
|
|
|
|
+ data := new(ConfigData)
|
|
|
|
+ dataInitErr := data.init(configFilePath, parserProvider)
|
|
|
|
+ if dataInitErr != nil && dataInitErr.IsError() {
|
|
|
|
+ return nil, dataInitErr
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if configFilePath == "" {
|
|
logger.Panic("config path is empty")
|
|
logger.Panic("config path is empty")
|
|
}
|
|
}
|
|
|
|
|
|
- configFilePath, err := filesystemutils.CleanFilePathAbs(inputFilePath)
|
|
|
|
|
|
+ configFilePath, err = filesystemutils.CleanFilePathAbs(configFilePath)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, configerror.NewErrorf("change config file path (%s) to abs error: %s", configFilePath, err.Error())
|
|
return nil, configerror.NewErrorf("change config file path (%s) to abs error: %s", configFilePath, err.Error())
|
|
}
|
|
}
|
|
|
|
|
|
- if provider == nil {
|
|
|
|
- provider = configparser.NewYamlProvider(nil)
|
|
|
|
|
|
+ if parserProvider == nil {
|
|
|
|
+ parserProvider = configparser.NewYamlProvider(nil)
|
|
}
|
|
}
|
|
|
|
|
|
- if !provider.CanUTF8() {
|
|
|
|
|
|
+ if !parserProvider.CanUTF8() {
|
|
return nil, configerror.NewErrorf("config file parser provider new support UTF-8")
|
|
return nil, configerror.NewErrorf("config file parser provider new support UTF-8")
|
|
}
|
|
}
|
|
|
|
|
|
- data := new(ConfigData)
|
|
|
|
- dataInitErr := data.init(configFilePath, provider)
|
|
|
|
- if dataInitErr != nil && dataInitErr.IsError() {
|
|
|
|
- return nil, dataInitErr
|
|
|
|
|
|
+ if outputFilePath != "" {
|
|
|
|
+ outputFilePath, err = filesystemutils.CleanFilePathAbs(outputFilePath)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, configerror.NewErrorf("change config file path (%s) to abs error: %s", configFilePath, err.Error())
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if outputProvider == nil {
|
|
|
|
+ outputProvider = configoutputer.NewYamlProvider(nil)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if !outputProvider.CanUTF8() {
|
|
|
|
+ return nil, configerror.NewErrorf("config file output provider new support UTF-8")
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
return &configInfo{
|
|
return &configInfo{
|
|
data: data,
|
|
data: data,
|
|
|
|
|
|
- ready: false,
|
|
|
|
|
|
+ ready: false,
|
|
|
|
+ hasOutput: false,
|
|
|
|
+
|
|
inputFile: configFilePath,
|
|
inputFile: configFilePath,
|
|
outputFile: outputFilePath,
|
|
outputFile: outputFilePath,
|
|
- provider: provider,
|
|
|
|
|
|
+
|
|
|
|
+ parserProvider: parserProvider,
|
|
|
|
+ outputProvider: outputProvider,
|
|
}, nil
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
|
|
@@ -59,12 +133,16 @@ func (c *configInfo) init() (err configerror.Error) {
|
|
return configerror.NewErrorf("config is ready")
|
|
return configerror.NewErrorf("config is ready")
|
|
}
|
|
}
|
|
|
|
|
|
- err = c.provider.ReadFile(c.inputFile)
|
|
|
|
|
|
+ if c.parserProvider == nil {
|
|
|
|
+ return configerror.NewErrorf("config parser provider not set")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ err = c.parserProvider.ReadFile(c.inputFile)
|
|
if err != nil && err.IsError() {
|
|
if err != nil && err.IsError() {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
- err = c.provider.ParserFile(c.data) // c.Data本身就是指针
|
|
|
|
|
|
+ err = c.parserProvider.ParserFile(c.data) // c.Data本身就是指针
|
|
if err != nil && err.IsError() {
|
|
if err != nil && err.IsError() {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
@@ -74,11 +152,12 @@ func (c *configInfo) init() (err configerror.Error) {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
- if c.outputFile != "" {
|
|
|
|
- err = c.provider.WriteFile(c.outputFile, c.data)
|
|
|
|
|
|
+ if c.outputFile != "" && c.outputProvider != nil {
|
|
|
|
+ err = c.outputProvider.WriteFile(c.outputFile, c.data)
|
|
if err != nil && err.IsError() {
|
|
if err != nil && err.IsError() {
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
|
|
+ c.hasOutput = true
|
|
}
|
|
}
|
|
|
|
|
|
err = c.data.check(c)
|
|
err = c.data.check(c)
|
|
@@ -95,23 +174,6 @@ func (c *configInfo) init() (err configerror.Error) {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (c *configInfo) output(filePath string) configerror.Error {
|
|
|
|
- if !c.ready {
|
|
|
|
- return configerror.NewErrorf("config is not ready")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if filePath == "" {
|
|
|
|
- return configerror.NewErrorf("config output file path is empty")
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- err := c.provider.WriteFile(filePath, c.data)
|
|
|
|
- if err != nil && err.IsError() {
|
|
|
|
- return err
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func (c *configInfo) GetData() (*ConfigData, configerror.Error) {
|
|
func (c *configInfo) GetData() (*ConfigData, configerror.Error) {
|
|
if !c.ready {
|
|
if !c.ready {
|
|
return nil, configerror.NewErrorf("config is not ready")
|
|
return nil, configerror.NewErrorf("config is not ready")
|
|
@@ -128,6 +190,9 @@ func (c *configInfo) Data() *ConfigData {
|
|
return c.data
|
|
return c.data
|
|
}
|
|
}
|
|
|
|
|
|
-func (c *configInfo) Output(filePath string) configerror.Error {
|
|
|
|
- return c.output(filePath)
|
|
|
|
|
|
+func (c *configInfo) OutputPath() string {
|
|
|
|
+ if c.hasOutput && c.outputProvider != nil && c.outputFile != "" {
|
|
|
|
+ return c.outputFile
|
|
|
|
+ }
|
|
|
|
+ return ""
|
|
}
|
|
}
|