Skip to content

Instantly share code, notes, and snippets.

@gnuWatchesU
Created January 11, 2023 22:48
Show Gist options
  • Save gnuWatchesU/993999d7c9be84efb495847ed37c7f30 to your computer and use it in GitHub Desktop.
Save gnuWatchesU/993999d7c9be84efb495847ed37c7f30 to your computer and use it in GitHub Desktop.
A dumb little python script to append a Sequel Ace connection
import plistlib
import sys
import subprocess
import json
import os
if len(sys.argv) != 2:
print('Provide stack as first parameter')
stack = sys.argv[1]
env = stack.split('-')[0]
env_map = {
'prod': 'Production',
'uat': 'UAT',
'dev': 'Development',
'sre': 'Development'
}
with open(os.path.expanduser('~/Library/Containers/com.sequel-ace.sequel-ace/Data/Library/Application Support/Sequel Ace/Data/Favorites.plist'), 'r+b') as fh:
favs = plistlib.load(fh)
subprocess.run(['tsh', 'db', 'login', '--db-user=super_admin', f"{stack}-rds"], check=True)
config_out = subprocess.run(['tsh', 'db', 'config', '--format', 'json'], capture_output=True, check=True)
config = json.loads(config_out.stdout)
for index, item in enumerate(favs['Favorites Root']['Children']):
if 'Children' in item.keys() and item['Name'] == env_map[env]:
break
else:
index = len(favs['Favorites Root']['Children'])
favs['Favorites Root']['Children'].append({
"Children": [],
"IsExpanded": True,
"Name": env_map[env]
})
if any(child for child in favs['Favorites Root']['Children'][index]['Children'] if child['host'] == config['host']):
print("Entry already added")
else:
favs['Favorites Root']['Children'][index]['Children'].append( {
"allowDataLocalInfile": 0,
"colorIndex": -1,
"database": "",
"enableClearTextPlugin": 0,
"host": config['host'],
"name": config['name'],
"port": str(config['port']),
"socket": "",
"sshHost": "",
"sshKeyLocation": "",
"sshKeyLocationEnabled": 0,
"sshPort": "",
"sshUser": "",
"sslCACertFileLocation": config['ca'],
"sslCACertFileLocationEnabled": 1,
"sslCertificateFileLocation": config['cert'],
"sslCertificateFileLocationEnabled": 1,
"sslKeyFileLocation": config['key'],
"sslKeyFileLocationEnabled": 1,
"timeZone": "",
"timeZoneMode": 0,
"type": 0,
"useSSL": 1,
"user": config['user']
})
favs['Favorites Root']['Children'][index]['Children'].sort(key=lambda child: child['name'])
fh.seek(0)
plistlib.dump(favs, fh, fmt=plistlib.FMT_BINARY)
fh.truncate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment