Commit 511ce661 by Girish Ramakrishnan

add surfer put examples and fix put

1 parent 38932f15
Showing with 51 additions and 27 deletions
...@@ -128,32 +128,27 @@ function logout() { ...@@ -128,32 +128,27 @@ function logout() {
}); });
} }
function put(filePath, otherFilePaths, options) { function putOne(filePath, destination, options, callback) {
checkConfig(options); const absoluteFilePath = path.resolve(filePath);
const stat = safe.fs.statSync(absoluteFilePath);
var destination = ''; if (!stat) return callback(`Could not stat ${filePath}: ${safe.error.message}`);
// take the last argument as destination let files, base;
if (otherFilePaths.length > 0) {
destination = otherFilePaths.pop(); if (stat.isFile()) {
if (otherFilePaths.length > 0 && destination[destination.length-1] !== '/') destination += '/'; base = destination + path.basename(filePath);
files = [ absoluteFilePath ];
} else if (stat.isDirectory()) {
base = destination + (filePath.endsWith('.') ? '' : path.basename(filePath) + '/');
files = collectFiles([ absoluteFilePath ], options);
} else {
return callback(); // ignore
} }
var files = collectFiles([ filePath ].concat(otherFilePaths), options);
async.eachSeries(files, function (file, callback) { async.eachSeries(files, function (file, callback) {
var relativeFilePath; let relativeFilePath = file.slice(absoluteFilePath.length + 1); // will be '' when filePath is a file
let destinationPath = base + relativeFilePath;
if (path.isAbsolute(file)) { console.log('Uploading file %s -> %s', file.cyan, destinationPath.cyan);
relativeFilePath = path.basename(file);
} else if (path.resolve(file).indexOf(process.cwd()) === 0) { // relative to current dir
relativeFilePath = path.resolve(file).slice(process.cwd().length + 1);
} else { // relative but somewhere else
relativeFilePath = path.basename(file);
}
var destinationPath = (destination ? '/' + destination : '') + '/' + relativeFilePath;
console.log('Uploading file %s -> %s', relativeFilePath.cyan, destinationPath.cyan);
superagent.post(gServer + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) { superagent.post(gServer + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) {
if (result && result.statusCode === 403) return callback(new Error('Upload destination ' + destinationPath + ' not allowed')); if (result && result.statusCode === 403) return callback(new Error('Upload destination ' + destinationPath + ' not allowed'));
...@@ -164,7 +159,25 @@ function put(filePath, otherFilePaths, options) { ...@@ -164,7 +159,25 @@ function put(filePath, otherFilePaths, options) {
callback(null); callback(null);
}); });
}, function (error) { }, callback);
}
function put(filePaths, options) {
checkConfig(options);
if (filePaths.length < 2) {
console.log('target directory is required.'.red);
process.exit(1);
}
let destination = filePaths.pop();
if (!path.isAbsolute(destination)) {
console.log('target directory must be absolute'.red);
process.exit(1);
}
if (!destination.endsWith('/')) destination += '/';
async.eachSeries(filePaths, (filePath, iteratorDone) => putOne(filePath, destination, options, iteratorDone), function (error) {
if (error) { if (error) {
console.log('Failed to put file.', error.message.red); console.log('Failed to put file.', error.message.red);
process.exit(1); process.exit(1);
......
...@@ -21,10 +21,21 @@ program.command('logout') ...@@ -21,10 +21,21 @@ program.command('logout')
.description('Logout from server') .description('Logout from server')
.action(actions.logout); .action(actions.logout);
program.command('put <file|dir> [files...]') program.command('put <file|dir...>')
.option('-a --all', 'Also include hidden files and folders.', false) .option('-a --all', 'Also include hidden files and folders.', false)
.description('Put a file, last argument is destination if provided') .description('Puts a list of files or dirs to the destination. The last argument is destination dir')
.action(actions.put); .action(actions.put)
.on('--help', function() {
console.log();
console.log(' Examples:');
console.log();
console.log(' $ surfer put file.txt / # puts to /file.txt');
console.log(' $ surfer put file.txt /data # puts to /data/file.txt');
console.log(' $ surfer put dir /data # puts dir/* as /data/dir/*');
console.log(' $ surfer put dir/. / # puts dir/* as /app/data/*');
console.log(' $ surfer put dir1 dir2 file1 / # puts as /dir1/* /dir2/* and /file');
console.log();
});
program.command('get [file|dir]') program.command('get [file|dir]')
.description('Get a file or directory listing') .description('Get a file or directory listing')
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!