mysql-parse
Version:
Parse a mysql connection string and return access parameters that can be fed to the mysql or mysqldump command line client.
25 lines (23 loc) • 773 B
JavaScript
import { buildMysqlParams } from './lib.js'
import * as readline from 'node:readline/promises'
import { stdin, stdout, exit, argv } from 'node:process'
// handle being called like so 'mysql-param <connection-string>'
const uri = argv[2]
if (uri) {
stdout.write(buildMysqlParams(uri))
exit(0)
}
// give the user a little time to pipe a connection string before giving up
const timeRef = setTimeout(() => {
if (!uri) {
console.error('Error. Please provide a mysql connection string.')
exit(1)
}
}, 1000)
const rl = readline.createInterface({ input: stdin, output: stdout })
// handle being called like so 'echo <connection-string> | mysql-param'
rl.on('line', (line) => {
clearTimeout(timeRef)
stdout.write(buildMysqlParams(line))
})