Files: 970c91583716ce49e23e1a89eb78103d6ecfdfa9 / index.js
4838 bytesRaw
1 | |
2 | ':' //; exec "$(command -v nodejs || command -v node)" "$0" "$@" |
3 | // http://unix.stackexchange.com/questions/65235/universal-node-js-shebang |
4 | |
5 | var SsbRef = require("ssb-ref"); |
6 | var Lib = require("./lib/"); |
7 | |
8 | (function () { |
9 | if (require.main !== module) { return module.exports = Lib; } |
10 | |
11 | var publishHelp = "\tdnssb publish [previous record key...] name [ttl] [class] type value"; |
12 | var updateHelp = "\tdnssb update name [ttl] [class] type value"; |
13 | var branchHelp = "\tdnssb branch name type (class)"; |
14 | var serverHelp = "\tdnssb server port host"; |
15 | var dumpHelp = "\tdnssb dump"; |
16 | var showHelp = "\tdnssb show [domain]"; |
17 | |
18 | var 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 | |
32 | var argv = process.argv.slice(2); |
33 | |
34 | switch (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 | // keep alive |
84 | setInterval(sbot.whoami, 10e3) |
85 | |
86 | Lib.server.listen(sbot, port, host, function () { |
87 | console.log("server listening on %s:%s", host, port); |
88 | }, config && config.dns); |
89 | }); |
90 | }()); |
91 | break; |
92 | case 'publish': |
93 | (function () { |
94 | var branches = []; |
95 | while (argv[1] && SsbRef.isMsgId(argv[1])) { |
96 | branches.push(argv.splice(1, 1)[0]); |
97 | } |
98 | |
99 | if (argv.length < 4) { |
100 | console.log("Try:"); |
101 | console.error(publishHelp); |
102 | return; |
103 | } |
104 | |
105 | var record = Lib.parse.argsToRecord(argv.slice(1)); |
106 | |
107 | Lib.publish.record(branches, record, function (err, msg) { |
108 | if (err) { |
109 | console.error(err); |
110 | process.exit(1); |
111 | } |
112 | console.log(JSON.stringify(msg, null, 2)); |
113 | process.exit(0); |
114 | }); |
115 | }()); |
116 | break; |
117 | case 'update': |
118 | (function () { |
119 | if (argv.length < 4) { |
120 | console.log("Try:"); |
121 | console.error(updateHelp); |
122 | return; |
123 | } |
124 | |
125 | var record = Lib.parse.argsToRecord(argv.slice(1)); |
126 | var Client = require("ssb-client"); |
127 | |
128 | Client(function (err, sbot) { |
129 | if (err) throw err; |
130 | |
131 | Lib.query.branches(sbot, record.name, record.type, record.class, function (err, branches) { |
132 | if (err) throw err; |
133 | Lib.publish.record(branches, record, function (err, msg) { |
134 | if (err) throw err; |
135 | console.log(msg); |
136 | process.exit(0); |
137 | }); |
138 | }); |
139 | }); |
140 | }()); |
141 | break; |
142 | |
143 | |
144 | case 'branch': |
145 | (function () { |
146 | if (argv.length < 3) { |
147 | console.log("Try:"); |
148 | console.error(branchHelp); |
149 | return; |
150 | } |
151 | |
152 | var name = argv[1]; |
153 | var type = argv[2]; |
154 | var _class = argv[3]; |
155 | var Client = require("ssb-client"); |
156 | |
157 | Client(function (err, sbot) { |
158 | if (err) throw err; |
159 | |
160 | Lib.query.branches(sbot, name, type, _class, function (err, branches) { |
161 | if (err) throw err; |
162 | |
163 | if (!branches.length) { |
164 | console.error("No branches found"); |
165 | process.exit(1); |
166 | } |
167 | branches.forEach(function (branch) { |
168 | console.log(branch); |
169 | }); |
170 | |
171 | process.exit(0); |
172 | }); |
173 | }); |
174 | }()); |
175 | break; |
176 | default: |
177 | CLI_Help(); |
178 | break; |
179 | } |
180 | |
181 | }()); |
182 |
Built with git-ssb-web