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 ( import (
"os" "os"
"xmr-remote-nodes/cmd/client" "xmr-remote-nodes/cmd/client"
"xmr-remote-nodes/internal/config"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
const AppVer = "0.0.1" const AppVer = "0.0.1"
var configFile string
var Root = &cobra.Command{ var Root = &cobra.Command{
Use: "xmr-nodes", Use: "xmr-nodes",
Short: "XMR Nodes", Short: "XMR Nodes",
@ -23,5 +26,15 @@ func Execute() {
} }
func init() { func init() {
cobra.OnInitialize(initConfig)
Root.PersistentFlags().StringVarP(&configFile, "config-file", "c", "", "Default to .env")
Root.AddCommand(client.ProbeCmd) Root.AddCommand(client.ProbeCmd)
} }
func initConfig() {
if configFile != "" {
config.LoadAll(configFile)
return
}
config.LoadAll(".env")
}

View file

@ -8,9 +8,8 @@ import (
// LoadAllConfigs set various configs // LoadAllConfigs set various configs
func LoadAll(envFile string) { func LoadAll(envFile string) {
err := godotenv.Load(envFile) if err := godotenv.Load(envFile); err != nil {
if err != nil { log.Fatalf("can't load environment file. error: %v", err)
log.Fatalf("can't load .env file. error: %v", err)
} }
LoadApp() LoadApp()

View file

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