Bump path-parse from 1.0.6 to 1.0.7
[KisSync.git] / servcmd.sh.js
blobd47cac68e65482c2853652745e2c05c232a139e7
1 #!/usr/bin/env node
2 /*
3 ** CyTube Service Socket Commandline
4 */
6 const readline = require('readline');
7 const spawn = require('child_process').spawn;
8 const util = require('util');
9 const net = require('net');
10 const fs = require('fs');
12 const COMPLETIONS = [
13 "/delete_old_tables",
14 "/gc",
15 "/globalban",
16 "/reload",
17 "/reloadcert",
18 "/reload-partitions",
19 "/switch",
20 "/unglobalban",
21 "/unloadchan"
24 var Config = require("./lib/config");
25 Config.load("config.yaml");
27 if(!Config.get("service-socket.enabled")){
28 console.error('The Service Socket is not enabled.');
29 process.exit(1);
32 const SOCKETFILE = Config.get("service-socket.socket");
34 // Wipe the TTY
35 process.stdout.write('\x1Bc');
37 var commandline, eventlog, syslog, errorlog;
38 var client = net.createConnection(SOCKETFILE).on('connect', () => {
39 commandline = readline.createInterface({
40 input: process.stdin,
41 output: process.stdout,
42 completer: tabcomplete
43 });
44 commandline.setPrompt("> ", 2);
45 commandline.on("line", function(line) {
46 if(line === 'exit'){ return cleanup(); }
47 if(line === 'quit'){ return cleanup(); }
48 if(line.match(/^\/globalban/) && line.split(/\s+/).length === 2){
49 console.log('You must provide a reason')
50 return commandline.prompt();
52 client.write(line);
53 commandline.prompt();
54 });
55 commandline.on('close', function() {
56 return cleanup();
57 });
58 commandline.on("SIGINT", function() {
59 commandline.clearLine();
60 commandline.question("Terminate connection? ", function(answer) {
61 return answer.match(/^y(es)?$/i) ? cleanup() : commandline.output.write("> ");
62 });
63 });
64 commandline.prompt();
66 console.log = function() { cmdouthndlr("log", arguments); }
67 console.warn = function() { cmdouthndlr("warn", arguments); }
68 console.error = function() { cmdouthndlr("error", arguments); }
69 // console.info is reserved in this script for the exit message
70 // this prevents an extraneous final prompt from readline on terminate
72 eventlog = spawn('tail', ['-f', 'events.log']);
73 eventlog.stdout.on('data', function (data) {
74 console.log(data.toString().replace(/^(.+)$/mg, 'events: $1'));
75 });
77 syslog = spawn('tail', ['-f', 'sys.log']);
78 syslog.stdout.on('data', function (data) {
79 console.log(data.toString().replace(/^(.+)$/mg, 'sys: $1'));
80 });
82 errorlog = spawn('tail', ['-f', 'error.log']);
83 errorlog.stdout.on('data', function (data) {
84 console.log(data.toString().replace(/^(.+)$/mg, 'error: $1'));
85 });
87 }).on('data', (msg) => {
88 msg = msg.toString();
90 if(msg === '__disconnect'){
91 console.log('Server shutting down.');
92 return cleanup();
95 // Generic message handler
96 console.log('server: ', data)
98 }).on('error', (data) => {
99 console.error('Unable to connect to Service Socket.', data);
100 process.exit(1);
103 function cmdouthndlr(type, args) {
104 var t = Math.ceil((commandline.line.length + 3) / process.stdout.columns);
105 var text = util.format.apply(console, args);
106 commandline.output.write("\n\x1B[" + t + "A\x1B[0J");
107 commandline.output.write(text + "\n");
108 commandline.output.write(Array(t).join("\n\x1B[E"));
109 commandline._refreshLine();
112 function cleanup(){
113 console.info('\n',"Terminating.",'\n');
114 eventlog.kill('SIGTERM');
115 syslog.kill('SIGTERM');
116 client.end();
117 process.exit(0);
120 function tabcomplete(line) {
121 return [COMPLETIONS.filter((cv)=>{ return cv.indexOf(line) == 0; }), line];