Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a53312b

Browse files
authoredJun 21, 2024
💻 feat: added env updater script (danny-avila#3107)
1 parent ab74685 commit a53312b

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
 

‎utils/update_env.py

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import os
2+
import re
3+
import argparse
4+
import sys
5+
6+
"""
7+
This script updates environment variables in a .env file with values from the local environment.
8+
Specifically, it looks for variables set to "GET_FROM_LOCAL_ENV" in the input .env file and replaces them
9+
with the corresponding values from the local environment variables.
10+
11+
Usage:
12+
1. Prepare an input .env file with some variables set to "GET_FROM_LOCAL_ENV".
13+
Example input .env file (input.env):
14+
# Example configuration
15+
16+
API_KEY=GET_FROM_LOCAL_ENV
17+
DB_PASSWORD=GET_FROM_LOCAL_ENV
18+
HOST=localhost
19+
PORT=3080
20+
21+
2. Set the corresponding environment variables in your local environment.
22+
Example in bash:
23+
export API_KEY=new_api_key_value
24+
export DB_PASSWORD=new_db_password_value
25+
26+
3. Run the script, specifying the input and output file paths.
27+
Example:
28+
python update_env.py input.env output.env
29+
"""
30+
31+
def read_env_file(file_path):
32+
"""Reads the .env file and returns the lines as a list."""
33+
with open(file_path, 'r') as file:
34+
lines = file.readlines()
35+
return lines
36+
37+
def write_env_file(file_path, lines):
38+
"""Writes the updated lines to the specified .env file."""
39+
with open(file_path, 'w') as file:
40+
file.writelines(lines)
41+
42+
def update_env_file_with_local_env(input_file_path, output_file_path):
43+
"""
44+
Reads the input .env file, updates the variables set to GET_FROM_LOCAL_ENV
45+
with values from the local environment, and writes the result to the output .env file.
46+
"""
47+
lines = read_env_file(input_file_path)
48+
updated_lines = []
49+
# Regex pattern to match lines ending with "GET_FROM_LOCAL_ENV"
50+
env_var_pattern = re.compile(r'^\s*([A-Z_]+)=GET_FROM_LOCAL_ENV\s*$')
51+
missing_vars = []
52+
updated_vars = []
53+
54+
for line in lines:
55+
match = env_var_pattern.match(line)
56+
if match:
57+
key = match.group(1)
58+
# Check if the environment variable is set in the local environment
59+
if key in os.environ:
60+
new_value = os.environ[key]
61+
updated_line = f'{key}={new_value}\n'
62+
updated_lines.append(updated_line)
63+
updated_vars.append(key)
64+
else:
65+
missing_vars.append(key)
66+
else:
67+
updated_lines.append(line)
68+
69+
# Print warnings and exit if any required environment variables are missing
70+
if missing_vars:
71+
for var in missing_vars:
72+
print(f"Warning: {var} set to GET_FROM_LOCAL_ENV, could not find {var}, please set {var} in your local environment and run again.")
73+
sys.exit(1)
74+
75+
# Write the updated lines to the output .env file
76+
write_env_file(output_file_path, updated_lines)
77+
78+
# Print the list of updated variables
79+
if updated_vars:
80+
print("Updated the following variables:")
81+
for var in updated_vars:
82+
print(var)
83+
84+
print(f"Processed {input_file_path} and wrote updates to {output_file_path}.")
85+
86+
if __name__ == "__main__":
87+
# Parse command-line arguments for input and output file paths
88+
parser = argparse.ArgumentParser(description='Update .env file with local environment variables.')
89+
parser.add_argument('input_file_path', type=str, help='Path to the input .env file')
90+
parser.add_argument('output_file_path', type=str, help='Path to the output .env file')
91+
args = parser.parse_args()
92+
93+
# Update the .env file with local environment variables
94+
update_env_file_with_local_env(args.input_file_path, args.output_file_path)

0 commit comments

Comments
 (0)
Please sign in to comment.