diff --git a/cmd/action.go b/cmd/action.go index 12b7287..b5628db 100644 --- a/cmd/action.go +++ b/cmd/action.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "git.sr.ht/~akilan1999/p2p-rendering-computation/client" + "git.sr.ht/~akilan1999/p2p-rendering-computation/config" "git.sr.ht/~akilan1999/p2p-rendering-computation/p2p" "git.sr.ht/~akilan1999/p2p-rendering-computation/server" "github.com/urfave/cli/v2" @@ -17,7 +18,6 @@ var CliAction = func(ctx *cli.Context) error { //Listing servers and also updates IP tables (Default 3 hops) if UpdateServerList { - err := client.UpdateIpTableListClient() if err != nil { fmt.Print(err) @@ -67,6 +67,14 @@ var CliAction = func(ctx *cli.Context) error { client.PrettyPrint(specs) } + //Sets default paths to the config file + if SetDefaultConfig { + err := config.SetDefaults() + if err != nil { + fmt.Print(err) + } + } + return nil } diff --git a/cmd/flags.go b/cmd/flags.go index c18b59e..8911f6f 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -15,6 +15,7 @@ var ( GPU bool UpdateServerList bool ServerList bool + SetDefaultConfig bool ) var AppConfigFlags = []cli.Flag{ @@ -74,4 +75,10 @@ var AppConfigFlags = []cli.Flag{ EnvVars: []string{"SPECS"}, Destination: &Specs, }, + &cli.BoolFlag{ + Name: "SetDefaultConfig", + Usage: "Sets a default configuration file", + EnvVars: []string{"SET_DEFAULT_CONFIG"}, + Destination: &SetDefaultConfig, + }, } \ No newline at end of file diff --git a/config/config.go b/config/config.go index aa1c78b..707b103 100644 --- a/config/config.go +++ b/config/config.go @@ -1,22 +1,21 @@ package config -import( +import ( "github.com/spf13/viper" + "os" ) var ( + defaultPath string defaults = map[string]interface{}{ - "IPTable": "/etc/p2p-rendering/ip_table.json", - "DockerFile": "/home/akilan/Documents/p2prendering/p2p-redering-computation/server/docker/containers/docker-ubuntu-sshd/", - "SpeedTestFile":"/etc/p2p-rendering/50.bin", - } + "IPTable": "/etc/p2p-rendering/ip_table.json", + "DockerFile": "/home/akilan/Documents/p2prendering/p2p-redering-computation/server/docker/containers/docker-ubuntu-sshd/", + "SpeedTestFile":"/etc/p2p-rendering/50.bin", + } configName = "config.json" configType = "json" configFile = "config.json" - configPaths = []string { - "/etc/p2p-rendering", - ".", - } + configPaths []string ) type Config struct { @@ -25,6 +24,51 @@ type Config struct { SpeedTestFile string } +// Exists reports whether the named file or directory exists. +func fileExists(name string) bool { + if _, err := os.Stat(name); err != nil { + if os.IsNotExist(err) { + return false + } + } + return true +} + +// 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 + } + + //Setting current directory to default path + defaultPath = curDir + "/" + + //Setting default paths for the config file + defaults["IPTable"] = defaultPath + "p2p/ip_table.json" + defaults["DockerFile"] = defaultPath + "server/docker/containers/docker-ubuntu-sshd/" + defaults["SpeedTestFile"] = defaultPath + "p2p/50.bin" + + //Paths to search for config file + configPaths = append(configPaths, defaultPath) + + if fileExists(defaultPath + "config.json") { + err := os.Remove(defaultPath + "config.json") + if err != nil { + return err + } + } + + //Calling configuration file + _, err = ConfigInit() + if err != nil { + return err + } + return nil +} + func ConfigInit()(*Config,error) { //Sets default configuration to viper for k,v := range defaults {