oasis-pub.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env node
  2. const path = require('path');
  3. const ssbConfig = require(path.join(__dirname, '..', 'src', 'server', 'ssb_config'));
  4. const ssbClient = require(path.join(__dirname, '..', 'src', 'server', 'node_modules', 'ssb-client'));
  5. const socketPath = path.join(ssbConfig.path, 'socket');
  6. const publicInteger = (ssbConfig.keys.public || '').replace('.ed25519', '');
  7. const remote = `unix:${socketPath}~noauth:${publicInteger}`;
  8. const cmd = process.argv[2];
  9. const args = process.argv.slice(3);
  10. const usage = () => {
  11. console.error('Usage: sh oasis.sh <command> [args]');
  12. console.error('');
  13. console.error('PUB admin commands (sbot must be running: sh oasis.sh server):');
  14. console.error(' whoami Print this PUB id');
  15. console.error(' invite [N] Create an invite code (default uses=1)');
  16. console.error(' name <text> Set PUB display name');
  17. console.error(' announce <host> [port] Publish a pub address (default port=8008)');
  18. console.error(' follow <feedId> Follow another PUB');
  19. console.error(' status Show peer / replication status');
  20. console.error(' gossip List known gossip peers');
  21. process.exit(1);
  22. };
  23. if (!cmd) usage();
  24. const call = (fn, ...a) => new Promise((res, rej) => fn(...a, (e, r) => e ? rej(e) : res(r)));
  25. ssbClient(ssbConfig.keys, { remote, caps: ssbConfig.caps }).then(async (ssb) => {
  26. try {
  27. switch (cmd) {
  28. case 'whoami': {
  29. const r = await call(ssb.whoami.bind(ssb));
  30. console.log(JSON.stringify(r, null, 2));
  31. break;
  32. }
  33. case 'invite': {
  34. const uses = Math.max(1, parseInt(args[0] || '1', 10));
  35. const code = await call(ssb.invite.create.bind(ssb.invite), uses);
  36. console.log(code);
  37. break;
  38. }
  39. case 'name': {
  40. const text = String(args[0] || '');
  41. if (!text) { console.error('Missing name'); process.exit(1); }
  42. const me = await call(ssb.whoami.bind(ssb));
  43. const r = await call(ssb.publish.bind(ssb), { type: 'about', about: me.id, name: text });
  44. console.log(JSON.stringify(r, null, 2));
  45. break;
  46. }
  47. case 'announce': {
  48. const host = args[0];
  49. const port = parseInt(args[1] || '8008', 10);
  50. if (!host) { console.error('Missing host'); process.exit(1); }
  51. const me = await call(ssb.whoami.bind(ssb));
  52. const r = await call(ssb.publish.bind(ssb), { type: 'pub', address: { key: me.id, host, port } });
  53. console.log(JSON.stringify(r, null, 2));
  54. break;
  55. }
  56. case 'follow': {
  57. const feedId = args[0];
  58. if (!feedId) { console.error('Missing feedId'); process.exit(1); }
  59. const r = await call(ssb.publish.bind(ssb), { type: 'contact', contact: feedId, following: true });
  60. console.log(JSON.stringify(r, null, 2));
  61. break;
  62. }
  63. case 'status': {
  64. const r = await call(ssb.status.bind(ssb));
  65. console.log(JSON.stringify(r, null, 2));
  66. break;
  67. }
  68. case 'gossip': {
  69. const r = await call(ssb.gossip.peers.bind(ssb.gossip));
  70. console.log(JSON.stringify(r, null, 2));
  71. break;
  72. }
  73. default:
  74. usage();
  75. }
  76. ssb.close();
  77. } catch (e) {
  78. console.error('Error:', e.message || e);
  79. process.exit(1);
  80. }
  81. }).catch((e) => {
  82. console.error('Connection error:', e.message || e);
  83. console.error('Is the Oasis sbot running? (sh oasis.sh server)');
  84. process.exit(1);
  85. });