-
Notifications
You must be signed in to change notification settings - Fork 1
/
cexec.js
108 lines (102 loc) · 4 KB
/
cexec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* @flow */
'use strict';
const Cjdnskeys = require('cjdnskeys');
const Cjdnstools = require('./index.js');
const Cjdnsadmin = require('cjdnsadmin');
const nThen = require('nthen');
const Minimist = require('minimist');
const FUNCTION_DOCS =
"https://github.com/cjdelisle/cjdns/blob/crashey/doc/admin-api.md#funcs";
const functionDescriptions = (cjdns) => {
const out = {};
Object.keys(cjdns._funcs).map((x) => {
const args = cjdns._funcs[x];
const argDesc = Object.keys(args).map((x) => {
const argDef = args[x];
let out = '--' + x + '=<' + argDef.type + '>';
if (!argDef.required) { out = '[' + out + ']'; }
return out;
});
argDesc.sort();
out[x] = 'cjdnstool cexec ' + x + ' ' + argDesc.join(' ');
});
return out;
};
const usage = module.exports.usage = () => {
console.log("cjdnstool cexec [COMMAND ARGS...]");
console.log(" # run `cjdnstool cexec` for all commands");
console.log(" # below are a couple of examples");
console.log(" Allocator_bytesAllocated # Determine how many bytes are allocated");
console.log(" Core_pid # Get the cjdns core pid number");
console.log(" ReachabilityCollector_getPeerInfo");
console.log(" --page=<Int> # Get information about your peers (paginated)");
console.log(" SupernodeHunter_status # Get a status report from the snode hunter");
console.log(" see: " + FUNCTION_DOCS);
};
const main = module.exports.main = (argv /*:Array<string>*/) => {
let cjdns;
nThen((waitFor) => {
Cjdnsadmin.connect(waitFor(function (err, c) {
if (err) {
console.error(err.message);
waitFor.abort();
}
cjdns = c;
}));
}).nThen((waitFor) => {
const defs = functionDescriptions(cjdns);
if (!argv.length) {
console.log("see: " + FUNCTION_DOCS);
const keys = Object.keys(defs);
keys.sort();
keys.forEach((k) => {
console.log(defs[k]);
});
} else {
const func = argv[0];
if (!cjdns[func]) {
return void console.error(func + " is not an RPC in cjdns");
}
const funcDef = cjdns._funcs[func];
const argNames = Object.keys(funcDef);
const argVals = new Array(argNames.length).fill(undefined);
let err;
if (argv.indexOf('--help') > -1) {
console.log("Usage: " + defs[func]);
console.log("see: " + FUNCTION_DOCS);
return;
}
const args = argv.slice(1).map((x) => {
const res = x.replace(/^--([a-zA-Z0-9]*)\=(.*)$/, (all, key, val) => {
if (argNames.indexOf(key) === -1) {
err = "Argument " + key + " (" + all + ") is not a valid arg to " +
"function call " + func;
return '';
}
if (typeof(val) === 'string' && funcDef[key].type !== 'String') {
try { val = JSON.parse(val); } catch (e) { }
}
argVals[argNames.indexOf(key)] = val;
return '';
});
if (res) { err = "Unexpected argument " + x; }
});
if (err) {
console.error(err);
console.error("Usage: " + defs[func]);
return;
}
argVals.push(waitFor((err, ret) => {
if (err) { return void console.error(err); }
delete ret.txid;
console.log(JSON.stringify(ret, null, ' '));
}));
cjdns[func].apply(cjdns, argVals);
}
}).nThen((waitFor) => {
cjdns.disconnect();
});
};
if (!module.parent) {
main(process.argv.slice(2));
}