You've already forked speedtest-go
Add option to specify config file
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@ -53,7 +55,7 @@ func Load() Config {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
log.Warnf("No config file found in search paths, using default values")
|
||||
} else {
|
||||
log.Fatalf("Error reading config: %+v", err)
|
||||
log.Fatalf("Error reading config: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,6 +68,30 @@ func Load() Config {
|
||||
return conf
|
||||
}
|
||||
|
||||
func LoadFile(configFile string) Config {
|
||||
var conf Config
|
||||
|
||||
f, err := os.OpenFile(configFile, os.O_RDONLY, 0444)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open config file: %s", err)
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
if err := viper.ReadConfig(f); err != nil {
|
||||
log.Fatalf("Error reading config: %s", err)
|
||||
}
|
||||
|
||||
if err := viper.Unmarshal(&conf); err != nil {
|
||||
log.Fatalf("Error parsing config: %s", err)
|
||||
}
|
||||
|
||||
loadedConfig = &conf
|
||||
|
||||
return conf
|
||||
}
|
||||
|
||||
func LoadedConfig() *Config {
|
||||
if loadedConfig == nil {
|
||||
Load()
|
||||
|
Reference in New Issue
Block a user