71 lines
1.3 KiB
Go
71 lines
1.3 KiB
Go
package core
|
|
|
|
import (
|
|
"errors"
|
|
"os/user"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
// Config will follow this model
|
|
// server : string
|
|
// port : int
|
|
|
|
var config_loaded bool = false
|
|
|
|
// Load config file
|
|
func LoadConfig() error {
|
|
|
|
// Get current user
|
|
currentUser, err := user.Current()
|
|
if err != nil {
|
|
// panic("LoadConfig: Failed to retrieve curent user -> " + err.Error())
|
|
return err
|
|
}
|
|
|
|
// Set default values
|
|
viper.SetDefault("server", "example.com")
|
|
viper.SetDefault("port", 5660)
|
|
|
|
// Set config paths
|
|
viper.SetConfigName("config")
|
|
viper.SetConfigType("json")
|
|
viper.AddConfigPath("/etc/cosync/")
|
|
viper.AddConfigPath("/home/" + currentUser.Username + "/.config/cosync")
|
|
|
|
// Read from config files
|
|
err = viper.ReadInConfig()
|
|
if err != nil {
|
|
panic("LoadConfig: Failed to read config file -> " + err.Error())
|
|
// return err
|
|
}
|
|
|
|
config_loaded = true
|
|
return nil
|
|
}
|
|
|
|
// Generate config folder and files
|
|
func MkConfig() error {
|
|
return errors.New("not implemented")
|
|
}
|
|
|
|
// Load sync list from sync file
|
|
func LoadSyncList() error {
|
|
return errors.New("not implemented")
|
|
}
|
|
|
|
// returns all config variables
|
|
func GetConfig() map[string]any {
|
|
|
|
if config_loaded {
|
|
return viper.AllSettings()
|
|
} else {
|
|
err := LoadConfig()
|
|
if err != nil {
|
|
panic("GetConfig: Failed to load config file -> " + err.Error())
|
|
}
|
|
|
|
return viper.AllSettings()
|
|
}
|
|
}
|