git ssb

15+

ansuz / dnssb



Tree: b3dbb252f189d256c891324515d83ad97f2ffee6

Files: b3dbb252f189d256c891324515d83ad97f2ffee6 / index.js

3641 bytesRaw
1#!/bin/sh
2':' //; exec "$(command -v nodejs || command -v node)" "$0" "$@"
3// http://unix.stackexchange.com/questions/65235/universal-node-js-shebang
4
5var SsbRef = require("ssb-ref");
6var Lib = require("./lib/");
7
8(function () {
9if (require.main !== module) { return module.exports = Lib; }
10
11var publishHelp = "\tdnssb publish [previous record key...] name [ttl] [class] type value";
12var updateHelp = "\tdnssb update name [ttl] [class] type value";
13var branchHelp = "\tdnssb branch name type (class)";
14var serverHelp = "\tdnssb server port host";
15var dumpHelp = "\tdnssb dump";
16
17var CLI_Help = function () {
18 [
19 "try one of:",
20 serverHelp,
21 publishHelp,
22 updateHelp,
23 branchHelp,
24 dumpHelp,
25 ].forEach(function (m) {
26 console.log(m);
27 });
28};
29
30var argv = process.argv.slice(2);
31
32switch (argv[0]) {
33 case undefined:
34 CLI_Help();
35 break;
36 case 'dump':
37 (function () {
38 var count = 0;
39 Lib.dump.records(function (record) { // each
40 console.log(JSON.stringify(record, null, 2));
41 count++;
42 }, function (sbot) { // done
43 console.log("Found a total of %s valid ssb-dns records", count);
44 sbot.close();
45 });
46 }());
47 break;
48 case 'server':
49 (function () {
50 var port = argv[1] || 53053;
51 var host = argv[2] || '127.0.0.1';
52 var Client = require("ssb-client");
53
54 Client(function (err, sbot) {
55 if (err) throw err;
56
57 Lib.server.listen(sbot, port, host, function () {
58 console.log("server listening on %s:%s", host, port);
59 });
60 });
61 }());
62 break;
63 case 'publish':
64 (function () {
65 var branches = [];
66 while (argv[1] && SsbRef.isMsgId(argv[1])) {
67 branches.push(argv.splice(1));
68 }
69
70 if (argv.length < 4) {
71 console.log("Try:");
72 console.error(publishHelp);
73 return;
74 }
75
76 var record = Lib.parse.argsToRecord(argv.slice(1));
77
78 Lib.publish.record(branches, record, function (err, msg) {
79 if (err) {
80 console.error(err);
81 process.exit(1);
82 }
83 console.log(JSON.stringify(msg, null, 2));
84 process.exit(0);
85 });
86 }());
87 break;
88 case 'update':
89 (function () {
90 if (argv.length < 4) {
91 console.log("Try:");
92 console.error(updateHelp);
93 return;
94 }
95
96 var record = Lib.parse.argsToRecord(argv.slice(1));
97
98 Lib.query.branches(record.name, record.type, record.class, function (err, branches) {
99 if (err) throw err;
100 Lib.publish.record(branches, record, function (err, msg) {
101 if (err) throw err;
102 console.log(msg);
103 process.exit(0);
104 });
105 })
106 }());
107 break;
108
109
110 case 'branch':
111 (function () {
112 if (argv.length < 3) {
113 console.log("Try:");
114 console.error(branchHelp);
115 return;
116 }
117
118 var name = argv[1];
119 var type = argv[2];
120 var _class = argv[3];
121
122 Lib.query.branches(name, type, _class, function (err, branches) {
123 if (!branches.length) {
124 console.error("No branches found");
125 process.exit(1);
126 }
127 branches.forEach(function (branch) {
128 console.log(branch);
129 });
130
131 process.exit(0);
132 });
133 }());
134 break;
135 default:
136 CLI_Help();
137 break;
138}
139
140}());
141

Built with git-ssb-web