-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
74 lines (64 loc) · 2.09 KB
/
index.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
#! /usr/bin/env node
const config = require('./config');
const importer = require('./importer');
const minimist = require('minimist');
const progress = require('progress');
const fs = require('fs');
const TXT_USAGE = `
Usage:
csv2influx init Creates template config file
csv2influx [options] data.csv Imports file data.csv to your influxDB
Options:
-c, --config /path/to/config.json [optional] Specifies path to your config file.
Default: ./csv2influx.conf.json
-q, --quiet [optional] Makes output quiet (progress bar instead of written to DB values)
`
var args = minimist(process.argv.slice(2), {
string: ['config'],
boolean: ['quiet'],
alias: { q: 'quiet', c: 'config' },
default: {
config: 'csv2influx.conf.json'
}
});
if(args._.length > 0) {
if(args._[0] === 'init') {
config.initConfig();
process.exit(0);
} else {
var conf = config.loadConfig(args.config);
var inputFile = args._[0];
var progressBar = undefined;
if (!fs.existsSync(inputFile)) {
console.error(`${inputFile} doesn't exist. Can't continue.`);
process.exit(1);
}
// TODO: make it possible to run the script without cutting lines
// TODO: maybe count progress by size of the file and current offset in the file
importer.countFileLines(inputFile)
.then(linesCount => {
console.log('lines count:' + linesCount);
if(args.quiet) {
progressBar = new progress(':current/:total (:percent) :bar ', {
width: 100,
total: linesCount
});
}
// TODO: don't put progressBar, but create an iterator
var imp = new importer.Importer(conf, inputFile, progressBar);
imp.run()
.then(() => console.log(''))
.catch(err => {
if(progressBar) {
progressBar.terminate();
}
console.error(err);
process.exit(1);
});
})
.catch(err => console.error(err));
}
} else {
console.log(TXT_USAGE);
process.exit(0);
}