Skip to content

Commit

Permalink
Code refactor + test for ssh
Browse files Browse the repository at this point in the history
  • Loading branch information
mthenw committed Feb 23, 2015
1 parent 4711e72 commit b85c828
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
18 changes: 12 additions & 6 deletions index.js
Expand Up @@ -93,13 +93,19 @@ if (program.daemonize) {
/**
* When connected send starting data
*/
var sshOptions = {
remoteHost: program.remoteHost,
remoteUser: program.remoteUser,
remotePort: program.remotePort
};
var tailer;
if (doSSH) {
var sshOptions = {
remoteHost: program.remoteHost,
remoteUser: program.remoteUser,
remotePort: program.remotePort
};

tailer = tail(program.args, {buffer: program.number, ssh: sshOptions});
} else {
tailer = tail(program.args, {buffer: program.number});
}

var tailer = tail(program.args, {buffer: program.number, sshOptions:sshOptions});
var filesSocket = io.of('/' + filesNamespace).on('connection', function (socket) {
socket.emit('options:lines', program.lines);

Expand Down
16 changes: 10 additions & 6 deletions lib/tail.js
@@ -1,7 +1,7 @@
'use strict';

var EventEmitter = require('events').EventEmitter;
var spawn = require('child_process').spawn;
var childProcess = require('child_process');
var util = require('util');
var CBuffer = require('CBuffer');

Expand All @@ -12,12 +12,16 @@ var Tail = function (path, options) {
this._buffer = new CBuffer(options.buffer);
var tail;

if (options && options.sshOptions && options.sshOptions.remoteHost) {
var args = [options.sshOptions.remoteUser + '@' + options.sshOptions.remoteHost, '-p',
options.sshOptions.remotePort, 'tail -f ' + path];
tail = spawn('ssh', args);
if (options.ssh) {
var args = [
options.ssh.remoteUser + '@' + options.ssh.remoteHost,
'-p', options.ssh.remotePort,
'tail -f ' + path
];

tail = childProcess.spawn('ssh', args);
} else {
tail = spawn('tail', ['-n', options.buffer, '-F', path]);
tail = childProcess.spawn('tail', ['-n', options.buffer, '-F', path]);
}

tail.stderr.on('data', function (data) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "frontail",
"version": "1.4.0",
"version": "1.4.1",
"description": "realtime log stream in the browser",
"homepage": "https://github.com/mthenw/frontail",
"author": "Maciej Winnicki <maciej.winnicki@gmail.com>",
Expand Down
18 changes: 18 additions & 0 deletions test/tail.js
Expand Up @@ -4,6 +4,8 @@ require('should');
var fs = require('fs');
var tail = require('../lib/tail');
var temp = require('temp');
var sinon = require('sinon');
var childProcess = require('child_process');

var TEMP_FILE_PROFIX = '';
var SPAWN_DELAY = 10;
Expand Down Expand Up @@ -46,6 +48,22 @@ describe('tail', function () {
});
});

describe('with ssh options', function () {
it('should call ssh command', function () {
sinon.spy(childProcess, 'spawn');
var sshOptions = {
remoteUser: 'testUser',
remoteHost: 'host',
remotePort: 1234
};

tail('test/path', {ssh: sshOptions});

childProcess.spawn.calledWith('ssh', ['testUser@host', '-p', 1234, 'tail -f test/path']).should.be.true;
childProcess.spawn.restore();
});
});

function writeLines(fd, count) {
for (var i = 0; i < count; i += 1) {
fs.writeSync(fd, 'line' + i + '\n');
Expand Down

0 comments on commit b85c828

Please sign in to comment.