fixed issues regarding viper and possibility of cli

This commit is contained in:
2021-05-24 20:52:06 +04:00
parent f89e7ccbcb
commit 49cd37624f
3 changed files with 32 additions and 20 deletions

1
.gitignore vendored
View File

@@ -5,4 +5,5 @@ p2p-rendering/
main main
server/docker/__pycache__ server/docker/__pycache__
p2p-rendering-computation p2p-rendering-computation
p2prc
config.json config.json

View File

@@ -1,11 +1,16 @@
SHELL := /bin/bash SHELL := /bin/bash
install: install:
go build . go build -o p2prc
./p2p-rendering-computation --SetDefaultConfig echo '# Paths for p2p rendering and computation' >> ~/.bashrc
echo 'export P2PRC=${PWD}' >> ~/.bashrc
echo 'export ${PWD}:$${PATH}' >> ~/.bashrc
source ~/.bashrc
./p2prc --SetDefaultConfig
build: build:
go build . go build -o p2prc
config: config:
./p2p-rendering-computation --SetDefaultConfig ./p2p-rendering-computation --SetDefaultConfig

View File

@@ -12,7 +12,7 @@ var (
"DockerFile": "/home/akilan/Documents/p2prendering/p2p-redering-computation/server/docker/containers/docker-ubuntu-sshd/", "DockerFile": "/home/akilan/Documents/p2prendering/p2p-redering-computation/server/docker/containers/docker-ubuntu-sshd/",
"SpeedTestFile":"/etc/p2p-rendering/50.bin", "SpeedTestFile":"/etc/p2p-rendering/50.bin",
} }
configName = "config.json" configName = "config"
configType = "json" configType = "json"
configFile = "config.json" configFile = "config.json"
configPaths []string configPaths []string
@@ -37,11 +37,8 @@ func fileExists(name string) bool {
// SetDefaults This function to be called only during a // SetDefaults This function to be called only during a
// make install // make install
func SetDefaults() error { func SetDefaults() error {
//Getting Current Directory //Getting Current Directory from environment variable
curDir, err := os.Getwd() curDir := os.Getenv("P2PRC")
if err != nil {
return err
}
//Setting current directory to default path //Setting current directory to default path
defaultPath = curDir + "/" defaultPath = curDir + "/"
@@ -62,7 +59,7 @@ func SetDefaults() error {
} }
//Calling configuration file //Calling configuration file
_, err = ConfigInit() _, err := ConfigInit()
if err != nil { if err != nil {
return err return err
} }
@@ -70,23 +67,32 @@ func SetDefaults() error {
} }
func ConfigInit()(*Config,error) { func ConfigInit()(*Config,error) {
//Sets default configuration to viper
for k,v := range defaults { curDir := os.Getenv("P2PRC")
viper.SetDefault(k,v) //Setting current directory to default path
} defaultPath = curDir + "/"
viper.SetConfigName(configName) //Paths to search for config file
viper.SetConfigFile(configFile) configPaths = append(configPaths, defaultPath)
viper.SetConfigType(configType)
//Add all possible configurations paths //Add all possible configurations paths
for _,v := range configPaths { for _,v := range configPaths {
viper.AddConfigPath(v) viper.AddConfigPath(v)
} }
//Read config file //Read config file
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
// If the error thrown is config file not found // If the error thrown is config file not found
if err = viper.WriteConfig(); err != nil { //Sets default configuration to viper
return nil,err 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 // Adds configuration to the struct