git ssb

15+

ansuz / dnssb



Tree: 5166584127078cfd0d1f1b37acff5e540c24ddce

Files: 5166584127078cfd0d1f1b37acff5e540c24ddce / index.js

4768 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";
16var showHelp = "\tdnssb show [domain]";
17
18var CLI_Help = function () {
19 [
20 "try one of:",
21 serverHelp,
22 publishHelp,
23 updateHelp,
24 branchHelp,
25 dumpHelp,
26 showHelp,
27 ].forEach(function (m) {
28 console.log(m);
29 });
30};
31
32var argv = process.argv.slice(2);
33
34switch (argv[0]) {
35 case undefined:
36 CLI_Help();
37 break;
38 case 'dump':
39 (function () {
40 var Client = require("ssb-client");
41
42 Client(function (err, sbot) {
43 if (err) throw err;
44
45 var count = 0;
46 Lib.dump.records(sbot, function (record) { // each
47 console.log(JSON.stringify(record, null, 2));
48 count++;
49 }, function (err) { // done
50 if (err) throw err;
51 console.log("Found a total of %s valid ssb-dns records", count);
52 sbot.close();
53 });
54 });
55 }());
56 break;
57 case 'show':
58 (function () {
59 var name = argv[1] || '';
60 var Client = require("ssb-client");
61
62 Client(function (err, sbot) {
63 if (err) throw err;
64
65 Lib.dump.formattedRecords(sbot, {name: name}, function (line) {
66 console.log(line);
67 }, function (err) { // done
68 if (err) throw err;
69 sbot.close();
70 });
71 });
72 }());
73 break;
74 case 'server':
75 (function () {
76 var port = argv[1] || 53053;
77 var host = argv[2] || '127.0.0.1';
78 var Client = require("ssb-client");
79
80 Client(function (err, sbot, config) {
81 if (err) throw err;
82
83 Lib.server.listen(sbot, port, host, function () {
84 console.log("server listening on %s:%s", host, port);
85 }, config && config.dns);
86 });
87 }());
88 break;
89 case 'publish':
90 (function () {
91 var branches = [];
92 while (argv[1] && SsbRef.isMsgId(argv[1])) {
93 branches.push(argv.splice(1, 1)[0]);
94 }
95
96 if (argv.length < 4) {
97 console.log("Try:");
98 console.error(publishHelp);
99 return;
100 }
101
102 var record = Lib.parse.argsToRecord(argv.slice(1));
103
104 Lib.publish.record(branches, record, function (err, msg) {
105 if (err) {
106 console.error(err);
107 process.exit(1);
108 }
109 console.log(JSON.stringify(msg, null, 2));
110 process.exit(0);
111 });
112 }());
113 break;
114 case 'update':
115 (function () {
116 if (argv.length < 4) {
117 console.log("Try:");
118 console.error(updateHelp);
119 return;
120 }
121
122 var record = Lib.parse.argsToRecord(argv.slice(1));
123 var Client = require("ssb-client");
124
125 Client(function (err, sbot) {
126 if (err) throw err;
127
128 Lib.query.branches(sbot, record.name, record.type, record.class, function (err, branches) {
129 if (err) throw err;
130 Lib.publish.record(branches, record, function (err, msg) {
131 if (err) throw err;
132 console.log(msg);
133 process.exit(0);
134 });
135 });
136 });
137 }());
138 break;
139
140
141 case 'branch':
142 (function () {
143 if (argv.length < 3) {
144 console.log("Try:");
145 console.error(branchHelp);
146 return;
147 }
148
149 var name = argv[1];
150 var type = argv[2];
151 var _class = argv[3];
152 var Client = require("ssb-client");
153
154 Client(function (err, sbot) {
155 if (err) throw err;
156
157 Lib.query.branches(sbot, name, type, _class, function (err, branches) {
158 if (err) throw err;
159
160 if (!branches.length) {
161 console.error("No branches found");
162 process.exit(1);
163 }
164 branches.forEach(function (branch) {
165 console.log(branch);
166 });
167
168 process.exit(0);
169 });
170 });
171 }());
172 break;
173 default:
174 CLI_Help();
175 break;
176}
177
178}());
179

Built with git-ssb-web