add dotenv string flag for changing dotenv filename

This commit is contained in:
c9s 2021-02-01 17:29:14 +08:00
parent 0a29ee99b8
commit eebb568b0c
2 changed files with 10 additions and 3 deletions

2
go.mod
View File

@ -21,7 +21,7 @@ require (
github.com/gorilla/websocket v1.4.2 github.com/gorilla/websocket v1.4.2
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
github.com/jmoiron/sqlx v1.2.0 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/json-iterator/go v1.1.10 // indirect
github.com/leekchan/accounting v0.0.0-20191218023648-17a4ce5f94d4 github.com/leekchan/accounting v0.0.0-20191218023648-17a4ce5f94d4
github.com/leodido/go-urn v1.2.1 // indirect github.com/leodido/go-urn v1.2.1 // indirect

View File

@ -38,7 +38,9 @@ func init() {
RunCmd.Flags().String("totp-account-name", "", "") RunCmd.Flags().String("totp-account-name", "", "")
RunCmd.Flags().Bool("enable-web-server", false, "enable web server") RunCmd.Flags().Bool("enable-web-server", false, "enable web server")
RunCmd.Flags().Bool("setup", false, "use setup mode") RunCmd.Flags().Bool("setup", false, "use setup mode")
RunCmd.Flags().Bool("no-dotenv", false, "disable built-in dotenv") 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") RunCmd.Flags().String("since", "", "pnl since time")
RootCmd.AddCommand(RunCmd) RootCmd.AddCommand(RunCmd)
@ -250,8 +252,13 @@ func run(cmd *cobra.Command, args []string) error {
} }
if !disableDotEnv { if !disableDotEnv {
if err := godotenv.Load(".env.local", ".env") ; err != nil { dotenvFile, err := cmd.Flags().GetString("dotenv")
return errors.Wrap(err, "error loading .env file") if err != nil {
return err
}
if err := godotenv.Load(dotenvFile); err != nil {
return errors.Wrap(err, "error loading dotenv file")
} }
} }