Skip to content

Commit

Permalink
Merge pull request #48 from suttod/load-dotenv
Browse files Browse the repository at this point in the history
Load content of .env file before merging env
  • Loading branch information
jzelinskie committed Jul 14, 2023
2 parents 3593f31 + 436564f commit 8071763
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
19 changes: 17 additions & 2 deletions cobrautil.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

"github.com/joho/godotenv"
"github.com/jzelinskie/stringz"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand All @@ -21,8 +22,8 @@ func IsBuiltinCommand(cmd *cobra.Command) bool {
)
}

// SyncViperPreRunE returns a Cobra run func that synchronizes Viper environment
// flags prefixed with the provided argument.
// SyncViperPreRunE returns a CobraRunFunc that synchronizes Viper environment
// flags with the provided prefix.
//
// Thanks to Carolyn Van Slyck: https://github.com/carolynvs/stingoftheviper
func SyncViperPreRunE(prefix string) CobraRunFunc {
Expand Down Expand Up @@ -50,6 +51,20 @@ func SyncViperPreRunE(prefix string) CobraRunFunc {
}
}

// SyncViperDotEnvPreRunE returns a CobraRunFunc that loads a .dotenv file
// before synchronizing Viper environment flags with the provided prefix.
//
// 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)
}
}

// CobraRunFunc is the signature of cobra.Command RunFuncs.
type CobraRunFunc func(cmd *cobra.Command, args []string) error

Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
github.com/go-logr/logr v1.2.3
github.com/joho/godotenv v1.5.1
github.com/jzelinskie/stringz v0.0.1
github.com/mattn/go-isatty v0.0.16
github.com/rs/zerolog v1.28.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -158,6 +158,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jzelinskie/stringz v0.0.1 h1:IahR+y8ct2nyj7B6i8UtFsGFj4ex1SX27iKFYsAheLk=
Expand Down

0 comments on commit 8071763

Please sign in to comment.