diff --git a/commands.js b/commands.js index 7277ea3..387aff3 100644 --- a/commands.js +++ b/commands.js @@ -78,7 +78,7 @@ const findShortcut = (shortcut, objects) => { return expansion; }; -const expandCommand = (msg, projects) => { +commands.expandCommand = (msg, projects) => { return new Promise((resolve, reject) => { const regex = /^!([a-zA-Z0-9]+) ([a-zA-Z0-9]+) ([a-zA-Z0-9]+) ?(.*)$/; const shortcuts = msg.match(regex); @@ -90,6 +90,7 @@ const expandCommand = (msg, projects) => { expansion.project = expansion.project.toLowerCase(); } else { reject('could not resolve project: ' + shortcuts[1]); + return; } expansion.service = findShortcut(shortcuts[2], projects[expansion.project]); @@ -97,6 +98,7 @@ const expandCommand = (msg, projects) => { expansion.service = expansion.service.toLowerCase(); } else { reject('could not resolve service: ' + shortcuts[2]); + return; } expansion.subcommand = findShortcut(shortcuts[3], projects[expansion.project][expansion.service]); @@ -104,12 +106,14 @@ const expandCommand = (msg, projects) => { expansion.subcommand = expansion.subcommand.toLowerCase(); } else { reject('could not resolve subcommand: ' + shortcuts[4]); + return; } // if args are given, write them in command if(shortcuts[4]) expansion.args = shortcuts[4]; resolve(expansion); + return; }); }; diff --git a/index.js b/index.js index d43f559..a90703a 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ const sdk = require('matrix-js-sdk'); const yaml = require('js-yaml'); global.config = yaml.safeLoad(fs.readFileSync('config.yaml')); -const matrixClient = sdk.createClient({ +global.matrixClient = sdk.createClient({ baseUrl: config.matrix.baseurl, accessToken: config.matrix.accesstoken, userId: config.matrix.userid @@ -25,24 +25,6 @@ process.on('unhandledRejection', (reason) => { console.log('Reason: ' + reason); }); -const commands = {}; - -commands['rebuild'] = {}; -commands['rebuild'].regex = /^!rebuild ([a-zA-Z0-9\-]*)$/; -commands['rebuild'].usage = '!rebuild '; -commands['rebuild'].exec = function(args, room, event) { - const container = args[1]; - - matrixClient.sendNotice(room.roomId, 'rebuilding container ' + container); - - remoteExec('./rebuild.sh ' + container) - .then(function(result) { - matrixClient.sendNotice(room.roomId, event.sender.userId + '\nrebuilt container ' + container); - }, function(result) { - matrixClient.sendNotice(room.roomId, event.sender.userId + '\nrebuild of container ' + container + ' failed.\ncode: ' + result.code + '\nstdout: \n```\n' + result.stdout + '```\nstderr: \n```\n' + result.stderr + '\n```'); - }); - -}; matrixClient.on('Room.timeline', function(event, room, resetTimeline) { @@ -53,33 +35,29 @@ matrixClient.on('Room.timeline', function(event, room, resetTimeline) { return; } - let body = event.getContent().body; + const body = event.getContent().body; - let project = body.match(/^\!([a-zA-Z]*)/); - if(project === null) { - return; - } - project = project[1]; + commands.expandCommand(body, commands.projects) + .then((expansion) => { + let subcommand = commands.projects[expansion.project][expansion.service][expansion.subcommand]; + const args = expansion.args.match(subcommand.regex); - const command = commands[project]; - - if(command !== undefined) { - const args = body.match(command.regex); - if(args === null) { - matrixClient.sendNotice(room.roomId, 'usage: ' + command.usage); + if(args === null && !subcommand.regex.test(expansion.args)) { + matrixClient.sendNotice(room.roomId, 'usage: ' + subcommand.usage); return; } - command.exec(args, room, event); - }else{ //command not found - let tmp = commands.fetchCommand(project, commands); + subcommand.exec(args, room, event); + }, (fail) => { // command not found +/* let tmp = commands.fetchCommand(project, commands.projects); let suggestions = ""; for(let i = 0; i < tmp.length; i++){ suggestions += "\n"+tmp[i]; } matrixClient.sendNotice(room.roomId, 'Unrecognized command.\n' + 'Did you mean:'+suggestions); - } +*/ + }); }); diff --git a/projects/lubiland.js b/projects/lubiland.js index da42eaa..d0343d7 100644 --- a/projects/lubiland.js +++ b/projects/lubiland.js @@ -8,15 +8,17 @@ lubiland['infrastructure'] = {}; lubiland['infrastructure']['rebuild'] = {}; lubiland['infrastructure']['rebuild'].regex = /^([a-zA-Z0-9\-\_]*)$/; lubiland['infrastructure']['rebuild'].usage = ''; -lubiland['infrastructure']['rebuild'].exec = (args) => { - const container = args[1]; - - matrixClient.sendNotice(room.roomId, 'rebuilding container ' + container); - - remoteExec('./rebuild.sh ' + container) - .then(function(result) { - matrixClient.sendNotice(room.roomId, event.sender.userId + '\nrebuilt container ' + container); - }, function(result) { - matrixClient.sendNotice(room.roomId, event.sender.userId + '\nrebuild of container ' + container + ' failed.\ncode: ' + result.code + '\nstdout: \n```\n' + result.stdout + '```\nstderr: \n```\n' + result.stderr + '\n```'); +lubiland['infrastructure']['rebuild'].exec = (args, room, event) => { + return new Promise((resolve, reject) => { + const container = args[1]; + + matrixClient.sendNotice(room.roomId, 'rebuilding container ' + container); + + remoteExec('./rebuild.sh ' + container) + .then(function(success) { + matrixClient.sendNotice(room.roomId, event.sender.userId + '\nrebuilt container ' + container); + }, function(process) { + matrixClient.sendNotice(room.roomId, event.sender.userId + '\nrebuild of container ' + container + ' failed.\ncode: ' + process.code + '\nstdout: \n```\n' + process.stdout + '```\nstderr: \n```\n' + process.stderr + '\n```'); + }); }); }; \ No newline at end of file