import json
def replace_token_versions(config_file, target_version):
"""
Replaces authentication token values in a configuration file
for isolated environments with support for older versions.
Args:
config_file (str): Path to the configuration file (JSON).
target_version (str): The version to replace tokens with.
"""
try:
with open(config_file, 'r') as f:
config = json.load(f)
# Iterate through the config and replace tokens. Adjust keys as needed.
for env in config.get('environments', []):
if env.get('name') == 'isolated': # Check for the isolated environment
for key, value in env.items():
if isinstance(value, str) and 'token' in key.lower(): # Check for token-related keys
env[key] = f"token_{target_version}"
# Write the modified config back to the file
with open(config_file, 'w') as f:
json.dump(config, f, indent=4)
print(f"Tokens in {config_file} updated to version {target_version} for isolated environment.")
except FileNotFoundError:
print(f"Error: Configuration file not found: {config_file}")
except json.JSONDecodeError:
print(f"Error: Invalid JSON format in {config_file}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == '__main__':
# Example usage:
replace_token_versions('config.json', 'v1')
Add your comment