Merge pull request #39 from urgentquest/better-config-file-handling
Better config file handling
This commit is contained in:
@@ -4,9 +4,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/go-i2p/go-i2p/lib/util"
|
||||||
"github.com/go-i2p/logger"
|
"github.com/go-i2p/logger"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -17,48 +17,13 @@ var (
|
|||||||
const GOI2P_BASE_DIR = ".go-i2p"
|
const GOI2P_BASE_DIR = ".go-i2p"
|
||||||
|
|
||||||
func InitConfig() {
|
func InitConfig() {
|
||||||
defaultConfigDir := filepath.Join(os.Getenv("HOME"), GOI2P_BASE_DIR)
|
|
||||||
defaultConfigFile := filepath.Join(defaultConfigDir, "config.yaml")
|
|
||||||
|
|
||||||
if CfgFile != "" {
|
if CfgFile != "" {
|
||||||
// Use config file from the flag
|
// Use config file from the flag
|
||||||
viper.SetConfigFile(CfgFile)
|
viper.SetConfigFile(CfgFile)
|
||||||
} else {
|
} else {
|
||||||
// Create default config if it doesn't exist
|
// Set up viper to use the default config path $HOME/.go-ip/
|
||||||
if _, err := os.Stat(defaultConfigFile); os.IsNotExist(err) {
|
viper.AddConfigPath(BuildI2PDirPath())
|
||||||
// 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
|
|
||||||
viper.AddConfigPath(defaultConfigDir)
|
|
||||||
viper.SetConfigName("config")
|
viper.SetConfigName("config")
|
||||||
viper.SetConfigType("yaml")
|
viper.SetConfigType("yaml")
|
||||||
}
|
}
|
||||||
@@ -66,11 +31,8 @@ func InitConfig() {
|
|||||||
// Load defaults
|
// Load defaults
|
||||||
setDefaults()
|
setDefaults()
|
||||||
|
|
||||||
if err := viper.ReadInConfig(); err != nil {
|
// handle config file creating it if needed
|
||||||
log.Warnf("Error reading config file: %s", err)
|
handleConfigFile()
|
||||||
} else {
|
|
||||||
log.Debugf("Using config file: %s", viper.ConfigFileUsed())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update RouterConfigProperties
|
// Update RouterConfigProperties
|
||||||
UpdateRouterConfig()
|
UpdateRouterConfig()
|
||||||
@@ -111,3 +73,41 @@ func UpdateRouterConfig() {
|
|||||||
ReseedServers: reseedServers,
|
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)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,20 +16,12 @@ type RouterConfig struct {
|
|||||||
Bootstrap *BootstrapConfig
|
Bootstrap *BootstrapConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func home() string {
|
|
||||||
h, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return h
|
|
||||||
}
|
|
||||||
|
|
||||||
func defaultBase() string {
|
func defaultBase() string {
|
||||||
return filepath.Join(home(), GOI2P_BASE_DIR, "base")
|
return filepath.Join(BuildI2PDirPath(), "base")
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultConfig() string {
|
func defaultConfig() string {
|
||||||
return filepath.Join(home(), GOI2P_BASE_DIR, "config")
|
return filepath.Join(BuildI2PDirPath(), "config")
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaults for router
|
// 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