Created
July 11, 2024 22:00
-
-
Save dotku/6b43b6ae32ee085aefd95ae26d56ffd0 to your computer and use it in GitHub Desktop.
load azd env in python
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
import subprocess | |
import os | |
from pprint import pprint | |
def load_azd_env_values(env_name=None): | |
command = ["azd", "env", "get-values"] | |
if env_name: | |
command.extend(["--environment", env_name]) | |
try: | |
result = subprocess.run(command, capture_output=True, text=True, check=True) | |
# Print the result to debug | |
# print("Command output:", result.stdout) | |
if not result.stdout.strip(): | |
raise ValueError("The command returned no output.") | |
env_values = {} | |
for line in result.stdout.strip().split('\n'): | |
if '=' in line: | |
key, value = line.split('=', 1) | |
env_values[key] = value.strip('"') | |
else: | |
raise ValueError(f"Invalid line in output: {line}") | |
for key, value in env_values.items(): | |
os.environ[key] = value | |
return env_values | |
except subprocess.CalledProcessError as e: | |
print(f"An error occurred while running azd env get-values: {e}") | |
return None | |
# Example usage: | |
env_values = load_azd_env_values() | |
print("\nCurrent environment variables:") | |
current_env_values = {key: os.environ.get(key) for key in env_values} | |
pprint(current_env_values) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment