From 898fe7c8bb0b04eea2e91d92f275c95713e8ec79 Mon Sep 17 00:00:00 2001 From: Johannes Zellner Date: Tue, 1 Mar 2016 13:04:12 +0100 Subject: Fix file deletion and add dry-run option --- cli/actions.js | 21 ++++++++++++++++----- cli/surfer.js | 2 ++ 2 files changed, 18 insertions(+), 5 deletions(-) (limited to 'cli') diff --git a/cli/actions.js b/cli/actions.js index d10e154..8a8624e 100644 --- a/cli/actions.js +++ b/cli/actions.js @@ -180,16 +180,27 @@ function get(filePath) { // }); } -function del(filePath) { +function del(filePath, options) { checkConfig(); + var query = safe.JSON.parse(safe.JSON.stringify(gQuery)); + query.recursive = options.recursive; + query.dryRun = options.dryRun; + var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1); - superagent.del(config.server() + API + relativeFilePath).query(gQuery).end(function (error, result) { - if (error && error.status === 401) return console.log('Login failed'); + superagent.del(config.server() + API + relativeFilePath).query(query).end(function (error, result) { + if (error && error.status === 401) return console.log('Login failed'.red); if (error && error.status === 404) return console.log('No such file or directory'); - if (error && error.status === 403) return console.log('No such file or directory'); + if (error && error.status === 403) return console.log('Failed. Target is a directory. Use %s to delete directories.', '--recursive'.yellow); if (error) return console.log('Failed', result ? result.body : error); - console.log('Success. Removed %s files.', result.body.entries.length); + if (options.dryRun) { + console.log('This would remove %s files:', result.body.entries.length); + result.body.entries.forEach(function (entry) { + console.log('\t %s', entry); + }); + } else { + console.log('Success. Removed %s files.', result.body.entries.length); + } }); } diff --git a/cli/surfer.js b/cli/surfer.js index 278724d..4c096d3 100755 --- a/cli/surfer.js +++ b/cli/surfer.js @@ -24,6 +24,8 @@ program.command('get [file]') .action(actions.get); program.command('del ') + .option('-r --recursive', 'Recursive delete directories.', false) + .option('-d --dry-run', 'Only list files to delete.', false) .description('Delete a file or directory') .action(actions.del); -- cgit v1.2.3