Skip to content

Commit

Permalink
Try add postbird command line util
Browse files Browse the repository at this point in the history
  • Loading branch information
Paxa committed Nov 6, 2016
1 parent 5cb868f commit ef3c5e0
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .postbird
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"default": "postgres://localhost/test_data"
}
17 changes: 15 additions & 2 deletions bin/postbird
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -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) => {
Expand Down
67 changes: 67 additions & 0 deletions lib/cli_util.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit ef3c5e0

Please sign in to comment.