From ef3c5e06075a414aa8375ee251c788dd55ade5b1 Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 6 Nov 2016 22:29:48 +0800 Subject: [PATCH] Try add postbird command line util --- .postbird | 3 +++ bin/postbird | 17 +++++++++++-- index.js | 7 +++++- lib/cli_util.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 .postbird create mode 100644 lib/cli_util.js diff --git a/.postbird b/.postbird new file mode 100644 index 00000000..0cdc5a9c --- /dev/null +++ b/.postbird @@ -0,0 +1,3 @@ +{ + "default": "postgres://localhost/test_data" +} \ No newline at end of file diff --git a/bin/postbird b/bin/postbird index 9aff53d0..a58f495c 100755 --- a/bin/postbird +++ b/bin/postbird @@ -1,9 +1,22 @@ #!/bin/sh BASEDIR=$(dirname $0) +WORKING_DIR=$(pwd) + +if [ "$1" == "--help" ]; then + echo "Usage:" + echo "postbird localhost/dbname -> open postgres://$USER@localhost/dbname" + echo "postbird . -> try to find .postbird in PWD" + echo "postbird ../pathto/project -> try to find .postbird in ../pathto/project" + exit 0 +fi + +# postbird localhost/dbname => open postgres://localhost/dbname#WORKING_DIR=$WORKING_DIR +# postbird => postgres://.#WORKING_DIR=$WORKING_DIR +# postbird ../pathto/project => postgres://../pathto/project#WORKING_DIR=$WORKING_DIR if [ $1 ]; then - open -a Postgres.app $1 + open postgres://$1\#WORKING_DIR=$WORKING_DIR else - open -a Postgres.app . + open postgres://.\#WORKING_DIR=$WORKING_DIR fi diff --git a/index.js b/index.js index d60558d6..1486d11e 100644 --- a/index.js +++ b/index.js @@ -71,6 +71,8 @@ require('./app/controllers/updates_controller'); require('./app/heroku_client'); require('./app/history_window'); +var CliUtil = require('./lib/cli_util'); + global.$u = window.$u = window.Zepto || window.jQuery; @@ -101,7 +103,10 @@ electron.ipcRenderer.on('open-file', function(event, message) { electron.ipcRenderer.on('open-url', function(event, url) { console.log('open-url', event, url); - App.openConnection(url); + CliUtil.resolveArg(url, (resultUrl) => { + console.log('CliUtil.resolveArg', resultUrl); + App.openConnection(resultUrl); + }); }); $(window).on('window-ready', (event) => { diff --git a/lib/cli_util.js b/lib/cli_util.js new file mode 100644 index 00000000..c566f157 --- /dev/null +++ b/lib/cli_util.js @@ -0,0 +1,67 @@ +var path = require('path'); +var fs = require('fs'); + +var CliUtil = { + resolveArg(url, callback) { + var parts = url.split('#WORKING_DIR='); + var workingDir = null; + if (parts.length == 2) { + url = parts[0]; + workingDir = parts[1]; + } + + console.log('CliUtil.resolveArg', url, workingDir); + + if (workingDir) { + this.checkFiles(url, workingDir, callback); + } else { + callback(url); + } + + }, + + checkFiles(url, workingDir, callback) { + var joinedPath = path.join(workingDir, url.replace('postgres://', '')); + console.log("ckecking if exist", joinedPath); + fs.access(joinedPath, 'r', (err, fd) => { + if (err) { + console.log(joinedPath, 'not exists'); + callback(url); + } else { + this.tryFindConfig(joinedPath, (result) => { + callback(result || url); + }); + //this.tryFindConfig(workingDir, callback); + } + }); + }, + + tryFindConfig(folder, callback) { + var file = path.join(folder, '.postbird'); + console.log("ckecking if exist", file); + fs.access(file, 'r', (err, fd) => { + if (err) { + callback(false); + } else { + this.loadDotPostbird(file, callback); + } + }); + }, + + loadDotPostbird(path, callback) { + fs.readFile(path, 'utf8', (err, data) => { + if (err) throw err; + var fileData = data.toString().trim(); + if (fileData[0] == "{") { + var data = JSON.parse(fileData); + callback(data.default); + } else { + var data = require(path); + callback(data.default); + } + }); + } + // nativeObject = YAML.parse(yamlString); +}; + +module.exports = CliUtil;