forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unify line endings to unix line endings
- Loading branch information
1 parent
a2fa41f
commit 23cdb2b
Showing
14 changed files
with
1,732 additions
and
1,732 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,135 @@ | ||
package config | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type ConfigSource interface { | ||
GetValue(key string) interface{} | ||
} | ||
|
||
type ConfigOption struct { | ||
Name string | ||
Description string | ||
DefaultValue interface{} | ||
LoadedValue interface{} | ||
Manager *ConfigManager | ||
} | ||
|
||
func (opt *ConfigOption) LoadValue() { | ||
newVal := opt.DefaultValue | ||
|
||
for i := len(opt.Manager.sources) - 1; i >= 0; i-- { | ||
source := opt.Manager.sources[i] | ||
|
||
v := source.GetValue(opt.Name) | ||
if v != nil { | ||
newVal = v | ||
break | ||
} | ||
} | ||
|
||
// parse ahead of time | ||
if opt.DefaultValue != nil { | ||
if _, ok := opt.DefaultValue.(int); ok { | ||
newVal = interface{}(intVal(newVal)) | ||
} else if _, ok := opt.DefaultValue.(bool); ok { | ||
newVal = interface{}(boolVal(newVal)) | ||
} | ||
} | ||
|
||
opt.LoadedValue = newVal | ||
} | ||
|
||
func (opt *ConfigOption) GetString() string { | ||
return strVal(opt.LoadedValue) | ||
} | ||
|
||
func (opt *ConfigOption) GetInt() int { | ||
return intVal(opt.LoadedValue) | ||
} | ||
|
||
func (opt *ConfigOption) GetBool() bool { | ||
return boolVal(opt.LoadedValue) | ||
} | ||
|
||
type ConfigManager struct { | ||
sources []ConfigSource | ||
options map[string]*ConfigOption | ||
} | ||
|
||
func NewConfigManager() *ConfigManager { | ||
return &ConfigManager{ | ||
options: make(map[string]*ConfigOption), | ||
} | ||
} | ||
|
||
func (c *ConfigManager) AddSource(source ConfigSource) { | ||
c.sources = append(c.sources, source) | ||
} | ||
|
||
func (c *ConfigManager) RegisterOption(name, desc string, defaultValue interface{}) *ConfigOption { | ||
opt := &ConfigOption{ | ||
Name: name, | ||
Description: desc, | ||
DefaultValue: defaultValue, | ||
Manager: c, | ||
} | ||
|
||
c.options[name] = opt | ||
return opt | ||
} | ||
|
||
func (c *ConfigManager) Load() { | ||
for _, v := range c.options { | ||
v.LoadValue() | ||
} | ||
} | ||
|
||
func strVal(i interface{}) string { | ||
switch t := i.(type) { | ||
case string: | ||
return t | ||
case int: | ||
return strconv.FormatInt(int64(t), 10) | ||
case Stringer: | ||
return t.String() | ||
} | ||
|
||
return "" | ||
} | ||
|
||
type Stringer interface { | ||
String() string | ||
} | ||
|
||
func intVal(i interface{}) int { | ||
switch t := i.(type) { | ||
case string: | ||
n, _ := strconv.ParseInt(t, 10, 64) | ||
return int(n) | ||
case int: | ||
return t | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func boolVal(i interface{}) bool { | ||
switch t := i.(type) { | ||
case string: | ||
lower := strings.ToLower(strings.TrimSpace(t)) | ||
if lower == "true" || lower == "yes" || lower == "on" || lower == "enabled" || lower == "1" { | ||
return true | ||
} | ||
|
||
return false | ||
case int: | ||
return t > 0 | ||
case bool: | ||
return t | ||
} | ||
|
||
return false | ||
} | ||
package config | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type ConfigSource interface { | ||
GetValue(key string) interface{} | ||
} | ||
|
||
type ConfigOption struct { | ||
Name string | ||
Description string | ||
DefaultValue interface{} | ||
LoadedValue interface{} | ||
Manager *ConfigManager | ||
} | ||
|
||
func (opt *ConfigOption) LoadValue() { | ||
newVal := opt.DefaultValue | ||
|
||
for i := len(opt.Manager.sources) - 1; i >= 0; i-- { | ||
source := opt.Manager.sources[i] | ||
|
||
v := source.GetValue(opt.Name) | ||
if v != nil { | ||
newVal = v | ||
break | ||
} | ||
} | ||
|
||
// parse ahead of time | ||
if opt.DefaultValue != nil { | ||
if _, ok := opt.DefaultValue.(int); ok { | ||
newVal = interface{}(intVal(newVal)) | ||
} else if _, ok := opt.DefaultValue.(bool); ok { | ||
newVal = interface{}(boolVal(newVal)) | ||
} | ||
} | ||
|
||
opt.LoadedValue = newVal | ||
} | ||
|
||
func (opt *ConfigOption) GetString() string { | ||
return strVal(opt.LoadedValue) | ||
} | ||
|
||
func (opt *ConfigOption) GetInt() int { | ||
return intVal(opt.LoadedValue) | ||
} | ||
|
||
func (opt *ConfigOption) GetBool() bool { | ||
return boolVal(opt.LoadedValue) | ||
} | ||
|
||
type ConfigManager struct { | ||
sources []ConfigSource | ||
options map[string]*ConfigOption | ||
} | ||
|
||
func NewConfigManager() *ConfigManager { | ||
return &ConfigManager{ | ||
options: make(map[string]*ConfigOption), | ||
} | ||
} | ||
|
||
func (c *ConfigManager) AddSource(source ConfigSource) { | ||
c.sources = append(c.sources, source) | ||
} | ||
|
||
func (c *ConfigManager) RegisterOption(name, desc string, defaultValue interface{}) *ConfigOption { | ||
opt := &ConfigOption{ | ||
Name: name, | ||
Description: desc, | ||
DefaultValue: defaultValue, | ||
Manager: c, | ||
} | ||
|
||
c.options[name] = opt | ||
return opt | ||
} | ||
|
||
func (c *ConfigManager) Load() { | ||
for _, v := range c.options { | ||
v.LoadValue() | ||
} | ||
} | ||
|
||
func strVal(i interface{}) string { | ||
switch t := i.(type) { | ||
case string: | ||
return t | ||
case int: | ||
return strconv.FormatInt(int64(t), 10) | ||
case Stringer: | ||
return t.String() | ||
} | ||
|
||
return "" | ||
} | ||
|
||
type Stringer interface { | ||
String() string | ||
} | ||
|
||
func intVal(i interface{}) int { | ||
switch t := i.(type) { | ||
case string: | ||
n, _ := strconv.ParseInt(t, 10, 64) | ||
return int(n) | ||
case int: | ||
return t | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func boolVal(i interface{}) bool { | ||
switch t := i.(type) { | ||
case string: | ||
lower := strings.ToLower(strings.TrimSpace(t)) | ||
if lower == "true" || lower == "yes" || lower == "on" || lower == "enabled" || lower == "1" { | ||
return true | ||
} | ||
|
||
return false | ||
case int: | ||
return t > 0 | ||
case bool: | ||
return t | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
type EnvSource struct{} | ||
|
||
func (e *EnvSource) GetValue(key string) interface{} { | ||
properKey := strings.ToUpper(key) | ||
properKey = strings.Replace(properKey, ".", "_", -1) | ||
v := os.Getenv(properKey) | ||
if v == "" { | ||
return nil | ||
} | ||
return v | ||
} | ||
package config | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
type EnvSource struct{} | ||
|
||
func (e *EnvSource) GetValue(key string) interface{} { | ||
properKey := strings.ToUpper(key) | ||
properKey = strings.Replace(properKey, ".", "_", -1) | ||
v := os.Getenv(properKey) | ||
if v == "" { | ||
return nil | ||
} | ||
return v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
package config | ||
|
||
var singleton = NewConfigManager() | ||
|
||
func AddSource(source ConfigSource) { | ||
singleton.AddSource(source) | ||
} | ||
|
||
func RegisterOption(name, desc string, defaultValue interface{}) *ConfigOption { | ||
return singleton.RegisterOption(name, desc, defaultValue) | ||
} | ||
|
||
func Load() { | ||
singleton.Load() | ||
} | ||
package config | ||
|
||
var singleton = NewConfigManager() | ||
|
||
func AddSource(source ConfigSource) { | ||
singleton.AddSource(source) | ||
} | ||
|
||
func RegisterOption(name, desc string, defaultValue interface{}) *ConfigOption { | ||
return singleton.RegisterOption(name, desc, defaultValue) | ||
} | ||
|
||
func Load() { | ||
singleton.Load() | ||
} |
Oops, something went wrong.