From 49cd37624fdbcbe6556c8bad2b24f042527dd53f Mon Sep 17 00:00:00 2001 From: Akilan Date: Mon, 24 May 2021 20:52:06 +0400 Subject: [PATCH] fixed issues regarding viper and possibility of cli --- .gitignore | 1 + Makefile | 11 ++++++++--- config/config.go | 40 +++++++++++++++++++++++----------------- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 9d95a8d..9416a36 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ p2p-rendering/ main server/docker/__pycache__ p2p-rendering-computation +p2prc config.json diff --git a/Makefile b/Makefile index dcb8290..a2ff861 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,16 @@ SHELL := /bin/bash install: - go build . - ./p2p-rendering-computation --SetDefaultConfig + go build -o p2prc + echo '# Paths for p2p rendering and computation' >> ~/.bashrc + echo 'export P2PRC=${PWD}' >> ~/.bashrc + echo 'export ${PWD}:$${PATH}' >> ~/.bashrc + source ~/.bashrc + ./p2prc --SetDefaultConfig + build: - go build . + go build -o p2prc config: ./p2p-rendering-computation --SetDefaultConfig diff --git a/config/config.go b/config/config.go index 707b103..b30a11d 100644 --- a/config/config.go +++ b/config/config.go @@ -12,7 +12,7 @@ var ( "DockerFile": "/home/akilan/Documents/p2prendering/p2p-redering-computation/server/docker/containers/docker-ubuntu-sshd/", "SpeedTestFile":"/etc/p2p-rendering/50.bin", } - configName = "config.json" + configName = "config" configType = "json" configFile = "config.json" configPaths []string @@ -37,11 +37,8 @@ func fileExists(name string) bool { // SetDefaults This function to be called only during a // make install func SetDefaults() error { - //Getting Current Directory - curDir, err := os.Getwd() - if err != nil { - return err - } + //Getting Current Directory from environment variable + curDir := os.Getenv("P2PRC") //Setting current directory to default path defaultPath = curDir + "/" @@ -62,7 +59,7 @@ func SetDefaults() error { } //Calling configuration file - _, err = ConfigInit() + _, err := ConfigInit() if err != nil { return err } @@ -70,23 +67,32 @@ func SetDefaults() error { } func ConfigInit()(*Config,error) { - //Sets default configuration to viper - for k,v := range defaults { - viper.SetDefault(k,v) - } - viper.SetConfigName(configName) - viper.SetConfigFile(configFile) - viper.SetConfigType(configType) + + curDir := os.Getenv("P2PRC") + //Setting current directory to default path + defaultPath = curDir + "/" + //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 - if err = viper.WriteConfig(); err != nil { - return nil,err - } + //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