Skip to content

Commit

Permalink
fix: .env files should overwrite env vars
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Davim <[email protected]>
  • Loading branch information
luisdavim committed Apr 26, 2022
1 parent 94a7cc6 commit 6e2824d
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 6e2824d

Please sign in to comment.