Commit 4a7923b5 authored by Erik Schwartz's avatar Erik Schwartz 🉐
Browse files

Complete rewrite to use ssh2 instead of scp2

parent 5b1b98e3
......@@ -5,7 +5,8 @@
// -------------------------------------------------------------------------
const fs = require('fs');
const scp = require('scp2');
const SshClient = require('ssh2').Client;
const ssh = new SshClient();
// -------------------------------------------------------------------------
// Helper functions
......@@ -22,27 +23,46 @@ function readSftpOptions(json) {
}
}
function downloadFile(opt, f, callback) {
scp.scp(opt, f, (err) => {
if (err) {
return callback(err);
}
return callback();
});
function downloadFile(sshOpt, remoteFile, localFile, callback) {
ssh.on('ready', () => {
ssh.sftp((err, sftp) => {
if (err) {
console.error('Problem setting up SFTP session');
return callback(err);
}
sftp.exists(remoteFile, (doesExist) => {
if (!doesExist) {
return callback(null, 'Remote file (' + remoteFile + ') not found');
}
sftp.fastGet(remoteFile, localFile, (err) => {
if (err) {
console.error('Problem downloading');
return callback(err);
}
sftp.unlink(remoteFile, (err) => {
if (err) {
console.error('Problem removing remote file');
}
return callback(null, 'Downloaded to ' + localFile);
});
});
});
});
}).connect(sshOpt);
}
// -------------------------------------------------------------------------
// Exported functions
// -------------------------------------------------------------------------
exports.doIt = function(optionsFile, callback) {
let o = readSftpOptions(optionsFile);
let scpOpt = o.remoteSettings;
let outFile = o.localFile;
downloadFile(scpOpt, outFile, (err) => {
exports.doIt = function(optionsConf, callback) {
let o = readSftpOptions(optionsConf);
downloadFile(o.sshSettings, o.remoteFile, o.localFile, (err, msg) => {
if (err) {
ssh.end();
return callback(err);
}
return callback();
ssh.end();
return callback(null, msg);
});
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment