From eebb568b0c22f4c6d38a3136cdcd329ff35e792a Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 1 Feb 2021 17:29:14 +0800 Subject: [PATCH] add dotenv string flag for changing dotenv filename --- go.mod | 2 +- pkg/cmd/run.go | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 1b447980b..11d7955a7 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/gorilla/websocket v1.4.2 github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect github.com/jmoiron/sqlx v1.2.0 - github.com/joho/godotenv v1.3.0 // indirect + github.com/joho/godotenv v1.3.0 github.com/json-iterator/go v1.1.10 // indirect github.com/leekchan/accounting v0.0.0-20191218023648-17a4ce5f94d4 github.com/leodido/go-urn v1.2.1 // indirect diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index e141622ae..777404fd9 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -38,7 +38,9 @@ func init() { RunCmd.Flags().String("totp-account-name", "", "") RunCmd.Flags().Bool("enable-web-server", false, "enable web server") RunCmd.Flags().Bool("setup", false, "use setup mode") + RunCmd.Flags().Bool("no-dotenv", false, "disable built-in dotenv") + RunCmd.Flags().String("dotenv", ".env.local", "the dotenv file you want to load") RunCmd.Flags().String("since", "", "pnl since time") RootCmd.AddCommand(RunCmd) @@ -250,8 +252,13 @@ func run(cmd *cobra.Command, args []string) error { } if !disableDotEnv { - if err := godotenv.Load(".env.local", ".env") ; err != nil { - return errors.Wrap(err, "error loading .env file") + dotenvFile, err := cmd.Flags().GetString("dotenv") + if err != nil { + return err + } + + if err := godotenv.Load(dotenvFile); err != nil { + return errors.Wrap(err, "error loading dotenv file") } }