mirror of
https://github.com/go-i2p/go-i2p.git
synced 2025-07-18 18:44:30 -04:00
Some additional tweaks. Split out actual config handling. Add an auxiliary func to build i2p base directory path
This commit is contained in:
@@ -17,14 +17,13 @@ var (
|
||||
const GOI2P_BASE_DIR = ".go-i2p"
|
||||
|
||||
func InitConfig() {
|
||||
defaultConfigDir := filepath.Join(util.UserHome(), GOI2P_BASE_DIR)
|
||||
|
||||
if CfgFile != "" {
|
||||
// Use config file from the flag
|
||||
viper.SetConfigFile(CfgFile)
|
||||
} else {
|
||||
// Set up viper to use the default config file: $HOME/.go-ip/config.yaml
|
||||
viper.AddConfigPath(defaultConfigDir)
|
||||
// Set up viper to use the default config path $HOME/.go-ip/
|
||||
viper.AddConfigPath(BuildI2PDirPath())
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("yaml")
|
||||
}
|
||||
@@ -32,19 +31,8 @@ func InitConfig() {
|
||||
// Load defaults
|
||||
setDefaults()
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
if CfgFile != "" {
|
||||
log.Fatalf("Config file %s is not found: %s", CfgFile, err)
|
||||
} else {
|
||||
createDefaultConfig(defaultConfigDir)
|
||||
}
|
||||
} else {
|
||||
log.Fatalf("Error reading config file: %s", err)
|
||||
}
|
||||
} else {
|
||||
log.Debugf("Using config file: %s", viper.ConfigFileUsed())
|
||||
}
|
||||
// handle config file creating if needed
|
||||
handleConfigFile()
|
||||
|
||||
// Update RouterConfigProperties
|
||||
UpdateRouterConfig()
|
||||
@@ -102,3 +90,24 @@ func createDefaultConfig(defaultConfigDir string) {
|
||||
log.Debugf("Created default configuration at: %s", defaultConfigFile)
|
||||
|
||||
}
|
||||
|
||||
func handleConfigFile() {
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
if CfgFile != "" {
|
||||
log.Fatalf("Config file %s is not found: %s", CfgFile, err)
|
||||
} else {
|
||||
createDefaultConfig(BuildI2PDirPath())
|
||||
}
|
||||
} else {
|
||||
log.Fatalf("Error reading config file: %s", err)
|
||||
}
|
||||
} else {
|
||||
log.Debugf("Using config file: %s", viper.ConfigFileUsed())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func BuildI2PDirPath() string {
|
||||
return filepath.Join(util.UserHome(), GOI2P_BASE_DIR)
|
||||
}
|
||||
|
@@ -2,8 +2,6 @@ package config
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/go-i2p/go-i2p/lib/util"
|
||||
)
|
||||
|
||||
// router.config options
|
||||
@@ -19,11 +17,11 @@ type RouterConfig struct {
|
||||
}
|
||||
|
||||
func defaultBase() string {
|
||||
return filepath.Join(util.UserHome(), GOI2P_BASE_DIR, "base")
|
||||
return filepath.Join(BuildI2PDirPath(), "base")
|
||||
}
|
||||
|
||||
func defaultConfig() string {
|
||||
return filepath.Join(util.UserHome(), GOI2P_BASE_DIR, "config")
|
||||
return filepath.Join(BuildI2PDirPath(), "config")
|
||||
}
|
||||
|
||||
// defaults for router
|
||||
|
Reference in New Issue
Block a user