ssb-g1-tip/tip/sbotc.js

57 lines
1.5 KiB
JavaScript
Executable File

const {program} = require("commander");
const ssbClient = require('ssb-client');
const pull = require('pull-stream');
let convertToInt = (value) => {
return parseInt(value);
}
let throwProgram = (err) => {
console.error(err.message);
process.exit(1);
}
program.version('.0.0.1');
program
.requiredOption('-s, --host <value>', 'host name or ip of ssb-server', 'oasis')
.requiredOption('-p, --port <number>', 'port of ssb-server', convertToInt, 8008)
/**
* whoami command
*/
program
.command('whoami')
.description('return your identity')
.action(() => {
ssbClient({host:program.host, port:program.port}, (err, sbot) => {
if(err) throwProgram(err)
sbot.whoami((err, identity) =>{
if(err) throwProgram
console.log(JSON.stringify(identity));
sbot.close();
})
})
});
/**
* query
*/
program
.command('query <query>')
.description('you can execute a query')
.action((query) => {
ssbClient({host:program.host, port:program.port}, (err, sbot) =>{
if(err) throwProgram(err);
pull(sbot.query.read({query:JSON.parse(query)}),pull.collect( (err, array) => {
if(err) throwProgram(err);
array.forEach((result) => {
console.log(JSON.stringify(result));
})
sbot.close();
}))
})
});
program.parse(process.argv);