Created
September 22, 2016 05:51
-
-
Save meeuw/f55485fd3d35d2102d31d3e3513e7489 to your computer and use it in GitHub Desktop.
mycli wrapper for tunneled connections and bash auto completion
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
#!/usr/bin/python3 | |
import os | |
import sys | |
import argcomplete | |
import argparse | |
import glob | |
import os.path | |
import sys | |
import subprocess | |
parser = argparse.ArgumentParser() | |
def mysqlconfigs(prefix, parsed_args, **kwargs): | |
result = [] | |
for config in glob.glob('/home/user/mysql/*'): | |
config = os.path.basename(config) | |
if config.startswith(prefix): | |
result.append(config) | |
return result | |
def mysqldatabases(prefix, parsed_args, **kwargs): | |
databases = subprocess.check_output(["mysqlc", parsed_args.server, "-NBe", "show databases"], stderr=subprocess.DEVNULL).decode('utf8').split('\n') | |
return (database for database in databases if database.startswith(prefix)) | |
parser.add_argument("server", help="server").completer = mysqlconfigs | |
parser.add_argument("database", help="database").completer = mysqldatabases | |
argcomplete.autocomplete(parser) | |
args, unknownargs = parser.parse_known_args() | |
sshtunnel = None | |
with open('/home/user/mysql/'+args.server) as f: | |
s = f.read() | |
if len(s) > 0 and s[0] == '#': | |
sshtunnel = s[1:].split('\n')[0] | |
if sshtunnel and os.system('ssh -O check %s-controlmaster' % sshtunnel) != 0: | |
os.system('ssh -f %s-controlmaster sleep 60' % sshtunnel) | |
os.execvp('mycli', ['mycli', '--defaults-file', '/home/user/mysql/'+args.server, args.database, '--prompt', args.server +' \d > ']+unknownargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment