diff --git a/docs/cmd_reference.md b/docs/cmd_reference.md index 28ab73e3..0bf30f42 100644 --- a/docs/cmd_reference.md +++ b/docs/cmd_reference.md @@ -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. diff --git a/internal/app/cli.go b/internal/app/cli.go index 4b92364c..4163d451 100644 --- a/internal/app/cli.go +++ b/internal/app/cli.go @@ -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") @@ -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) } }