Created
September 7, 2022 12:12
-
-
Save larsyencken/eb505fbc44d16ca09f67c6af1402874f to your computer and use it in GitHub Desktop.
MySQL: use the ~/.my.cnf file to store multiple connection settings and use this tool to switch between them
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 python | |
# | |
# mysql-profile | |
# | |
# Switch between mysql profiles. | |
# | |
from pathlib import Path | |
import configparser | |
from dataclasses import dataclass | |
import click | |
MYSQL_CONFIG = Path.home() / '.my.cnf' | |
@click.group() | |
def cli(): | |
pass | |
@cli.command() | |
def list(): | |
config = _parse_config() | |
profiles = [] | |
for section in config.sections(): | |
if section.startswith('client-'): | |
name = section.split('-', 1)[1] | |
profiles.append(name) | |
for profile in sorted(profiles): | |
print(profile) | |
@cli.command() | |
@click.argument('profile') | |
def use(profile): | |
config = _parse_config() | |
name = f'client-{profile}' | |
config['client'] = config[name] | |
with open(MYSQL_CONFIG, 'w') as f: | |
config.write(f) | |
def _parse_config() -> configparser.ConfigParser: | |
config = configparser.ConfigParser() | |
config.read(MYSQL_CONFIG) | |
return config | |
if __name__ == '__main__': | |
cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment