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 ', 'host name or ip of ssb-server', 'oasis') .requiredOption('-p, --port ', '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 ') .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);