mirror of
https://github.com/taigrr/jety.git
synced 2026-04-02 03:19:03 -07:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
59b8a9078f
|
|||
|
550537be3b
|
|||
|
95bdc4d109
|
|||
|
64d37d936f
|
@@ -1,4 +1,4 @@
|
|||||||
package config
|
package jety
|
||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package config
|
package jety
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -143,6 +143,8 @@ func (c *ConfigManager) GetStringSlice(key string) []string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch val := v.Value.(type) {
|
switch val := v.Value.(type) {
|
||||||
|
case []string:
|
||||||
|
return val
|
||||||
case []any:
|
case []any:
|
||||||
var ret []string
|
var ret []string
|
||||||
for _, v := range val {
|
for _, v := range val {
|
||||||
@@ -200,6 +202,8 @@ func (c *ConfigManager) GetIntSlice(key string) []int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch val := v.Value.(type) {
|
switch val := v.Value.(type) {
|
||||||
|
case []int:
|
||||||
|
return val
|
||||||
case []any:
|
case []any:
|
||||||
var ret []int
|
var ret []int
|
||||||
for _, v := range val {
|
for _, v := range val {
|
||||||
|
|||||||
12
jety.go
12
jety.go
@@ -1,4 +1,4 @@
|
|||||||
package config
|
package jety
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -41,7 +41,10 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrConfigFileNotFound = errors.New("config File Not Found")
|
var (
|
||||||
|
ErrConfigFileNotFound = errors.New("config file not found")
|
||||||
|
ErrConfigFileEmpty = errors.New("config file is empty")
|
||||||
|
)
|
||||||
|
|
||||||
func NewConfigManager() *ConfigManager {
|
func NewConfigManager() *ConfigManager {
|
||||||
cm := ConfigManager{}
|
cm := ConfigManager{}
|
||||||
@@ -181,9 +184,12 @@ func (c *ConfigManager) ReadInConfig() error {
|
|||||||
|
|
||||||
func readFile(filename string, fileType configType) (map[string]any, error) {
|
func readFile(filename string, fileType configType) (map[string]any, error) {
|
||||||
fileData := make(map[string]any)
|
fileData := make(map[string]any)
|
||||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
if d, err := os.Stat(filename); os.IsNotExist(err) {
|
||||||
return nil, ErrConfigFileNotFound
|
return nil, ErrConfigFileNotFound
|
||||||
|
} else if d.Size() == 0 {
|
||||||
|
return nil, ErrConfigFileEmpty
|
||||||
}
|
}
|
||||||
|
|
||||||
switch fileType {
|
switch fileType {
|
||||||
case ConfigTypeTOML:
|
case ConfigTypeTOML:
|
||||||
_, err := toml.DecodeFile(filename, &fileData)
|
_, err := toml.DecodeFile(filename, &fileData)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package config
|
package jety
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
@@ -35,8 +35,8 @@ func (c *ConfigManager) SetDefault(key string, value any) {
|
|||||||
c.defaultConfig[lower] = ConfigMap{Key: key, Value: value}
|
c.defaultConfig[lower] = ConfigMap{Key: key, Value: value}
|
||||||
if _, ok := c.mapConfig[lower]; !ok {
|
if _, ok := c.mapConfig[lower]; !ok {
|
||||||
if envVal, ok := c.envConfig[lower]; ok {
|
if envVal, ok := c.envConfig[lower]; ok {
|
||||||
c.mapConfig[lower] = envVal
|
c.mapConfig[lower] = ConfigMap{Key: key, Value: envVal.Value}
|
||||||
c.combinedConfig[lower] = envVal
|
c.combinedConfig[lower] = ConfigMap{Key: key, Value: envVal.Value}
|
||||||
} else {
|
} else {
|
||||||
c.combinedConfig[lower] = ConfigMap{Key: key, Value: value}
|
c.combinedConfig[lower] = ConfigMap{Key: key, Value: value}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user