mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-18 16:54:25 +08:00
load configuration option value from file
This commit is contained in:
+35
-18
@@ -16,10 +16,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ebkWorkDirEnvName = "EBK_WORK_DIR"
|
ebkWorkDirEnvName = "EBK_WORK_DIR"
|
||||||
ebkEnvNamePrefix = "EBK"
|
ebkConfigItemValueEnvNamePrefix = "EBK"
|
||||||
defaultConfigPath = "/conf/ezbookkeeping.ini"
|
ebkConfigItemFilePathEnvNamePrefix = "EBKCFP"
|
||||||
defaultStaticRootPath = "public"
|
defaultConfigPath = "/conf/ezbookkeeping.ini"
|
||||||
|
defaultStaticRootPath = "public"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SystemMode represents running mode of system
|
// SystemMode represents running mode of system
|
||||||
@@ -1183,8 +1184,7 @@ func getNotificationConfiguration(configFile *ini.File, sectionName string, enab
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getConfigItemIsSet(configFile *ini.File, sectionName string, itemName string) bool {
|
func getConfigItemIsSet(configFile *ini.File, sectionName string, itemName string) bool {
|
||||||
environmentKey := getEnvironmentKey(sectionName, itemName)
|
environmentValue := getConfigItemValueFromEnvironment(sectionName, itemName)
|
||||||
environmentValue := os.Getenv(environmentKey)
|
|
||||||
|
|
||||||
if len(environmentValue) > 0 {
|
if len(environmentValue) > 0 {
|
||||||
return true
|
return true
|
||||||
@@ -1200,8 +1200,7 @@ func getConfigItemIsSet(configFile *ini.File, sectionName string, itemName strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getConfigItemStringValue(configFile *ini.File, sectionName string, itemName string, defaultValue ...string) string {
|
func getConfigItemStringValue(configFile *ini.File, sectionName string, itemName string, defaultValue ...string) string {
|
||||||
environmentKey := getEnvironmentKey(sectionName, itemName)
|
environmentValue := getConfigItemValueFromEnvironment(sectionName, itemName)
|
||||||
environmentValue := os.Getenv(environmentKey)
|
|
||||||
|
|
||||||
if len(environmentValue) > 0 {
|
if len(environmentValue) > 0 {
|
||||||
return environmentValue
|
return environmentValue
|
||||||
@@ -1217,8 +1216,7 @@ func getConfigItemStringValue(configFile *ini.File, sectionName string, itemName
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getConfigItemUint8Value(configFile *ini.File, sectionName string, itemName string, defaultValue uint8) uint8 {
|
func getConfigItemUint8Value(configFile *ini.File, sectionName string, itemName string, defaultValue uint8) uint8 {
|
||||||
environmentKey := getEnvironmentKey(sectionName, itemName)
|
environmentValue := getConfigItemValueFromEnvironment(sectionName, itemName)
|
||||||
environmentValue := os.Getenv(environmentKey)
|
|
||||||
|
|
||||||
if len(environmentValue) > 0 {
|
if len(environmentValue) > 0 {
|
||||||
value, err := strconv.ParseUint(environmentValue, 10, 8)
|
value, err := strconv.ParseUint(environmentValue, 10, 8)
|
||||||
@@ -1239,8 +1237,7 @@ func getConfigItemUint8Value(configFile *ini.File, sectionName string, itemName
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getConfigItemUint16Value(configFile *ini.File, sectionName string, itemName string, defaultValue uint16) uint16 {
|
func getConfigItemUint16Value(configFile *ini.File, sectionName string, itemName string, defaultValue uint16) uint16 {
|
||||||
environmentKey := getEnvironmentKey(sectionName, itemName)
|
environmentValue := getConfigItemValueFromEnvironment(sectionName, itemName)
|
||||||
environmentValue := os.Getenv(environmentKey)
|
|
||||||
|
|
||||||
if len(environmentValue) > 0 {
|
if len(environmentValue) > 0 {
|
||||||
value, err := strconv.ParseUint(environmentValue, 10, 16)
|
value, err := strconv.ParseUint(environmentValue, 10, 16)
|
||||||
@@ -1261,8 +1258,7 @@ func getConfigItemUint16Value(configFile *ini.File, sectionName string, itemName
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getConfigItemUint32Value(configFile *ini.File, sectionName string, itemName string, defaultValue uint32) uint32 {
|
func getConfigItemUint32Value(configFile *ini.File, sectionName string, itemName string, defaultValue uint32) uint32 {
|
||||||
environmentKey := getEnvironmentKey(sectionName, itemName)
|
environmentValue := getConfigItemValueFromEnvironment(sectionName, itemName)
|
||||||
environmentValue := os.Getenv(environmentKey)
|
|
||||||
|
|
||||||
if len(environmentValue) > 0 {
|
if len(environmentValue) > 0 {
|
||||||
value, err := strconv.ParseUint(environmentValue, 10, 32)
|
value, err := strconv.ParseUint(environmentValue, 10, 32)
|
||||||
@@ -1283,8 +1279,7 @@ func getConfigItemUint32Value(configFile *ini.File, sectionName string, itemName
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getConfigItemBoolValue(configFile *ini.File, sectionName string, itemName string, defaultValue bool) bool {
|
func getConfigItemBoolValue(configFile *ini.File, sectionName string, itemName string, defaultValue bool) bool {
|
||||||
environmentKey := getEnvironmentKey(sectionName, itemName)
|
environmentValue := getConfigItemValueFromEnvironment(sectionName, itemName)
|
||||||
environmentValue := os.Getenv(environmentKey)
|
|
||||||
|
|
||||||
if len(environmentValue) > 0 {
|
if len(environmentValue) > 0 {
|
||||||
value, err := strconv.ParseBool(environmentValue)
|
value, err := strconv.ParseBool(environmentValue)
|
||||||
@@ -1298,8 +1293,30 @@ func getConfigItemBoolValue(configFile *ini.File, sectionName string, itemName s
|
|||||||
return section.Key(itemName).MustBool(defaultValue)
|
return section.Key(itemName).MustBool(defaultValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getEnvironmentKey(sectionName string, itemName string) string {
|
func getConfigItemValueFromEnvironment(sectionName string, itemName string) string {
|
||||||
return fmt.Sprintf("%s_%s_%s", ebkEnvNamePrefix, strings.ToUpper(sectionName), strings.ToUpper(itemName))
|
itemFilePathEnvironmentKey := getConfigItemFilePathEnvironmentKey(sectionName, itemName)
|
||||||
|
itemFilePath := os.Getenv(itemFilePathEnvironmentKey)
|
||||||
|
|
||||||
|
if itemFilePath != "" {
|
||||||
|
content, err := os.ReadFile(itemFilePath)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
return string(content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
itemValueEnvironmentKey := getConfigItemValueEnvironmentKey(sectionName, itemName)
|
||||||
|
itemValueEnvironmentValue := os.Getenv(itemValueEnvironmentKey)
|
||||||
|
|
||||||
|
return itemValueEnvironmentValue
|
||||||
|
}
|
||||||
|
|
||||||
|
func getConfigItemFilePathEnvironmentKey(sectionName string, itemName string) string {
|
||||||
|
return fmt.Sprintf("%s_%s_%s", ebkConfigItemFilePathEnvNamePrefix, strings.ToUpper(sectionName), strings.ToUpper(itemName))
|
||||||
|
}
|
||||||
|
|
||||||
|
func getConfigItemValueEnvironmentKey(sectionName string, itemName string) string {
|
||||||
|
return fmt.Sprintf("%s_%s_%s", ebkConfigItemValueEnvNamePrefix, strings.ToUpper(sectionName), strings.ToUpper(itemName))
|
||||||
}
|
}
|
||||||
|
|
||||||
func getLogLevel(logLevelStr string) (Level, error) {
|
func getLogLevel(logLevelStr string) (Level, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user