initial commit

This commit is contained in:
Nils Norman Haukås 2018-05-19 12:28:48 +02:00
commit 79000b2a45
8 changed files with 4192 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/node_modules/

21
LICENSE.txt Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018 Nils Norman Haukås
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

37
README.md Normal file
View File

@ -0,0 +1,37 @@
# Node Mysql parse
A command line utility for parsing a mysql connection string and generating the correct connection parameters to feed to a mysql command.
## Installation
Install by running `npm install -g mysql-parse`
## Example usage
##Basic
Running `mysql-parse --uri mysql://examplename:somepassword@examplehost:3306/dbname)` will generate string like this `-u examplename -psomepassword -h examplehost -P 3306 dbname` which you can pass to mysql or mysqldump commands.
## Using mysql
`mysql $(mysql-parse --uri <some mysql connection string>)`
## Using mysqldump
`mysqldump $(mysql-parse --uri <some mysql connection string>) > dump.sql`
## Passing in other options to mysql/mysqldump
Because the last thing out of mysql-parse is the database name (if defined in the connection string), options need to be added before mysql-parse like so:
`mysqldump --compact $(mysql-parse --uri <some mysql connection string>) > dump.sql`
## Development
1. Download this project.
2. Run tests with `npm test`.
3. Develop and submit PR. Please note: If you are considering substantial changes, please open an issue first because the feature you're thinking of might not be right for this project.
## License
Project code is licensed under the MIT license, [see license](LICENSE.txt).

20
index.js Executable file
View File

@ -0,0 +1,20 @@
#!/usr/bin/env node
'use strict'
const { buildMysqlParams } = require('./lib')
const pjson = require('./package.json')
const program = require('commander')
program
.version(pjson.version)
.description(pjson.description)
.option('--uri <uri>', 'A mysql connection uri to be parsed')
.parse(process.argv)
if (!program.uri) {
console.error('No --uri provided, please provide one.')
process.exit(1)
}
process.stdout.write(buildMysqlParams(program.uri))

28
lib.js Normal file
View File

@ -0,0 +1,28 @@
function parseUri(uri) {
const [
originalString,
scheme,
user,
password,
host,
port,
database
] = /(\w*):\/\/(\w*):(\w*)@([\w|\.|-]*):(\d*)\/(\w*)/g.exec(uri)
return { scheme, user, host, password, port, database }
}
function buildMysqlParams(uri) {
const { scheme, user, host, password, port, database } = parseUri(uri)
return [
user ? `-u ${user}` : '',
user ? `-p${password}` : '',
host ? `-h ${host}` : '',
port ? `-P ${port}` : '',
database ? `${database}` : ''
].join(' ')
}
module.exports = {
parseUri,
buildMysqlParams
}

26
lib.test.js Normal file
View File

@ -0,0 +1,26 @@
import test from 'ava'
import { parseUri, buildMysqlParams } from './lib'
test('should be able to parse fully formed url', t => {
const testUrl =
'mysql://asdf123123:zxcvasdf234789@example123.org.somewhere-here.com:3306/khe4zx5encg0ao15'
t.deepEqual(parseUri(testUrl), {
scheme: 'mysql',
user: 'asdf123123',
password: 'zxcvasdf234789',
host: 'example123.org.somewhere-here.com',
port: '3306',
database: 'khe4zx5encg0ao15'
})
})
test('should be able to prepare mysql connection parameters', t => {
const has = (str, target) => str.indexOf(target) !== -1
const testUrl =
'mysql://asdf123123:zxcvasdf234789@example123.org.somewhere-here.com:3306/khe4zx5encg0ao15'
t.is(
buildMysqlParams(testUrl),
'-u asdf123123 -pzxcvasdf234789 -h example123.org.somewhere-here.com -P 3306 khe4zx5encg0ao15',
'was not able to generate correct cmd connection string'
)
})

4038
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

21
package.json Normal file
View File

@ -0,0 +1,21 @@
{
"name": "mysql-parse",
"version": "1.0.0",
"description":
"Parse a mysql connection string and return access parameters that can be fed to the mysql or mysqldump command line client.",
"main": "index.js",
"scripts": {
"test": "ava"
},
"bin": {
"mysql-parse": "./index.js"
},
"author": "Nils Norman Haukås",
"license": "MIT",
"dependencies": {
"commander": "^2.15.1"
},
"devDependencies": {
"ava": "^0.25.0"
}
}