-
Notifications
You must be signed in to change notification settings - Fork 0
/
nuke
executable file
·69 lines (51 loc) · 1.95 KB
/
nuke
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
#!/usr/bin/env node
var dbi = require('./lib/dbi'), // DB interface
qa = require('./lib/io'), // Q&A prompt
fs = require('fs'),
async = require('async');
var question = function(q, options, abort) {
return function(callback) {
qa(q, options, function(a) {
var err = abort ? (a == abort) : null;
callback(err, a);
});
};
};
async.series([
question('Are you sure you want to nuke the database?', ['n', 'y'], 'n'),
question('Really!?', ['n', 'yes'], 'n'),
question('Can I at least make a backup for you?', ['y', 'no'])
], function(abort, results) {
if (abort) {
console.log('Phew! That was close.');
} else {
async.series([
// Backup routine
// ...this could probably be done better :P
(function(makeBackup) {
if (makeBackup) {
return function(callback) {
// We know the file exists, because SQLite would have
// complained before now. So we copy the database file to
// '[timestamp].backup'.
var timestamp = new Date().getTime(),
backupName = 'data/' + timestamp + '.backup',
sourceFile = fs.createReadStream('data/stupidTrains.sqlite'),
backupFile = fs.createWriteStream(backupName);
sourceFile.on('error', function() { callback('Cannot perform backup - nuke aborted!', null); })
.on('end', function() { callback(null, 'Database backed up as ' + backupName); });
sourceFile.pipe(backupFile);
};
} else {
return function(callback) { callback(null, 'No backup requested.'); };
}
})(results[2] == 'y'),
dbi.nuke
], function(err, result) {
// Report outcome
console.log(result.filter(function(a) { return a != null; }).join('\n'));
if (err) console.error('Error: ' + err);
});
}
});
// vim: ft=javascript