You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
2.9 KiB
159 lines
2.9 KiB
package ConfigUtil
|
|
|
|
import (
|
|
"ShlBackup/Utils/CommonUtil"
|
|
"fmt"
|
|
"gopkg.in/ini.v1"
|
|
)
|
|
|
|
var (
|
|
//Obs
|
|
Bucket string
|
|
Ak string
|
|
Sk string
|
|
Endpoint string
|
|
BackupPrefix string
|
|
RemainDays int32
|
|
)
|
|
|
|
func init() {
|
|
//判断是不是单元测试,如果是的话,那么配置文件的路径需要加上一个.
|
|
var configIniFile = "./Config/Config.ini"
|
|
//判断文件不是存在
|
|
if !CommonUtil.Exists(configIniFile) {
|
|
configIniFile = "." + configIniFile
|
|
}
|
|
|
|
iniParser := IniParser{}
|
|
if err := iniParser.Load(configIniFile); err != nil {
|
|
fmt.Printf("try load config file[%s] error[%s]\n", configIniFile, err.Error())
|
|
return
|
|
}
|
|
//obs配置
|
|
Ak= iniParser.GetString("obs", "ak")
|
|
Sk= iniParser.GetString("obs", "sk")
|
|
Bucket= iniParser.GetString("obs", "bucket")
|
|
Endpoint= iniParser.GetString("obs", "endpoint")
|
|
BackupPrefix= iniParser.GetString("obs", "backupPrefix")
|
|
RemainDays=iniParser.GetInt32("obs", "remainDays")
|
|
}
|
|
|
|
type IniParser struct {
|
|
confReader *ini.File // config reader
|
|
}
|
|
|
|
type IniParserError struct {
|
|
errorInfo string
|
|
}
|
|
|
|
func (e *IniParserError) Error() string { return e.errorInfo }
|
|
|
|
func (this *IniParser) Load(configFileName string) error {
|
|
conf, err := ini.Load(configFileName)
|
|
if err != nil {
|
|
this.confReader = nil
|
|
return err
|
|
}
|
|
this.confReader = conf
|
|
return nil
|
|
}
|
|
|
|
func (this *IniParser) GetString(section string, key string) string {
|
|
if this.confReader == nil {
|
|
return ""
|
|
}
|
|
|
|
s := this.confReader.Section(section)
|
|
if s == nil {
|
|
return ""
|
|
}
|
|
|
|
return s.Key(key).String()
|
|
}
|
|
|
|
func (this *IniParser) GetInt32(section string, key string) int32 {
|
|
if this.confReader == nil {
|
|
return 0
|
|
}
|
|
|
|
s := this.confReader.Section(section)
|
|
if s == nil {
|
|
return 0
|
|
}
|
|
|
|
valueInt, _ := s.Key(key).Int()
|
|
|
|
return int32(valueInt)
|
|
}
|
|
|
|
func (this *IniParser) GetUint32(section string, key string) uint32 {
|
|
if this.confReader == nil {
|
|
return 0
|
|
}
|
|
|
|
s := this.confReader.Section(section)
|
|
if s == nil {
|
|
return 0
|
|
}
|
|
|
|
valueInt, _ := s.Key(key).Uint()
|
|
|
|
return uint32(valueInt)
|
|
}
|
|
|
|
func (this *IniParser) GetInt64(section string, key string) int64 {
|
|
if this.confReader == nil {
|
|
return 0
|
|
}
|
|
|
|
s := this.confReader.Section(section)
|
|
if s == nil {
|
|
return 0
|
|
}
|
|
|
|
valueInt, _ := s.Key(key).Int64()
|
|
return valueInt
|
|
}
|
|
|
|
func (this *IniParser) GetUint64(section string, key string) uint64 {
|
|
if this.confReader == nil {
|
|
return 0
|
|
}
|
|
|
|
s := this.confReader.Section(section)
|
|
if s == nil {
|
|
return 0
|
|
}
|
|
|
|
valueInt, _ := s.Key(key).Uint64()
|
|
return valueInt
|
|
}
|
|
|
|
func (this *IniParser) GetFloat32(section string, key string) float32 {
|
|
if this.confReader == nil {
|
|
return 0
|
|
}
|
|
|
|
s := this.confReader.Section(section)
|
|
if s == nil {
|
|
return 0
|
|
}
|
|
|
|
valueFloat, _ := s.Key(key).Float64()
|
|
return float32(valueFloat)
|
|
}
|
|
|
|
func (this *IniParser) GetFloat64(section string, key string) float64 {
|
|
if this.confReader == nil {
|
|
return 0
|
|
}
|
|
|
|
s := this.confReader.Section(section)
|
|
if s == nil {
|
|
return 0
|
|
}
|
|
|
|
valueFloat, _ := s.Key(key).Float64()
|
|
return valueFloat
|
|
}
|