move Copay.js -> API.js, and handle case of no command better

* API is a more appropriate name for what this is. It is intended to be the
 interface used by external apps.
* The case where you run "copay" with no command is now handled better.
This commit is contained in:
Ryan X. Charles 2014-04-15 10:50:54 -03:00
parent 4773b6577a
commit dd2e8c585e
2 changed files with 32 additions and 32 deletions

View File

@ -1,12 +1,12 @@
var imports = require('soop').imports();
var PublicKeyRing = imports.PublicKeyRing || require('../js/models/core/PublicKeyRing');
var PublicKeyRing = imports.PublicKeyRing || require('./js/models/core/PublicKeyRing');
//var Wallet = imports.Wallet || require('../js/models/core/Wallet');
var Copay = function(opts) {
var API = function(opts) {
this._init(opts);
};
Copay.prototype._init = function(opts) {
API.prototype._init = function(opts) {
var self = this;
opts = opts ? opts : {};
@ -26,14 +26,14 @@ Copay.prototype._init = function(opts) {
//self.wallet = self.opts.wallet ? self.opts.wallet : new Wallet();
};
Copay.prototype._command = function(command, args, callback) {
API.prototype._command = function(command, args, callback) {
var self = this;
if (command[0] == "_")
if (!command || command[0] == "_")
return callback(new Error('invalid command'));
if (typeof self[command] == 'function') {
var f = Copay.prototype[command];
var f = API.prototype[command];
if (f.argTypes[f.argTypes.length-1][1] == 'function')
return self[command].apply(self, args.concat([callback]));
else
@ -43,8 +43,8 @@ Copay.prototype._command = function(command, args, callback) {
return callback(new Error('invalid command'));
};
Copay._checkArgTypes = function(command, args) {
var f = Copay.prototype[command];
API._checkArgTypes = function(command, args) {
var f = API.prototype[command];
for (var i in args) {
if (typeof args[i] != f.argTypes[i][1])
return false;
@ -53,63 +53,63 @@ Copay._checkArgTypes = function(command, args) {
};
function checkArgs(name, args) {
if (!Copay._checkArgTypes(name, args))
if (!API._checkArgTypes(name, args))
throw new Error('Invalid arguments');
}
Copay.prototype.echo = function echo(str, callback) {
API.prototype.echo = function echo(str, callback) {
var self = this;
checkArgs(arguments.callee.name, arguments);
return callback(null, str);
};
Copay.prototype.echo.argTypes =
API.prototype.echo.argTypes =
[
['str', 'string'],
['callback', 'function']
];
/*
Copay.prototype.getBalance = function(callback) {
API.prototype.getBalance = function(callback) {
var self = this;
checkArgs('getBalance', arguments);
return callback(null, self.wallet.getBalance([]));
};
Copay.prototype.getBalance.argTypes =
API.prototype.getBalance.argTypes =
[
['callback', 'function']
];
*/
Copay.prototype.getArgTypes = function getArgTypes(command, callback) {
API.prototype.getArgTypes = function getArgTypes(command, callback) {
var self = this;
checkArgs(arguments.callee.name, arguments);
if (command[0] == '_' || typeof Copay.prototype[command] != 'function')
if (command[0] == '_' || typeof API.prototype[command] != 'function')
return callback(new Error('Invalid command'));
var argTypes = Copay.prototype[command].argTypes;
var argTypes = API.prototype[command].argTypes;
return callback(null, argTypes);
};
Copay.prototype.getArgTypes.argTypes =
API.prototype.getArgTypes.argTypes =
[
['command', 'string'],
['callback', 'function']
];
Copay.prototype.getCommands = function getCommands(callback) {
API.prototype.getCommands = function getCommands(callback) {
var self = this;
checkArgs(arguments.callee.name, arguments);
var fs = [];
for (var i in Copay.prototype) {
var f = Copay.prototype[i];
for (var i in API.prototype) {
var f = API.prototype[i];
if (typeof f == 'function' && i[0] != "_")
fs.push(i);
};
@ -117,30 +117,30 @@ Copay.prototype.getCommands = function getCommands(callback) {
return callback(null, fs);
};
Copay.prototype.getCommands.argTypes =
API.prototype.getCommands.argTypes =
[
['callback', 'function']
];
Copay.prototype.getPublicKeyRingId = function getPublicKeyRingId(callback) {
API.prototype.getPublicKeyRingId = function getPublicKeyRingId(callback) {
var self = this;
checkArgs(arguments.callee.name, arguments);
return callback(null, self.publicKeyRing.id);
};
Copay.prototype.getPublicKeyRingId.argTypes =
API.prototype.getPublicKeyRingId.argTypes =
[
['callback', 'function']
];
Copay.prototype.help = function help(callback) {
API.prototype.help = function help(callback) {
this.getCommands.apply(this, arguments);
};
Copay.prototype.help.argTypes =
API.prototype.help.argTypes =
[
['callback', 'function']
];
module.exports = require('soop')(Copay);
module.exports = require('soop')(API);

View File

@ -1,26 +1,26 @@
#!/usr/bin/env node
var Copay = require('./Copay.js');
var API = require('../API.js');
var commander = require('commander');
var main = function() {
commander
.version("0.0.1")
.option('-f, --file [file]', 'AES encrypted data file', 'copay.json.aes')
.option('-f, --file [file]', 'AES encrypted data file', 'api.json.aes')
.option('-p, --pass [passphrase]', 'AES wallet passphrase')
.option('-c, --client', 'Issue command over RPC to copay daemon')
.option('-c, --client', 'Issue command over RPC to api daemon')
.option('-d, --daemon', 'Run as daemon accepting RPC commands')
.option('--rpcport [port]', 'RPC port [18332]', Number, 18332)
.option('--rpcuser [user]', 'RPC user [user]', String, 'user')
.option('--rpcpass [password]', 'RPC password [pass]', String, 'pass')
.parse(process.argv);
var copay = new Copay(commander);
var api = new API(commander);
var args = commander.args;
try {
copay._command(args[0], args.slice(1), function(err, result) {
api._command(args[0], args.slice(1), function(err, result) {
if (err)
return console.log("" + err);