Skip to content

Instantly share code, notes, and snippets.

@franciscoafonsoo
Last active February 13, 2020 13:34
Show Gist options
  • Save franciscoafonsoo/37230bc6046d65ea03d8fd9529f75e47 to your computer and use it in GitHub Desktop.
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.
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