Skip to content

Commit 78bee45

Browse files
author
Ramesh, Gaurav
committed
implement help and gem commands
1 parent 1987ced commit 78bee45

File tree

9 files changed

+178
-0
lines changed

9 files changed

+178
-0
lines changed

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# node-waf configuration
20+
.lock-wscript
21+
22+
# Compiled binary addons (http://nodejs.org/api/addons.html)
23+
build/Release
24+
25+
# Dependency directory
26+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
27+
node_modules

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# slack-terminalize-cli
2+
3+
Sample app to work with [slack-terminalize](https://github.com/ggauravr/slack-terminalize) with some basic custom commands

commands/app-error.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function (param) {
2+
console.log("param.. err handling", param.command, param.args);
3+
};

commands/gem.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var request = require('request'),
2+
util = require('../util');
3+
4+
module.exports = function (param) {
5+
var channel = param.channel,
6+
endpoint = param.commandConfig.endpoint.replace('{gem}', param.args[0]);
7+
8+
(function(channel){
9+
10+
request(endpoint, function (err, response, body) {
11+
var info = [];
12+
13+
if (!err && response.statusCode === 200) {
14+
body = JSON.parse(body);
15+
16+
info.push('Gem: ' + body.name + ' - ' + body.info);
17+
info.push('Authors: ' + body.authors);
18+
info.push('Project URI: ' + body.project_uri);
19+
}
20+
else {
21+
info = ['No such gem found!'];
22+
}
23+
24+
util.postMessage(channel, info.join('\n\n'));
25+
});
26+
27+
})(channel);
28+
29+
};

commands/help.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var slackTerminal = require('slack-terminalize'),
2+
commands = slackTerminal.getCommandObjects(),
3+
util = require('../util');
4+
5+
var _helpAll = function () {
6+
var name,
7+
index,
8+
command,
9+
response = [];
10+
11+
index = 1;
12+
for (name in commands) {
13+
command = commands[name];
14+
15+
if (!command.exclude) {
16+
response.push(index++ + '. ' + _helpCommand(name));
17+
}
18+
}
19+
20+
return response.join('\n\n');
21+
};
22+
23+
var _helpCommand = function (name) {
24+
var response = [ commands[name].help, 'Alias: ' + commands[name].alias.join(', '), commands[name].description ];
25+
26+
return response.join('\n\n');
27+
};
28+
29+
module.exports = function (param) {
30+
var channel = param.channel,
31+
response;
32+
33+
if (!param.args.length) {
34+
response = _helpAll();
35+
}
36+
else {
37+
response = _helpCommand(param.args[0]);
38+
}
39+
40+
util.postMessage(channel, response);
41+
};

config/commands.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"help": {
3+
"alias": [ "halp" ],
4+
"endpoint": "#",
5+
"help": "help [command](optional)",
6+
"description": "To get help on all supported commands, or a specified command"
7+
},
8+
9+
"gem": {
10+
"alias": [],
11+
"endpoint": "https://rubygems.org/api/v1/gems/{gem}.json",
12+
"help": "gem [gem-name]",
13+
"description": "Fetches details of the specified Ruby gem"
14+
},
15+
16+
"app-error": {
17+
"exclude": true
18+
}
19+
}

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var slackTerminal = require('slack-terminalize');
2+
3+
slackTerminal.init({
4+
SLACK_TOKEN: '<xoxb-your-token-here>'
5+
}, {
6+
CONFIG_DIR: __dirname + '/config',
7+
COMMAND_DIR: __dirname + '/commands'
8+
});

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "slack-sample-cli",
3+
"version": "1.0.0",
4+
"description": "Sample app to show working with slack-terminalize module",
5+
"main": "index.js",
6+
"dependencies": {
7+
"request": "^2.65.0",
8+
"slack-terminalize": "^0.0.3"
9+
},
10+
"scripts": {
11+
"test": "echo \"Error: no test specified\" && exit 1"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "[email protected]:gnramesh/slack-sample-cli.git"
16+
},
17+
"engines": {
18+
"node": "0.12.x"
19+
},
20+
"keywords": [
21+
"slack",
22+
"terminal"
23+
],
24+
"author": "Gaurav Ramesh",
25+
"license": "MIT"
26+
}

util.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Wrapper function for postMessage from slack-client to handle formatting.
3+
*
4+
* @param { object } slack-client Channel boject
5+
* @param { string } message to send to Slack channel
6+
* @param { boolean } flag to indicate block formatting
7+
* @return { none }
8+
*
9+
*/
10+
var postMessage = function (channel, response, format) {
11+
12+
format = format || true;
13+
response = (format && '```' + response + '```') || response;
14+
15+
channel.postMessage({
16+
as_user: true,
17+
text: response
18+
});
19+
20+
};
21+
22+
exports.postMessage = postMessage;

0 commit comments

Comments
 (0)