Skip to content

Commit

Permalink
pass logger to dotenv
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Aug 14, 2023
1 parent be4afdf commit d023222
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions cobrautil.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/go-logr/logr"
"github.com/joho/godotenv"
"github.com/jzelinskie/stringz"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -56,9 +57,13 @@ 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 {
func SyncViperDotEnvPreRunE(prefix, envfilePath string, l logr.Logger) CobraRunFunc {
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)
l.Info(
"skipped loading dotenv",
"path", envfilePath,
"err", err,
)
}
return SyncViperPreRunE(prefix)
}
Expand Down

0 comments on commit d023222

Please sign in to comment.