Skip to content

Conversation

rizo
Copy link

@rizo rizo commented Jul 5, 2024

Hi! I often find the getenv ppx very useful, but one thing that is missing from it is the ability to statically fail when the environment variable is not set.

This implements a new extension getenv_strict that encountering a missing environment variable will fail with:

File "src_test/test_ppx_getenv.ml", line 5, characters 36-55:
5 |   assert_equal "42" [%getenv_strict "PPX_GETENV_CHECK_MISSING"]
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^
Error: [%getenv_strict] not set: PPX_GETENV_CHECK_MISSING

If you think this is a good addition, I'm happy to document this in the README and add a test case.

@kit-ty-kate
Copy link
Collaborator

If you think this is a good addition, I'm happy to document this in the README and add a test case.

sorry for the lack of feedback. Yes i personally think it's a good addition. If you could add these i should be able to merge and make a release

@rizo
Copy link
Author

rizo commented Dec 2, 2024

Thanks for looking into this @kit-ty-kate! I was just thinking about this PR last week and remembered it could be useful to also have a variant that uses a default value if the var lookup fails.

Maybe we could do something like this?

[%getenv "SOMETHING" {default="foo"}]
[%getenv "SOMETHING" {strict=true}]

Not sure if this is the most appropriate syntax. Let me know what you think.

@kit-ty-kate
Copy link
Collaborator

The syntax looks good. The default option sounds nice, maybe it could take either a string or None, in the latter case it would have the same behaviour as Sys.getenv_opt

@rizo
Copy link
Author

rizo commented Dec 2, 2024

Great, I'll try to get this done soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants