mirror of
https://github.com/taigrr/jety.git
synced 2026-04-02 03:19:03 -07:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
9c5923bd4e
|
|||
|
5643d4d262
|
|||
|
ee74c94359
|
|||
|
e84645ccfa
|
|||
|
b2e7785e00
|
|||
|
ef3350d66d
|
|||
|
ac7820de64
|
@@ -1,6 +1,6 @@
|
|||||||
# JETY
|
# JETY
|
||||||
|
|
||||||
JSON, ENV, YAML, TOML
|
JSON, ENV, TOML, YAML
|
||||||
|
|
||||||
This is a package for collapsing multiple configuration stores (env+json, env+yaml, env+toml) and writing them back to a centralized config.
|
This is a package for collapsing multiple configuration stores (env+json, env+yaml, env+toml) and writing them back to a centralized config.
|
||||||
|
|
||||||
|
|||||||
18
jety.go
18
jety.go
@@ -107,6 +107,10 @@ func (c *ConfigManager) collapse() {
|
|||||||
func (c *ConfigManager) WriteConfig() error {
|
func (c *ConfigManager) WriteConfig() error {
|
||||||
c.mutex.RLock()
|
c.mutex.RLock()
|
||||||
defer c.mutex.RUnlock()
|
defer c.mutex.RUnlock()
|
||||||
|
flattenedConfig := make(map[string]any)
|
||||||
|
for _, v := range c.combinedConfig {
|
||||||
|
flattenedConfig[v.Key] = v.Value
|
||||||
|
}
|
||||||
switch c.configType {
|
switch c.configType {
|
||||||
case ConfigTypeTOML:
|
case ConfigTypeTOML:
|
||||||
f, err := os.Create(c.configFileUsed)
|
f, err := os.Create(c.configFileUsed)
|
||||||
@@ -115,7 +119,7 @@ func (c *ConfigManager) WriteConfig() error {
|
|||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
enc := toml.NewEncoder(f)
|
enc := toml.NewEncoder(f)
|
||||||
err = enc.Encode(c.combinedConfig)
|
err = enc.Encode(flattenedConfig)
|
||||||
return err
|
return err
|
||||||
case ConfigTypeYAML:
|
case ConfigTypeYAML:
|
||||||
f, err := os.Create(c.configFileUsed)
|
f, err := os.Create(c.configFileUsed)
|
||||||
@@ -124,7 +128,7 @@ func (c *ConfigManager) WriteConfig() error {
|
|||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
enc := yaml.NewEncoder(f)
|
enc := yaml.NewEncoder(f)
|
||||||
err = enc.Encode(c.combinedConfig)
|
err = enc.Encode(flattenedConfig)
|
||||||
return err
|
return err
|
||||||
case ConfigTypeJSON:
|
case ConfigTypeJSON:
|
||||||
f, err := os.Create(c.configFileUsed)
|
f, err := os.Create(c.configFileUsed)
|
||||||
@@ -133,7 +137,7 @@ func (c *ConfigManager) WriteConfig() error {
|
|||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
enc := json.NewEncoder(f)
|
enc := json.NewEncoder(f)
|
||||||
return enc.Encode(c.combinedConfig)
|
return enc.Encode(flattenedConfig)
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("config type %s not supported", c.configType)
|
return fmt.Errorf("config type %s not supported", c.configType)
|
||||||
}
|
}
|
||||||
@@ -158,8 +162,6 @@ func (c *ConfigManager) SetEnvPrefix(prefix string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigManager) ReadInConfig() error {
|
func (c *ConfigManager) ReadInConfig() error {
|
||||||
c.mutex.Lock()
|
|
||||||
defer c.mutex.Unlock()
|
|
||||||
// assume config = map[string]any
|
// assume config = map[string]any
|
||||||
confFileData, err := readFile(c.configFileUsed, c.configType)
|
confFileData, err := readFile(c.configFileUsed, c.configType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -170,12 +172,18 @@ func (c *ConfigManager) ReadInConfig() error {
|
|||||||
lower := strings.ToLower(k)
|
lower := strings.ToLower(k)
|
||||||
conf[lower] = ConfigMap{Key: k, Value: v}
|
conf[lower] = ConfigMap{Key: k, Value: v}
|
||||||
}
|
}
|
||||||
|
c.mutex.Lock()
|
||||||
c.mapConfig = conf
|
c.mapConfig = conf
|
||||||
|
c.mutex.Unlock()
|
||||||
|
c.collapse()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
return nil, ErrConfigFileNotFound
|
||||||
|
}
|
||||||
switch fileType {
|
switch fileType {
|
||||||
case ConfigTypeTOML:
|
case ConfigTypeTOML:
|
||||||
_, err := toml.DecodeFile(filename, &fileData)
|
_, err := toml.DecodeFile(filename, &fileData)
|
||||||
|
|||||||
@@ -34,9 +34,13 @@ func (c *ConfigManager) SetDefault(key string, value any) {
|
|||||||
lower := strings.ToLower(key)
|
lower := strings.ToLower(key)
|
||||||
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] = envVal
|
||||||
c.combinedConfig[lower] = envVal
|
c.combinedConfig[lower] = envVal
|
||||||
|
} else {
|
||||||
|
c.combinedConfig[lower] = ConfigMap{Key: key, Value: value}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
c.combinedConfig[lower] = c.mapConfig[lower]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user