Add CLI options

This commit is contained in:
2024-11-14 18:48:47 +01:00
parent d69aaa50e0
commit 9ed9cce86b
4 changed files with 42 additions and 7 deletions

34
src/cli/index.ts Normal file
View 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);
}

View File

@@ -3,13 +3,9 @@
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";
import { parseConfig } from "./config";
import { run } from "./cli";
dayjs.extend(utc);
dayjs.extend(timezone);
const config = parseConfig('./config.yaml');
if (config.timezone) {
dayjs.tz.setDefault(config.timezone);
}
run();

4
src/types/cli.ts Normal file
View File

@@ -0,0 +1,4 @@
export type CLIOptions = {
config: string;
date: string;
}

View File

@@ -3,4 +3,5 @@ export * from './consumer';
export * from './fetcher';
export * from './influxdb';
export * from './mqtt';
export * from './tauron';
export * from './tauron';
export * from './cli';