Skip to content

fix: Throw error on empty env variable#499

Merged
kodiakhq[bot] merged 3 commits intomainfrom
feat/throw_error_on_empty_env
Dec 14, 2022
Merged

fix: Throw error on empty env variable#499
kodiakhq[bot] merged 3 commits intomainfrom
feat/throw_error_on_empty_env

Conversation

@yevgenypats
Copy link
Copy Markdown
Contributor

Closes #289

@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 14, 2022

⏱️ Benchmark results

Comparing with dd5f008

  • DefaultConcurrency-2 resources/s: 12,040 ⬆️ 4.99% increase vs. dd5f008
  • Glob-2 ns/op: 159.6 ⬇️ 0.19% decrease vs. dd5f008
  • TablesWithChildrenDefaultConcurrency-2 resources/s: 28,837 ⬇️ 4.14% decrease vs. dd5f008
  • BufferedScanner-2 ns/op: 9.392 ⬇️ 10.41% decrease vs. dd5f008
  • LogReader-2 ns/op: 30.78 ⬆️ 0.16% increase vs. dd5f008

envVar := envRegex.FindSubmatch(match)[1]
content, ok := os.LookupEnv(string(envVar))
if !ok {
expandErr = fmt.Errorf("env variable %s not found", envVar)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we ok with existing but empty env variables? From Lookupenv godoc:

If the variable is present in the environment the value (which may be empty) is returned and the boolean is true.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the intended behaviour and why I used lookupEnv rather then getEnv and compare with empty string.

Copy link
Copy Markdown
Member

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added one comment, but can also be done in a future PR

return fmt.Errorf("failed to expand file variable in file %s: %w", path, err)
}
data = []byte(os.ExpandEnv(string(data)))
data, err = expandEnv(data)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could just use Expand with a custom function?
https://pkg.go.dev/os#Expand

This would remove the need for both regular expressions

@erezrokah
Copy link
Copy Markdown
Member

Also maybe we can report all missing envs/files and not just the last one

@kodiakhq kodiakhq bot merged commit 4b77cf5 into main Dec 14, 2022
@kodiakhq kodiakhq bot deleted the feat/throw_error_on_empty_env branch December 14, 2022 13:14
kodiakhq bot pushed a commit that referenced this pull request Dec 14, 2022
🤖 I have created a release *beep* *boop*
---


## [1.12.3](v1.12.2...v1.12.3) (2022-12-14)


### Bug Fixes

* Throw error on empty env variable ([#499](#499)) ([4b77cf5](4b77cf5))
* Validate json strings and handle empty strings ([#497](#497)) ([dd5f008](dd5f008))

---
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Provide a better error when someone references an env variable that doesn't exist

3 participants