Skip to content

Commit

Permalink
Merge pull request #649 from Praqma/load_env_behaviour
Browse files Browse the repository at this point in the history
fix: .env files should overwrite env vars
  • Loading branch information
luisdavim authored Apr 27, 2022
2 parents 94a7cc6 + 6e2824d commit a65e2f4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
3 changes: 2 additions & 1 deletion docs/cmd_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ This lists available CMD options in Helmsman:
apply the dry-run (do not update) option for helm commands.

`-e value`
file(s) to load environment variables from (default .env), may be supplied more than once.
additional file(s) to load environment variables from, may be supplied more than once, it extends default .env file lookup, every next file takes precedence over previous ones in case of having the same environment variables defined.
If a `.env` file exists, it will be loaded by default, if additional env files are specified using the `-e` flag, the environment file will be loaded in order where the last file will take precedence.

`-f value`
desired state file name(s), may be supplied more than once to merge state files.
Expand Down
19 changes: 7 additions & 12 deletions internal/app/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func printUsage() {
func (c *cli) parse() {
// parsing command line flags
flag.Var(&c.files, "f", "desired state file name(s), may be supplied more than once to merge state files")
flag.Var(&c.envFiles, "e", "file(s) to load environment variables from (default .env), may be supplied more than once")
flag.Var(&c.envFiles, "e", "additional file(s) to load environment variables from, may be supplied more than once, it extends default .env file lookup, every next file takes precedence over previous ones in case of having the same environment variables defined")
flag.Var(&c.target, "target", "limit execution to specific app.")
flag.Var(&c.group, "group", "limit execution to specific group of apps.")
flag.IntVar(&c.diffContext, "diff-context", -1, "number of lines of context to show around changes in helm diff output")
Expand Down Expand Up @@ -244,20 +244,15 @@ func (c *cli) parse() {

// readState gets the desired state from files
func (c *cli) readState(s *state) error {
// read the env file
if len(c.envFiles) == 0 {
if _, err := os.Stat(".env"); err == nil {
err = godotenv.Load()
if err != nil {
return fmt.Errorf("error loading .env file: %w", err)
}
}
// read the env file if it exists
if _, err := os.Stat(".env"); err == nil {
c.envFiles = append([]string{".env"}, c.envFiles...)
}

for _, e := range c.envFiles {
err := godotenv.Load(e)
if len(c.envFiles) != 0 {
err := godotenv.Overload(c.envFiles...)
if err != nil {
return fmt.Errorf("error loading %s env file: %w", e, err)
return fmt.Errorf("error loading env file: %w", err)
}
}

Expand Down

0 comments on commit a65e2f4

Please sign in to comment.