feat: Allow user to specify custom .env location
Some checks failed
Build / build (push) Has been cancelled
Test / lint (push) Has been cancelled

This feature can also be useful for running tests in CI.
This commit is contained in:
ditatompel 2024-06-18 04:23:08 +07:00
parent 6ba79cc00b
commit e9cacb478c
No known key found for this signature in database
GPG key ID: 31D3D06D77950979
3 changed files with 19 additions and 9 deletions

View file

@ -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")
}

View file

@ -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()
}

View file

@ -2,10 +2,8 @@ package main
import (
"xmr-remote-nodes/cmd"
"xmr-remote-nodes/internal/config"
)
func main() {
config.LoadAll(".env")
cmd.Execute()
}