Last active
February 13, 2020 13:34
-
-
Save franciscoafonsoo/37230bc6046d65ea03d8fd9529f75e47 to your computer and use it in GitHub Desktop.
Matches all environment variables with a certain prefix. Returns a dict with the key of the variable without the prefix. Use case: filtering certain variables for using in the a CI system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def strip_var_envs(prefix): | |
""" | |
Matches all environment variables with a certain prefix. | |
Returns a dict with the key of the variable without the prefix. | |
Example: | |
env vars: | |
- CI_API_KEY=key | |
- CI_URL=https://httpbin.org | |
- BASE_URL=https://example.com | |
prefix: | |
- CI_ | |
result: | |
- {'API_KEY': "key", 'URL': "https://httpbin.org"} | |
""" | |
return { | |
k.lstrip(prefix): os.getenv(k) | |
for k in dict(os.environ).keys() | |
if k.startswith(prefix) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment