Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an optional progress bar for sftp uploads. #47

Merged
merged 1 commit into from
Jan 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ sftp: {
path: '/tmp/',
host: '<%= secret.host %>',
username: '<%= secret.username %>',
password: '<%= secret.password %>'
password: '<%= secret.password %>',
showProgress: true
}
}
},
Expand Down Expand Up @@ -186,6 +187,10 @@ The permissions to apply to directories created with createDirectories. The def
directoryPermissions: parseInt(755, 8)
```

###### showProgress ```boolean```

Show a progress bar during the file transfer. The default is ```false```.

##### Connection options

There are three mutually exclusive sets of connection options. They are
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
},
"dependencies": {
"ssh2": ">=0.2.14",
"async": "*"
"async": "*",
"progress": "~1.1.3"
},
"devDependencies": {
"grunt": "~0.4",
Expand Down
23 changes: 23 additions & 0 deletions tasks/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,28 @@ exports.init = function (grunt) {
return connectionOptions;
};

// Converts a file size in bytes into a human-readable file size string.
exports.fileSizeReadable = function (bytes) {
var KB = 1024,
MB = KB * 1024,
GB = MB * 1024,
result;

if (bytes < KB) {
result = bytes + 'B';
}
else if (bytes < MB) {
result = Math.floor(bytes / KB) + 'KB';
}
else if (bytes < GB) {
result = Math.floor(bytes / MB) + 'MB';
}
else {
result = Math.floor(bytes / GB) + 'GB';
}

return result;
};

return exports;
};
31 changes: 29 additions & 2 deletions tasks/sftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = function (grunt) {
var Connection = require('ssh2');
var path = require('path');
var sftpHelper = require("./lib/sftpHelpers").init(grunt);
var ProgressBar = require('progress');

var options = this.options({
path: '',
Expand All @@ -29,7 +30,8 @@ module.exports = function (grunt) {
minimatch: {},
srcBasePath: "",
createDirectories: false,
directoryPermissions: parseInt(755, 8)
directoryPermissions: parseInt(755, 8),
showProgress: false
});

var tally = {
Expand All @@ -56,6 +58,7 @@ module.exports = function (grunt) {
setOption('username');
setOption('password');
setOption('passphrase');
setOption('showProgress');

// add trailing slash to path if needed
if (!options.path.match(/(\/|\\)$/)) {
Expand Down Expand Up @@ -149,8 +152,32 @@ module.exports = function (grunt) {
}

async.eachSeries(fileQueue, function (file, callback) {
var fpOptions;

if (options.showProgress) {
var fileSize = fs.statSync(file.src).size;
var barTemplate = file.src + ' [:bar] :percent of ' + utillib.fileSizeReadable(fileSize);

var bar = new ProgressBar(barTemplate, {
complete: '=',
incomplete: ' ',
width: 20,
total: fileSize
});

fpOptions = {
step: function (totalSent, lastSent, total) {
bar.tick(lastSent);
}
};
} else {
fpOptions = {
step: function () {}
};
}

grunt.verbose.writeln('copying ' + file.src + ' to ' + file.dest);
sftp.fastPut(file.src, file.dest, function (err) {
sftp.fastPut(file.src, file.dest, fpOptions, function (err) {
if (err) {
return callback(err);
}
Expand Down
11 changes: 10 additions & 1 deletion test/util_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ module.exports = {
test.equal(util.validateNumber("name", 32), 32, "should validate number");
test.equal(util.validateNumber("name", 0), 0, "should validate zero");
test.done();
}
},
fileSizeReadable: function (test) {
'use strict';
test.equal(util.fileSizeReadable(0), "0B", "0 should be 0B");
test.equal(util.fileSizeReadable(1023), "1023B", "less than one KB should be B");
test.equal(util.fileSizeReadable(1048575), "1023KB", "less than one MB should be KB");
test.equal(util.fileSizeReadable(1073741823), "1023MB", "less than one GB should be MB");
test.equal(util.fileSizeReadable(1073741825), "1GB", "greater than one GB should be GB");

test.done();
}
};