mecano
Version:
Common functions for system deployment.
105 lines (100 loc) • 2.81 kB
JavaScript
// Generated by CoffeeScript 1.9.1
var fs, misc, remove;
module.exports = function(options, callback) {
var do_chkhash, do_dsthash, do_exists, do_move, do_remove_src, do_replace_dest, do_srchash;
do_exists = function() {
if (typeof options.log === "function") {
options.log("Mecano `move`: Stat destination");
}
return fs.stat(options.ssh, options.destination, function(err, stat) {
if ((err != null ? err.code : void 0) === 'ENOENT') {
return do_move();
}
if (err) {
return callback(err);
}
if (options.force) {
return do_replace_dest();
} else {
return do_srchash();
}
});
};
do_srchash = function() {
if (options.source_md5) {
return do_dsthash();
}
if (typeof options.log === "function") {
options.log("Mecano `move`: Get source md5");
}
return misc.file.hash(options.ssh, options.source, 'md5', function(err, hash) {
if (err) {
return callback(err);
}
options.source_md5 = hash;
return do_dsthash();
});
};
do_dsthash = function() {
if (options.destination_md5) {
return do_chkhash();
}
if (typeof options.log === "function") {
options.log("Mecano `move`: Get destination md5");
}
return misc.file.hash(options.ssh, options.destination, 'md5', function(err, hash) {
if (err) {
return callback(err);
}
options.destination_md5 = hash;
return do_chkhash();
});
};
do_chkhash = function() {
if (options.source_md5 === options.destination_md5) {
return do_remove_src();
} else {
return do_replace_dest();
}
};
do_replace_dest = function() {
if (typeof options.log === "function") {
options.log("Mecano `move`: Remove " + options.destination);
}
return remove({
ssh: options.ssh,
destination: options.destination
}, function(err, removed) {
if (err) {
return callback(err);
}
return do_move();
});
};
do_move = function() {
if (typeof options.log === "function") {
options.log("Mecano `move`: Rename " + options.source + " to " + options.destination);
}
return fs.rename(options.ssh, options.source, options.destination, function(err) {
if (err) {
return callback(err);
}
return callback(null, true);
});
};
do_remove_src = function() {
if (typeof options.log === "function") {
options.log("Mecano `move`: Remove " + options.source);
}
return remove({
ssh: options.ssh,
destination: options.source
}, function(err, removed) {
return callback(err);
});
};
return do_exists();
};
fs = require('ssh2-fs');
remove = require('./remove');
misc = require('./misc');