Skip to content

Commit

Permalink
Merge pull request #49 from suttod/pass-dotenv-load-error
Browse files Browse the repository at this point in the history
Print notification and pass when failing to load .env file
  • Loading branch information
jzelinskie committed Aug 14, 2023
2 parents 8071763 + 84573bf commit be4afdf
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions cobrautil.go
Expand Up @@ -57,12 +57,10 @@ func SyncViperPreRunE(prefix string) CobraRunFunc {
// If empty, envfilePath defaults to ".env".
// The .dotenv file is loaded first before any additional Viper behavior.
func SyncViperDotEnvPreRunE(prefix string, envfilePath string) CobraRunFunc {
return func(cmd *cobra.Command, args []string) error {
if err := godotenv.Load(stringz.DefaultEmpty(envfilePath, ".env")); err != nil {
return err
}
return SyncViperPreRunE(prefix)(cmd, args)
if err := godotenv.Load(stringz.DefaultEmpty(envfilePath, ".env")); err != nil {
fmt.Printf("Info: attempt to load env vars from config file at path: %s failed due to error: %s. Continue without loading it.\n", envfilePath, err)
}
return SyncViperPreRunE(prefix)
}

// CobraRunFunc is the signature of cobra.Command RunFuncs.
Expand Down

0 comments on commit be4afdf

Please sign in to comment.