]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/commitdiff
Fix file deletion and add dry-run option
authorJohannes Zellner <johannes@nebulon.de>
Tue, 1 Mar 2016 12:04:12 +0000 (13:04 +0100)
committerJohannes Zellner <johannes@nebulon.de>
Tue, 1 Mar 2016 12:04:12 +0000 (13:04 +0100)
cli/actions.js
cli/surfer.js
package.json
src/files.js

index d10e1545870cd41f67486d4ac45063c45c89dc81..8a8624e345d3354348c3834ed5b0bdbb818a3869 100644 (file)
@@ -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);
+        }
     });
 }
index 278724d777b88e78bd38dfe59a9272376a832b68..4c096d3bb18ab200a66de198f2fb8b4b2fa954c1 100755 (executable)
@@ -24,6 +24,8 @@ program.command('get [file]')
     .action(actions.get);
 
 program.command('del <file>')
+    .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);
 
index cf47f5467ee87cfa33eb5207bbb9b9f6814cf2e2..5fab285f472cf64138d6f1962e80764b025b9602 100644 (file)
@@ -29,7 +29,7 @@
     "connect-timeout": "^1.6.2",
     "cookie-parser": "^1.4.1",
     "debug": "^2.2.0",
-    "del": "^1.2.0",
+    "del": "^2.2.0",
     "ejs": "^2.4.1",
     "express": "^4.13.4",
     "express-session": "^1.13.0",
index c2a4e0f570e76032ee0ca17046637a2621dae355..2214449042f5d9cb52a0a849c8f17a8820bc1281 100644 (file)
@@ -60,6 +60,10 @@ function getAbsolutePath(filePath) {
     return absoluteFilePath;
 }
 
+function removeBasePath(filePath) {
+    return filePath.slice(gBasePath.length);
+}
+
 function get(req, res, next) {
     var filePath = req.params[0];
     var absoluteFilePath = getAbsolutePath(filePath);
@@ -104,18 +108,29 @@ function put(req, res, next) {
 
 function del(req, res, next) {
     var filePath = req.params[0];
+    var recursive = !!req.query.recursive;
+    var dryRun = !!req.query.dryRun;
+
     var absoluteFilePath = getAbsolutePath(filePath);
     if (!absoluteFilePath) return next(new HttpError(404, 'Not found'));
 
     // absoltueFilePath has to have the base path prepended
-    if (absoluteFilePath.length <= gBasePath.length) return next(new HttpError(403, 'Forbidden'));
+    if (absoluteFilePath.length <= gBasePath.length) return next(new HttpError(404, 'Not found'));
 
     fs.stat(absoluteFilePath, function (error, result) {
         if (error) return next(new HttpError(404, error));
 
-        rm(absoluteFilePath, function (error, result) {
-            if (error) return next(new HttpError(500, 'Unable to remove'));
+        if (result.isDirectory() && !recursive) return next(new HttpError(403, 'Is directory'));
+
+        // add globs to get file listing
+        if (result.isDirectory()) absoluteFilePath += '/**';
+
+        rm(absoluteFilePath, { dryRun: dryRun }).then(function (result) {
+            result = result.map(removeBasePath);
             next(new HttpSuccess(200, { entries: result }));
+        }, function (error) {
+            console.error(error);
+            next(new HttpError(500, 'Unable to remove'));
         });
     });
 }