(tag: string, cmd: string, callback: (code) => void)
| 67 | } |
| 68 | |
| 69 | export function sendCommand(tag: string, cmd: string, callback: (code) => void) { |
| 70 | let cmdMap = { |
| 71 | 'stop': COMMAND.STOP, |
| 72 | 'restart': COMMAND.RESTART, |
| 73 | 'status': COMMAND.STATUS, |
| 74 | 'statusjson': COMMAND.STATUSJSON, |
| 75 | }; |
| 76 | |
| 77 | let command = cmdMap[cmd.toLowerCase()]; |
| 78 | |
| 79 | if (!command) { |
| 80 | console.error('Command is not supported'); |
| 81 | return callback(1); |
| 82 | } |
| 83 | |
| 84 | let path = util.format('/tmp/lightsword-%s.sock', tag); |
| 85 | let socket = net.createConnection(path, async () => { |
| 86 | await socket.writeAsync(new Buffer([command])); |
| 87 | let msg = await socket.readAsync(); |
| 88 | console.info(msg.toString('utf8')); |
| 89 | socket.destroy(); |
| 90 | callback(0); |
| 91 | }); |
| 92 | |
| 93 | socket.on('error', (err: Error) => { console.info(`${tag} is not running or unix socket error.`); callback(1); }); |
| 94 | socket.setTimeout(5 * 1000); |
| 95 | } |
nothing calls this directly
no test coverage detected