-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
82 lines (74 loc) · 2.28 KB
/
cli.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
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env node
const fs = require('fs');
const {program} = require('commander');
const retrieve = require('.');
program
.option('-b <bands>', 'bands to extract, comma-separated list of strings and/or numbers')
.option(
'-w <win>',
'window to extract, comma-separated list of WGS84 coordinates in left, top, right, bottom order'
)
.option('-j <file>', 'read configuration from a JSON file')
.option('-v', 'verbose output')
.option('-q', 'suppress all output')
.parse();
// Replace all /strings/ with a RegExp object
function createRegexp(str) {
if (typeof str === 'string' && str.startsWith('/') && str.endsWith('/'))
return new RegExp(str.substring(0, str.length - 1).substring(1));
return str;
}
function createAllRegexps(o) {
for (const key of Object.keys(o))
if (typeof o[key] === 'object') createAllRegexps(o[key]);
else if (typeof o[key] === 'string') o[key] = createRegexp(o[key]);
}
const opts = program.opts();
// Parse the JSON configuration file
let conf, bands, bbox;
if (opts.j) {
try {
conf = JSON.parse(fs.readFileSync(opts.j));
if (conf.bands) createAllRegexps(conf.bands);
bands = conf.bands;
bbox = conf.bbox;
} catch (e) {
console.error(`Failed parsing ${opts.j}: ${e}`);
process.exit(1);
}
}
// Parse all other CLI options
if (opts.b) {
bands = opts.b.split(',').map((band) => {
if (!isNaN(band)) return {id: +band};
if (band.startsWith('/') && band.endsWith('/'))
return {description: createRegexp(band)};
return {description: band};
});
}
if (opts.w) {
bbox = opts.w.split(',').map((coord) => +coord);
}
const url = program.args[0];
const filename = program.args[1];
// Download
retrieve({
url,
filename,
bands,
bbox,
verbose: opts.q ? () => undefined : opts.v ? console.debug : () => process.stdout.write('.')
})
.then(() => {
if (!opts.quiet) process.stdout.write('\n');
})
.catch((e) => {
if (!opts.q) {
console.error(e.message);
console.log('Usage:');
console.log(
'geosub [-b band1,band2...] [-w left,top,right,bottom] [-v] [-q] url destination'
);
}
process.exit(1);
});