/
mrmdir
executable file
·112 lines (92 loc) · 2.54 KB
/
mrmdir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env node
// -*- mode: js -*-
/*
* Copyright 2018 Joyent, Inc.
*/
var assert = require('assert-plus');
var f = require('util').format;
var path = require('path');
var bunyan = require('bunyan');
var dashdash = require('dashdash');
var manta = require('../lib');
///--- Functions
function optionsParser(name) {
var parser = dashdash.createParser({
options: manta.DEFAULT_CLI_OPTIONS.concat([
{
group: name + ' options'
},
{
names: ['interactive', 'I'],
type: 'bool',
help: 'confirm before deleting directories'
}
])
});
return (parser);
}
function ifError(err) {
if (err) {
console.error('mrmdir: ' + err.toString());
process.exit(1);
}
}
///--- Mainline
(function main() {
var argTypes = ['mdir'];
var name = path.basename(process.argv[1]);
var log = bunyan.createLogger({
name: name,
level: (process.env.LOG_LEVEL || 'info'),
stream: process.stderr
});
var parser = optionsParser(name);
var parseArgs = {
name: name,
parser: parser,
argTypes: argTypes,
log: log
};
var options = manta.parseOptions(parseArgs);
var client = manta.createBinClient(options);
var i = -1;
if (options.interactive && !process.stdin.isTTY) {
console.error('stdin must be a tty when interactive specified');
process.exit(1);
}
(function rmdir(cb_err) {
ifError(cb_err);
var p = options.paths[++i];
if (!p) {
client.close();
return;
}
client.info(p, function (err, info) {
if (err) {
client.get(p, function (err2) {
ifError(err2);
// We *shouldn't* ever hit this...
ifError(err);
});
return;
}
if (info.extension !== 'directory') {
console.error('%s is not a directory', p);
process.exit(1);
}
if (!options.interactive) {
client.unlink(p, rmdir);
return;
}
manta.promptConfirm(f('remove directory `%s` [y/N]?: ', p),
function (ans) {
if (!ans) {
// skip removing this file
rmdir();
return;
}
client.unlink(p, rmdir);
});
});
})();
})();