Skip to content
This repository has been archived by the owner on Feb 5, 2022. It is now read-only.

Commit

Permalink
add binary file
Browse files Browse the repository at this point in the history
  • Loading branch information
Hsiaoming Yang committed Mar 5, 2013
1 parent 6fa9e7f commit d4fa388
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 81 deletions.
165 changes: 165 additions & 0 deletions bin/scp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#!/usr/bin/env node

var fs = require('fs');
var path = require('path');
var util = require('util');
var client = require('../');

main(process.argv.slice());

function main(argv) {
var identify, src, dest;

var quiet = false;
var port = 22;
var defaults = {};

var getArg = function() {
var args = argv.shift();
args = args.split('=');
if (args.length > 1) {
argv.unshit(args.slice(1).join('='));
}
return args[0];
}

var arg, remains = [];

while (argv.length) {
arg = getArg();
switch(arg) {
case '-p':
case '--port':
port = argv.shift();
break;
case '-q':
case '--quiet':
quiet = true;
break;
case '-i':
case '--identify':
identify = argv.shift();
break;
case '-h':
case '--help':
helpMessage();
break;
default:
remains.push(arg);
break;
}
}

if (remains.length !== 4) {
helpMessage();
}
printLog(quiet);

src = remains[2];
dest = remains[3];

defaults = {
port: parseInt(port, 10)
}

var password;
var parsed = client.parse(src);

if (parsed.username && parsed.host && parsed.path) {
password = parsed.password;
} else {
password = client.parse(dest).password;
}

client.on('error', function(err) {
if (err.code === 'ECONNREFUSED') {
prompt(' password: ', function(val) {
client.close();
delete client.__ssh;
defaults.password = val;
scp(src, dest, defaults);
});
} else {
console.error(err);
process.exit(1);
}
});

if (!password && identify && fs.existsSync(identify)) {
defaults.privateKey = fs.readFileSync(identify);
scp(src, dest, defaults);
} else if (!password) {
prompt(' password: ', function(val) {
defaults.password = val;
scp(src, dest, defaults);
});
} else {
defaults.password = password;
scp(src, dest, defaults);
}
}

function helpMessage() {
console.log();
var lines = [
' Usage:',
' scp2 [-p 22] localfile server',
'',
' Options:',
' -p, --port=<port> ssh port, default: 22',
' -i, --identify=<file> identify file',
' -q, --quiet do not show log',
' -h, --help display this message',
'',
' Examples:',
' $ scp2 data.txt admin:[email protected]:/home/admin/',
' $ scp2 data.txt [email protected]:/home/admin/rename.txt',
'',
];
console.log(lines.join('\n'));
process.exit();
}

function printLog(quiet) {
if (!quiet) {
client.on('connect', function() {
util.log('connected');
});
client.on('ready', function() {
util.log('ready');
});
client.on('mkdir', function(p) {
util.log('mkdir ' + p);
});
client.on('write', function(o) {
util.log('write ' + o.destination);
});
client.on('close', function() {
util.log('close');
});
client.on('end', function() {
util.log('end');
});
}
}

function prompt(str, fn) {
process.stdout.write(str);
process.stdin.setEncoding('utf8');
process.stdin.once('data', function(val) {
fn(val.trim());
}).resume();
}

function scp(src, dest, defaults) {
client.defaults(defaults);
client.scp(src, dest, function(err) {
if (err) {
console.error(err);
process.exit(1);
} else {
util.log('scp success.');
}
process.exit();
});
}
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/scp');
40 changes: 31 additions & 9 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var Buffer = require('buffer').Buffer;
var EventEmitter = require('events').EventEmitter;
var Connection = require('ssh2');
var _ = require('lodash');
var file = require('./file');


function Client(options) {
Expand All @@ -29,6 +28,7 @@ Client.prototype.parse = function(remote) {
// username:password@host:/path/to
var regex = /^([a-zA-Z0-9\-\.]+)(\:.*)?@([^:]+)(\:.*)?$/;
var m = remote.match(regex);
if (!m) return {};
var ret = {
username: m[1],
host: m[3]
Expand All @@ -39,8 +39,10 @@ Client.prototype.parse = function(remote) {
if (m[4]) {
ret.path = m[4].slice(1);
}
this.remote = ret;
return ret;
}
this.remote = remote;
return remote;
};

Expand All @@ -49,9 +51,14 @@ Client.prototype.sftp = function(callback) {
callback(null, this.__sftp);
return;
}

var remote = _.defaults(this.remote, this._options);
var self = this;
if (this.__ssh) {
this.__ssh.connect(remote);
return;
}

var self = this;
var ssh = new Connection();
ssh.on('connect', function() {
self.emit('connect');
Expand Down Expand Up @@ -137,33 +144,47 @@ Client.prototype.write = function(options, callback) {
content = new Buffer(content, options.encoding);
}
var self = this;

this.sftp(function(err, sftp) {
sftp.open(destination, 'w', attrs, function(err, handle) {

var _write = function(handle) {
self.emit('write', options);
sftp.write(handle, content, 0, content.length, 0, function(err) {
var writeErr = err;
sftp.close(handle, function(err) {
callback(err || writeErr);
});
});
};

sftp.open(destination, 'w', attrs, function(err, handle) {
if (err) {
// destination is directory
destination = path.join(
destination, path.basename(options.source)
);
sftp.open(destination, 'w', attrs, function(err, handle) {
_write(handle);
});
} else {
_write(handle);
}
});
});
};

Client.prototype.upload = function(src, dest, callback) {
if (process.platform === 'win32') {
dest = dest.replace(/\\/g, '/');
}
var self = this;
fs.stat(src, function(err, stats) {
if (err) {
callback(err);
return;
}

var attrs = {
ctime: stats.ctime,
atime: stats.atime,
mtime: stats.mtime
};

var attrs = util.inspect(stats);
fs.readFile(src, function(err, content) {
if (err) {
callback(err);
Expand All @@ -172,6 +193,7 @@ Client.prototype.upload = function(src, dest, callback) {
// mkdir for safety
self.mkdir(path.dirname(dest), attrs, function(err) {
self.write({
source: src,
destination: dest,
content: content,
attrs: attrs
Expand Down
43 changes: 0 additions & 43 deletions lib/file.js

This file was deleted.

29 changes: 0 additions & 29 deletions lib/index.js

This file was deleted.

Loading

0 comments on commit d4fa388

Please sign in to comment.