Skip to content

Instantly share code, notes, and snippets.

@eefret
Created April 7, 2022 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eefret/2bb37d2e50a3a8f90a85481ce2859b00 to your computer and use it in GitHub Desktop.
Save eefret/2bb37d2e50a3a8f90a85481ce2859b00 to your computer and use it in GitHub Desktop.
Converts app settings from azure app service to dotenv backwards and forwards
from ast import arg
import sys
import json
# should pass the path of the appsettings.json file as the first argument
# should pass the path of the .env file as the second argument
# python3 turn_app_setting_json_to_dotenv.py appsettings.json config.env
with open(sys.argv[1]) as file_obj:
data = json.load(file_obj)
dotenv_list = []
for setting in data:
dotenv_list.append("{}={}".format(setting["name"], setting["value"]))
# write dotenv_list to a file
dotenv_file = open(sys.argv[2], "w")
dotenv_file.write("\n".join(dotenv_list))
dotenv_file.close()
import sys
import json
#should pass the path of the .env file as the first argument
#should pass the path of the secrets .env file as the second argument
# python3 turn_app_setting_json_to_dotenv.py config.env secrets.env
def load_env_file(dotenv_path, override=False):
with open(dotenv_path) as file_obj:
lines = file_obj.read().splitlines() # Removes \n from lines
dotenv_vars = {}
for line in lines:
line = line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, value = line.split("=", maxsplit=1)
dotenv_vars.setdefault(key, value)
return dotenv_vars
config_path = sys.argv[1]
secret_path = sys.argv[2]
config = load_env_file(config_path)
secret = load_env_file(secret_path)
print(len(config))
print(len(secret))
all_envs = config.copy()
all_envs.update(secret)
print(len(all_envs))
setting_list = []
for k, v in all_envs.items():
setting = {}
setting["name"] = k
setting["value"] = v
setting["slotSetting"] = "false"
setting_list.append(setting)
app_settings_file = open("appsettings.json", "w")
app_settings_file.write(json.dumps(setting_list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment