Created
August 8, 2023 07:38
-
-
Save t-book/bfeaaa3b550558797b3c16567f8a5c88 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
import sys | |
import os | |
import datetime | |
""" | |
Script to update an environment file based on a template | |
Inspired by: https://github.com/Rillke/Docker-env-file-update/blob/master/update-env.sh | |
""" | |
def extract_key_value(line: str) -> tuple[str, str]: | |
key, value = line.split('=', 1) | |
return key, value.strip() | |
def get_user_input(prompt: str, default: str) -> str: | |
user_input = input(f"{prompt} (default: '{default}'): ") | |
return user_input if user_input else default | |
def ask_user_to_update(key: str, local_file_value: str, template_value: str) -> str: | |
print(f"The value for '{key}' in the second file differs from the first file.") | |
choice = input(f"Do you want to update it with the value from the first file? ({template_value}) (y/n): ").lower() | |
if choice == "y": | |
return template_value | |
return local_file_value | |
def main(): | |
if len(sys.argv) != 4: | |
print(f"Usage: {sys.argv[0]} <template-file> <file-to-update> <new-file>") | |
sys.exit(1) | |
template_file, file_to_update, new_file = sys.argv[1], sys.argv[2], sys.argv[3] | |
if not os.path.isfile(template_file): | |
print(f"Error: Template file '{template_file}' does not exist.") | |
sys.exit(1) | |
if not os.path.isfile(file_to_update): | |
print(f"Error: File to update '{file_to_update}' does not exist.") | |
sys.exit(1) | |
vars_to_update = {} | |
vars_in_template = {} | |
with open(file_to_update) as f: | |
for line in f: | |
if '=' in line and not line.startswith('#'): | |
key, value = extract_key_value(line) | |
vars_to_update[key] = value | |
new_content = [] | |
with open(template_file) as f: | |
help_text = "" | |
for line in f: | |
if line.startswith('#') and help_text: | |
help_text += line | |
elif '=' in line: | |
key, value = extract_key_value(line) | |
vars_in_template[key] = value | |
if key in vars_to_update: | |
if vars_to_update[key] != vars_in_template[key]: | |
updated_value = ask_user_to_update(key, vars_to_update[key], vars_in_template[key]) | |
new_content.extend(["\n", f"# Updated by {os.environ['USER']} on {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n", f"{key}={updated_value}\n"]) | |
else: | |
new_content.append(f"{key}={vars_to_update[key]}\n") | |
del vars_to_update[key] | |
else: | |
new_value = get_user_input(f"Enter a new value for {key}", value) | |
new_content.extend(["\n", f"# Added by {os.environ['USER']} on {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n", f"{key}={new_value}\n"]) | |
print(f"Added new variable {key} with value: {new_value}") | |
help_text = "" | |
else: | |
new_content.append(line) | |
print("Checking for obsolete variables ...") | |
for key, value in vars_to_update.items(): | |
if key not in vars_in_template: | |
print(f"Warning: Variable {key} with value {value} might be obsolete.") | |
with open(new_file, "w") as f: | |
f.writelines(new_content) | |
print(f"Done updating. Changes written to '{new_file}'.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
env_master.txt
env_4.0.txt
run with
python fix_env_file.py env_master.txt env_4.0.txt new.txt