config abstraction changes added

This commit is contained in:
2023-03-22 09:43:40 +00:00
parent 88dddd0230
commit 69c0aa5cb1
14 changed files with 2015 additions and 1924 deletions

View File

@@ -1,7 +1,8 @@
package config
import (
"github.com/spf13/viper"
"encoding/json"
"io/ioutil"
"os"
)
@@ -28,6 +29,7 @@ type Config struct {
GroupTrackContainersPath string
FRPServerPort string
BehindNAT string
CustomConfig interface{}
//NetworkInterface string
//NetworkInterfaceIPV6Index int
}
@@ -41,6 +43,9 @@ func GetPathP2PRC(Envname string) (string, error) {
}
}
curDir := os.Getenv(defaultEnvName)
if curDir == "" {
return curDir, nil
}
return curDir + "/", nil
}
@@ -59,45 +64,93 @@ func GetEnvName() string {
}
// ConfigInit Pass environment name as an optional parameter
func ConfigInit(defaultsParameter map[string]interface{}, envNameOptional ...string) (*Config, error) {
func ConfigInit(defaultsParameter map[string]interface{}, CustomConfig interface{}, envNameOptional ...string) (*Config, error) {
if len(envNameOptional) > 0 {
defaultEnvName = envNameOptional[0]
}
//
////Setting current directory to default path
//defaultPath, err := GetPathP2PRC(defaultEnvName)
//if err != nil {
// return nil, err
//}
////Paths to search for config file
//configPaths = append(configPaths, defaultPath)
//
////Add all possible configurations paths
//for _, v := range configPaths {
// viper.AddConfigPath(v)
//}
//
////Read config file
//if err := viper.ReadInConfig(); err != nil {
// // If the error thrown is config file not found
// //Sets default configuration to viper
// for k, v := range defaults {
// viper.SetDefault(k, v)
// }
// viper.SetConfigName(configName)
// viper.SetConfigFile(configFile)
// viper.SetConfigType(configType)
//
// if err = viper.WriteConfig(); err != nil {
// return nil, err
// }
//}
//
//// Adds configuration to the struct
//var config Config
//if err := viper.Unmarshal(&config); err != nil {
// return nil, err
//}
//
//return &config, nil
//Setting current directory to default path
defaultPath, err := GetPathP2PRC(defaultEnvName)
if err != nil {
return nil, err
}
//Paths to search for config file
configPaths = append(configPaths, defaultPath)
//Add all possible configurations paths
for _, v := range configPaths {
viper.AddConfigPath(v)
// Open our jsonFile
jsonFile, err := os.Open(defaultPath + configFile)
// if we os.Open returns an error then handle it
if err != nil {
return nil, err
}
//Read config file
if err := viper.ReadInConfig(); err != nil {
// If the error thrown is config file not found
//Sets default configuration to viper
for k, v := range defaults {
viper.SetDefault(k, v)
}
viper.SetConfigName(configName)
viper.SetConfigFile(configFile)
viper.SetConfigType(configType)
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
if err = viper.WriteConfig(); err != nil {
byteValue, _ := ioutil.ReadAll(jsonFile)
var config Config
json.Unmarshal(byteValue, &config)
if CustomConfig != nil {
// Convert Custom Config to byte
customConfigByte, err := json.Marshal(config.CustomConfig)
if err != nil {
return nil, err
}
}
// Adds configuration to the struct
var config Config
if err := viper.Unmarshal(&config); err != nil {
return nil, err
// Again map the byte to the CustomConfig interface
json.Unmarshal(customConfigByte, &CustomConfig)
}
return &config, nil
}
func (c *Config) WriteConfig() error {
//Getting Current Directory from environment variable
//curDir := os.Getenv("REMOTEGAMING")
defaultPath, err := GetPathP2PRC(defaultEnvName)
if err != nil {
return err
}
file, _ := json.MarshalIndent(c, "", " ")
_ = ioutil.WriteFile(defaultPath+"config.json", file, 0644)
return nil
}

View File

@@ -37,7 +37,7 @@ func GetCurrentPath() (string, error) {
// SetDefaults This function to be called only during a
// make install
func SetDefaults(envName string, forceDefault bool, ConfigUpdate ...*config.Config) (*config.Config, error) {
func SetDefaults(envName string, forceDefault bool, CustomConfig interface{}, NoBoilerPlate bool, ConfigUpdate ...*config.Config) (*config.Config, error) {
//Setting current directory to default path
defaultPath, err := GetCurrentPath()
if err != nil {
@@ -68,38 +68,31 @@ func SetDefaults(envName string, forceDefault bool, ConfigUpdate ...*config.Conf
// return err
//}
var Defaults config.Config
if len(ConfigUpdate) == 0 {
//Setting default paths for the config file
defaults["IPTable"] = defaultPath + "p2p/iptable/ip_table.json"
defaults["DefaultDockerFile"] = defaultPath + "server/docker/containers/docker-ubuntu-sshd/"
defaults["DockerContainers"] = defaultPath + "server/docker/containers/"
defaults["SpeedTestFile"] = defaultPath + "p2p/50.bin"
defaults["IPV6Address"] = ""
defaults["PluginPath"] = defaultPath + "plugin/deploy"
defaults["TrackContainersPath"] = defaultPath + "client/trackcontainers/trackcontainers.json"
defaults["GroupTrackContainersPath"] = defaultPath + "client/trackcontainers/grouptrackcontainers.json"
defaults["ServerPort"] = "8088"
defaults["FRPServerPort"] = "0"
defaults["BehindNAT"] = "True"
Defaults.IPTable = defaultPath + "p2p/iptable/ip_table.json"
Defaults.DefaultDockerFile = defaultPath + "server/docker/containers/docker-ubuntu-sshd/"
Defaults.DockerContainers = defaultPath + "server/docker/containers/"
Defaults.SpeedTestFile = defaultPath + "p2p/50.bin"
Defaults.IPV6Address = ""
Defaults.PluginPath = defaultPath + "plugin/deploy"
Defaults.TrackContainersPath = defaultPath + "client/trackcontainers/trackcontainers.json"
Defaults.GroupTrackContainersPath = defaultPath + "client/trackcontainers/grouptrackcontainers.json"
Defaults.ServerPort = "8088"
Defaults.FRPServerPort = "True"
Defaults.CustomConfig = CustomConfig
Defaults.BehindNAT = "True"
// Random name generator
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
defaults["MachineName"] = hostname
Defaults.MachineName = hostname
} else {
defaults["IPTable"] = ConfigUpdate[0].IPTable
defaults["DefaultDockerFile"] = ConfigUpdate[0].DefaultDockerFile
defaults["DockerContainers"] = ConfigUpdate[0].DockerContainers
defaults["SpeedTestFile"] = ConfigUpdate[0].SpeedTestFile
defaults["IPV6Address"] = ConfigUpdate[0].IPV6Address
defaults["PluginPath"] = ConfigUpdate[0].PluginPath
defaults["TrackContainersPath"] = ConfigUpdate[0].TrackContainersPath
defaults["GroupTrackContainersPath"] = ConfigUpdate[0].GroupTrackContainersPath
defaults["ServerPort"] = ConfigUpdate[0].ServerPort
defaults["FRPServerPort"] = ConfigUpdate[0].FRPServerPort
defaults["BehindNAT"] = ConfigUpdate[0].BehindNAT
Defaults = *ConfigUpdate[0]
}
//defaults["NetworkInterface"] = "wlp0s20f3"
@@ -108,22 +101,31 @@ func SetDefaults(envName string, forceDefault bool, ConfigUpdate ...*config.Conf
//Paths to search for config file
configPaths = append(configPaths, defaultPath)
if fileExists(defaultPath + "config.json") && forceDefault {
if fileExists(defaultPath+"config.json") && forceDefault {
err := os.Remove(defaultPath + "config.json")
if err != nil {
return nil, err
}
}
//Calling configuration file
Config, err := config.ConfigInit(defaults, envName)
// write defaults to the config file
err = Defaults.WriteConfig()
if err != nil {
return nil, err
}
err = GenerateFiles()
//Calling configuration file
Config, err := config.ConfigInit(defaults, nil, envName)
if err != nil {
return nil, err
}
if !NoBoilerPlate {
err = GenerateFiles()
if err != nil {
return nil, err
}
}
return Config, nil
}

View File

@@ -0,0 +1,35 @@
package generate
import (
"fmt"
"github.com/Akilan1999/p2p-rendering-computation/config"
"testing"
)
type CustomConfig struct {
Test string
}
// Test case to generate defaults with custom data-structure
func TestSetDefaults(t *testing.T) {
setDefaults, err := SetDefaults("", true, &CustomConfig{Test: "lol"}, true)
if err != nil {
fmt.Println(err)
t.Fail()
return
}
fmt.Println(setDefaults)
var c CustomConfig
_, err = config.ConfigInit(nil, &c)
if err != nil {
fmt.Println(err)
t.Fail()
return
}
fmt.Println(c)
}