diff --git a/cmd/cmd.go b/cmd/cmd.go index 4663c02..6c1d5e7 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -3,12 +3,15 @@ package cmd import ( "os" "xmr-remote-nodes/cmd/client" + "xmr-remote-nodes/internal/config" "github.com/spf13/cobra" ) const AppVer = "0.0.1" +var configFile string + var Root = &cobra.Command{ Use: "xmr-nodes", Short: "XMR Nodes", @@ -23,5 +26,15 @@ func Execute() { } func init() { + cobra.OnInitialize(initConfig) + Root.PersistentFlags().StringVarP(&configFile, "config-file", "c", "", "Default to .env") Root.AddCommand(client.ProbeCmd) } + +func initConfig() { + if configFile != "" { + config.LoadAll(configFile) + return + } + config.LoadAll(".env") +} diff --git a/internal/config/config.go b/internal/config/config.go index 2842ddc..d70108f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,18 +1,17 @@ package config import ( - "log" + "log" - "github.com/joho/godotenv" + "github.com/joho/godotenv" ) // LoadAllConfigs set various configs func LoadAll(envFile string) { - err := godotenv.Load(envFile) - if err != nil { - log.Fatalf("can't load .env file. error: %v", err) - } + if err := godotenv.Load(envFile); err != nil { + log.Fatalf("can't load environment file. error: %v", err) + } LoadApp() - LoadDBCfg() + LoadDBCfg() } diff --git a/main.go b/main.go index a4e9242..025f22f 100644 --- a/main.go +++ b/main.go @@ -2,10 +2,8 @@ package main import ( "xmr-remote-nodes/cmd" - "xmr-remote-nodes/internal/config" ) func main() { - config.LoadAll(".env") cmd.Execute() }