mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2025-01-03 17:39:48 +00:00
feat: Allow user to specify custom .env location
This feature can also be useful for running tests in CI.
This commit is contained in:
parent
6ba79cc00b
commit
e9cacb478c
3 changed files with 19 additions and 9 deletions
13
cmd/cmd.go
13
cmd/cmd.go
|
@ -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")
|
||||||
|
}
|
||||||
|
|
|
@ -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()
|
||||||
|
|
2
main.go
2
main.go
|
@ -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()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue