mirror of
https://github.com/go-i2p/go-i2p.git
synced 2025-07-13 11:54:46 -04:00
Refactor config handling
- Replace low-level operations like file existance checks, file io with viper calls. That also allows to get rid of manual config structure initialization. - Split out UserHome() into itil/home.go to reduce code duplicacy. - Better error handling and general readbility.
This commit is contained in:
@ -4,9 +4,9 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/go-i2p/go-i2p/lib/util"
|
||||
"github.com/go-i2p/logger"
|
||||
"github.com/spf13/viper"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -17,47 +17,13 @@ var (
|
||||
const GOI2P_BASE_DIR = ".go-i2p"
|
||||
|
||||
func InitConfig() {
|
||||
defaultConfigDir := filepath.Join(os.Getenv("HOME"), GOI2P_BASE_DIR)
|
||||
defaultConfigFile := filepath.Join(defaultConfigDir, "config.yaml")
|
||||
defaultConfigDir := filepath.Join(util.UserHome(), GOI2P_BASE_DIR)
|
||||
|
||||
if CfgFile != "" {
|
||||
// Use config file from the flag
|
||||
viper.SetConfigFile(CfgFile)
|
||||
} else {
|
||||
// Create default config if it doesn't exist
|
||||
if _, err := os.Stat(defaultConfigFile); os.IsNotExist(err) {
|
||||
// Ensure directory exists
|
||||
if err := os.MkdirAll(defaultConfigDir, 0o755); err != nil {
|
||||
log.Fatalf("Could not create config directory: %s", err)
|
||||
}
|
||||
|
||||
// Create default configuration
|
||||
defaultConfig := struct {
|
||||
BaseDir string `yaml:"base_dir"`
|
||||
WorkingDir string `yaml:"working_dir"`
|
||||
NetDB NetDbConfig `yaml:"netdb"`
|
||||
Bootstrap BootstrapConfig `yaml:"bootstrap"`
|
||||
}{
|
||||
BaseDir: DefaultRouterConfig().BaseDir,
|
||||
WorkingDir: DefaultRouterConfig().WorkingDir,
|
||||
NetDB: *DefaultRouterConfig().NetDb,
|
||||
Bootstrap: *DefaultRouterConfig().Bootstrap,
|
||||
}
|
||||
|
||||
yamlData, err := yaml.Marshal(defaultConfig)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not marshal default config: %s", err)
|
||||
}
|
||||
|
||||
// Write default config file
|
||||
if err := os.WriteFile(defaultConfigFile, yamlData, 0o644); err != nil {
|
||||
log.Fatalf("Could not write default config file: %s", err)
|
||||
}
|
||||
|
||||
log.Debugf("Created default configuration at: %s", defaultConfigFile)
|
||||
}
|
||||
|
||||
// Set up viper to use the config file
|
||||
// Set up viper to use the default config file: $HOME/.go-ip/config.yaml
|
||||
viper.AddConfigPath(defaultConfigDir)
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("yaml")
|
||||
@ -67,7 +33,15 @@ func InitConfig() {
|
||||
setDefaults()
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
log.Warnf("Error reading config file: %s", err)
|
||||
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())
|
||||
}
|
||||
@ -111,3 +85,20 @@ func UpdateRouterConfig() {
|
||||
ReseedServers: reseedServers,
|
||||
}
|
||||
}
|
||||
|
||||
func createDefaultConfig(defaultConfigDir string) {
|
||||
|
||||
defaultConfigFile := filepath.Join(defaultConfigDir, "config.yaml")
|
||||
// Ensure directory exists
|
||||
if err := os.MkdirAll(defaultConfigDir, 0o755); err != nil {
|
||||
log.Fatalf("Could not create config directory: %s", err)
|
||||
}
|
||||
|
||||
// Write current config file
|
||||
if err := viper.WriteConfig(); err != nil {
|
||||
log.Fatalf("Could not write default config file: %s", err)
|
||||
}
|
||||
|
||||
log.Debugf("Created default configuration at: %s", defaultConfigFile)
|
||||
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/go-i2p/go-i2p/lib/util"
|
||||
)
|
||||
|
||||
// router.config options
|
||||
@ -17,20 +18,12 @@ type RouterConfig struct {
|
||||
Bootstrap *BootstrapConfig
|
||||
}
|
||||
|
||||
func home() string {
|
||||
h, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func defaultBase() string {
|
||||
return filepath.Join(home(), GOI2P_BASE_DIR, "base")
|
||||
return filepath.Join(util.UserHome(), GOI2P_BASE_DIR, "base")
|
||||
}
|
||||
|
||||
func defaultConfig() string {
|
||||
return filepath.Join(home(), GOI2P_BASE_DIR, "config")
|
||||
return filepath.Join(util.UserHome(), GOI2P_BASE_DIR, "config")
|
||||
}
|
||||
|
||||
// defaults for router
|
||||
|
15
lib/util/home.go
Normal file
15
lib/util/home.go
Normal file
@ -0,0 +1,15 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func UserHome() string {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to get current user's home directory. $HOME environment variable issue? %s", err)
|
||||
}
|
||||
|
||||
return homeDir
|
||||
}
|
Reference in New Issue
Block a user