Last active
September 24, 2021 13:35
-
-
Save nichollsc81/f62a6a04b268e5c4e56aff21efeacb15 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 json | |
import sys | |
#from pprint import pprint as pretty | |
# read json | |
def read_json(appSettingsFile): | |
with open(appSettingsFile) as data_file: | |
data = json.load(data_file) | |
# pretty(data) | |
return data | |
# write json | |
def write_json(data, appSettingsFile, depth=2): | |
with open(appSettingsFile, 'w') as data_file: | |
json.dump(data, data_file, indent=depth, sort_keys=False) | |
def validate_pattern(data, keyName): | |
# validate that ConnString key exists | |
if 'ConnectionStrings' not in data: | |
raise KeyError('No ConnectionStrings in target file.') | |
# validate if key name to lookup exists in ConnectionStrings dict | |
if keyName not in data['ConnectionStrings']: | |
raise KeyError('No ConnectionStrings key ' + keyName + ' in target file.') | |
def main(appSettingsFile: str, keyName: str, connectionStringValue: str): | |
# read json into variable | |
data = read_json(appSettingsFile) | |
# validate_pattern(appSettingsFile,keyName) | |
validate_pattern(data, keyName) | |
# walk key/values in data object | |
for k,v in data.items(): | |
# if doc contains ConnectionStrings update value based on key name | |
if 'ConnectionStrings' in k: | |
try: | |
v[str(keyName)] = connectionStringValue.format(k) | |
print("Connection string key " + keyName + " value updated.") | |
except: | |
print("Failed to update " + keyName + " connection string.") | |
# update appsettings | |
write_json(data, appSettingsFile) | |
# Map command line arguments to function arguments. | |
if __name__ == '__main__': | |
main(*sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment