Add CLI options
This commit is contained in:
34
src/cli/index.ts
Normal file
34
src/cli/index.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import dayjs from "dayjs";
|
||||||
|
import { CLIOptions } from "@types";
|
||||||
|
import { program } from "commander";
|
||||||
|
import { parseConfig } from "../config";
|
||||||
|
import { Fetcher } from "../fetcher";
|
||||||
|
import { Tauron } from "../tauron";
|
||||||
|
|
||||||
|
export const run = () => {
|
||||||
|
const options = program
|
||||||
|
.name('tauron-scrapper')
|
||||||
|
.description('CLI tool which fetches the data from Tauron API')
|
||||||
|
.version('0.0.1')
|
||||||
|
.requiredOption('-c, --config <file>', 'sets the path to the YAML config file')
|
||||||
|
.requiredOption('-d, --date <date>', 'sets the date of measurement intended to be fetched (in YYYY-MM-DD format)')
|
||||||
|
.parse()
|
||||||
|
.opts<CLIOptions>();
|
||||||
|
|
||||||
|
const config = parseConfig(options.config);
|
||||||
|
|
||||||
|
if (config.timezone) {
|
||||||
|
dayjs.tz.setDefault(config.timezone);
|
||||||
|
}
|
||||||
|
|
||||||
|
const date = dayjs(options.date, 'YYYY-MM-DD');
|
||||||
|
|
||||||
|
if (!date.isValid) {
|
||||||
|
throw new Error(`Invalid date: ${options.date}, expected date to be of 'YYYY-MM-DD' format`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const tauron = new Tauron(config.tauron);
|
||||||
|
const fetcher = new Fetcher(config, tauron);
|
||||||
|
|
||||||
|
fetcher.fetch(date);
|
||||||
|
}
|
||||||
@@ -3,13 +3,9 @@
|
|||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
import utc from "dayjs/plugin/utc";
|
import utc from "dayjs/plugin/utc";
|
||||||
import timezone from "dayjs/plugin/timezone";
|
import timezone from "dayjs/plugin/timezone";
|
||||||
import { parseConfig } from "./config";
|
import { run } from "./cli";
|
||||||
|
|
||||||
dayjs.extend(utc);
|
dayjs.extend(utc);
|
||||||
dayjs.extend(timezone);
|
dayjs.extend(timezone);
|
||||||
|
|
||||||
const config = parseConfig('./config.yaml');
|
run();
|
||||||
|
|
||||||
if (config.timezone) {
|
|
||||||
dayjs.tz.setDefault(config.timezone);
|
|
||||||
}
|
|
||||||
4
src/types/cli.ts
Normal file
4
src/types/cli.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
export type CLIOptions = {
|
||||||
|
config: string;
|
||||||
|
date: string;
|
||||||
|
}
|
||||||
@@ -3,4 +3,5 @@ export * from './consumer';
|
|||||||
export * from './fetcher';
|
export * from './fetcher';
|
||||||
export * from './influxdb';
|
export * from './influxdb';
|
||||||
export * from './mqtt';
|
export * from './mqtt';
|
||||||
export * from './tauron';
|
export * from './tauron';
|
||||||
|
export * from './cli';
|
||||||
Reference in New Issue
Block a user