forked from rjcorwin/CouchDB-Wedge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wedge-push-json.js
executable file
·69 lines (55 loc) · 1.6 KB
/
wedge-push-json.js
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 program = require('commander');
var pushJson = require('./lib/PushJson')
var fs = require('fs')
function puts(error, stdout, stderr) { sys.puts(stdout); sys.puts(stderr); sys.puts(error) }
program
.option('-u, --url <url>', 'The URL of where to push documents to.', '')
.option('-f, --file <path>', 'Path to a file of JSON data.', '')
.option('--verbose', 'Verbose mode')
program.on('--help', function(){
process.stdout.write(' Examples:')
process.stdout.write('')
process.stdout.write(' $ wedge push-json --file data.json --url http://username:password@foo.tangerinecentral.org/group-testsweet')
process.stdout.write('')
});
program.parse(process.argv);
if (program.file) {
program.json = JSON.parse(fs.readFileSync(program.file, 'utf8'))
pushJson(program, function(err, res) {
if(err) {
process.stderr.write(err)
process.stderr.write(res)
process.exit(1)
}
else {
process.stdout.write(JSON.stringify(res))
process.exit(0)
}
})
}
else {
var stdin = process.stdin,
stdout = process.stdout,
inputChunks = [];
stdin.resume();
stdin.setEncoding('utf8');
stdin.on('data', function (chunk) {
inputChunks.push(chunk);
});
stdin.on('end', function () {
program.json = inputChunks.join()
program.json = JSON.parse(program.json)
pushJson(program, function(err, res) {
if(err) {
process.stderr.write(err)
process.stderr.write(res)
process.exit(1)
}
else {
process.stdout.write(JSON.stringify(res))
process.exit(0)
}
})
});
}