Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
PUBLIC
/
surfer-okd
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 898fe7c8
authored
Mar 01, 2016
by
Johannes Zellner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix file deletion and add dry-run option
1 parent
d755925f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
37 additions
and
9 deletions
cli/actions.js
cli/surfer.js
package.json
src/files.js
cli/actions.js
View file @
898fe7c
...
@@ -180,16 +180,27 @@ function get(filePath) {
...
@@ -180,16 +180,27 @@ function get(filePath) {
// });
// });
}
}
function
del
(
filePath
)
{
function
del
(
filePath
,
options
)
{
checkConfig
();
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
);
var
relativeFilePath
=
path
.
resolve
(
filePath
).
slice
(
process
.
cwd
().
length
+
1
);
superagent
.
del
(
config
.
server
()
+
API
+
relativeFilePath
).
query
(
gQ
uery
).
end
(
function
(
error
,
result
)
{
superagent
.
del
(
config
.
server
()
+
API
+
relativeFilePath
).
query
(
q
uery
).
end
(
function
(
error
,
result
)
{
if
(
error
&&
error
.
status
===
401
)
return
console
.
log
(
'Login failed'
);
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
===
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
);
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
);
}
});
});
}
}
cli/surfer.js
View file @
898fe7c
...
@@ -24,6 +24,8 @@ program.command('get [file]')
...
@@ -24,6 +24,8 @@ program.command('get [file]')
.
action
(
actions
.
get
);
.
action
(
actions
.
get
);
program
.
command
(
'del <file>'
)
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'
)
.
description
(
'Delete a file or directory'
)
.
action
(
actions
.
del
);
.
action
(
actions
.
del
);
...
...
package.json
View file @
898fe7c
...
@@ -29,7 +29,7 @@
...
@@ -29,7 +29,7 @@
"connect-timeout"
:
"^1.6.2"
,
"connect-timeout"
:
"^1.6.2"
,
"cookie-parser"
:
"^1.4.1"
,
"cookie-parser"
:
"^1.4.1"
,
"debug"
:
"^2.2.0"
,
"debug"
:
"^2.2.0"
,
"del"
:
"^
1
.2.0"
,
"del"
:
"^
2
.2.0"
,
"ejs"
:
"^2.4.1"
,
"ejs"
:
"^2.4.1"
,
"express"
:
"^4.13.4"
,
"express"
:
"^4.13.4"
,
"express-session"
:
"^1.13.0"
,
"express-session"
:
"^1.13.0"
,
...
...
src/files.js
View file @
898fe7c
...
@@ -60,6 +60,10 @@ function getAbsolutePath(filePath) {
...
@@ -60,6 +60,10 @@ function getAbsolutePath(filePath) {
return
absoluteFilePath
;
return
absoluteFilePath
;
}
}
function
removeBasePath
(
filePath
)
{
return
filePath
.
slice
(
gBasePath
.
length
);
}
function
get
(
req
,
res
,
next
)
{
function
get
(
req
,
res
,
next
)
{
var
filePath
=
req
.
params
[
0
];
var
filePath
=
req
.
params
[
0
];
var
absoluteFilePath
=
getAbsolutePath
(
filePath
);
var
absoluteFilePath
=
getAbsolutePath
(
filePath
);
...
@@ -104,18 +108,29 @@ function put(req, res, next) {
...
@@ -104,18 +108,29 @@ function put(req, res, next) {
function
del
(
req
,
res
,
next
)
{
function
del
(
req
,
res
,
next
)
{
var
filePath
=
req
.
params
[
0
];
var
filePath
=
req
.
params
[
0
];
var
recursive
=
!!
req
.
query
.
recursive
;
var
dryRun
=
!!
req
.
query
.
dryRun
;
var
absoluteFilePath
=
getAbsolutePath
(
filePath
);
var
absoluteFilePath
=
getAbsolutePath
(
filePath
);
if
(
!
absoluteFilePath
)
return
next
(
new
HttpError
(
404
,
'Not found'
));
if
(
!
absoluteFilePath
)
return
next
(
new
HttpError
(
404
,
'Not found'
));
// absoltueFilePath has to have the base path prepended
// absoltueFilePath has to have the base path prepended
if
(
absoluteFilePath
.
length
<=
gBasePath
.
length
)
return
next
(
new
HttpError
(
40
3
,
'Forbidden
'
));
if
(
absoluteFilePath
.
length
<=
gBasePath
.
length
)
return
next
(
new
HttpError
(
40
4
,
'Not found
'
));
fs
.
stat
(
absoluteFilePath
,
function
(
error
,
result
)
{
fs
.
stat
(
absoluteFilePath
,
function
(
error
,
result
)
{
if
(
error
)
return
next
(
new
HttpError
(
404
,
error
));
if
(
error
)
return
next
(
new
HttpError
(
404
,
error
));
rm
(
absoluteFilePath
,
function
(
error
,
result
)
{
if
(
result
.
isDirectory
()
&&
!
recursive
)
return
next
(
new
HttpError
(
403
,
'Is directory'
));
if
(
error
)
return
next
(
new
HttpError
(
500
,
'Unable to remove'
));
// 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
}));
next
(
new
HttpSuccess
(
200
,
{
entries
:
result
}));
},
function
(
error
)
{
console
.
error
(
error
);
next
(
new
HttpError
(
500
,
'Unable to remove'
));
});
});
});
});
}
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment