Skip to content
This repository has been archived by the owner on Mar 26, 2021. It is now read-only.

Commit

Permalink
Add version and update commands
Browse files Browse the repository at this point in the history
  • Loading branch information
robarnold committed Jun 4, 2011
1 parent 5f244f4 commit 243d384
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bot.js
@@ -1,5 +1,6 @@
var irc = require("irc");
var channels = require("./channels");
var updater = require("./updater");

var mode = 'irc';
if (process.argv.length > 2)
Expand Down Expand Up @@ -29,6 +30,12 @@ var client = new irc.Client("irc.mozilla.org", kNick, {
port: 6697
});

updater.restart = function () {
client.disconnect("Updating...hopefully don't need metaafrosdwilsh");
require("child_process").spawn('node', [__filename], { customFds: [0, 1, 2] });
process.exit();
};

client.on("invite", function (channel, from) {
if (kAuthorizedUsers.indexOf(from) !== -1) {
// TODO: solve race condition here
Expand Down
9 changes: 9 additions & 0 deletions channels.js
Expand Up @@ -7,6 +7,7 @@ var randompicker = require("./randompicker");
var committers = require("./committers");
var textutils = require("./textutils");
var TemporalCache = require("./temporalcache");
var updater = require("./updater");

var kIssueTrackerUrl = 'https://github.com/sdwilsh/tree-bot/issues/new';
var treeNames = require('./jsondb')('treenames.json').db;
Expand Down Expand Up @@ -218,6 +219,12 @@ ChannelController.prototype = {
help: function (from) {
this.channel.tell(from)("See https://github.com/sdwilsh/tree-bot/blob/master/README for a list of commands");
},
version: function (from) {
this.channel.tell(from)(updater.version);
},
update: function (from) {
updater.update(this.channel.tell(from));
},
handleCommand: function (from, text) {
var self = this;
function tryCommand(matcher, cb) {
Expand All @@ -235,6 +242,8 @@ ChannelController.prototype = {
tryCommand(/^watch ([A-Fa-f0-9]{12}) on ([A-Za-z-]+)(?: for (.+))?/, this.watchTree);
tryCommand(/^unwatch ([A-Fa-f0-9]{12}) on ([A-Za-z-]+)(?: for (.+))?/, this.unwatchTree);
tryCommand(/^h[ae]lp/, this.help);
tryCommand(/^version$/, this.version);
tryCommand(/^update/, this.update);
}
};

Expand Down
33 changes: 33 additions & 0 deletions updater.js
@@ -0,0 +1,33 @@
var exec = require('child_process').exec;

var version;
var restart;

exec('git rev-parse HEAD', function (error, stdout, stderr) {
if (error !== null) {
console.log("Got error trying to grab current version");
} else {
version = stdout;
}
});

exports.__defineGetter__('version', function () { return version; });
exports.__defineGetter__('restart', function () { return restart; });
exports.__defineSetter__('restart', function (r) { restart = r; });
exports.update = function update(cb) {
exec('git pull --rebase origin master', function (error, stdout, stderr) {
if (error !== null) {
return cb("Had trouble fetching a new version: {0}", stderr);
}
exec('git rev-parse origin/master', function (error, new_version, stderr) {
if (error !== null) {
return cb("Had trouble parsing fetched version: {0}", stderr);
}
if (new_version === version) {
return cb("Looks like I'm up to date!");
}
cb("Updating to {0}. See you soon I hope!", new_version);
restart();
});
});
};

0 comments on commit 243d384

Please sign in to comment.