From 9ed9cce86b7eb50dc7303a4b67cc795e532a1c38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Thu, 14 Nov 2024 18:48:47 +0100 Subject: [PATCH] Add CLI options --- src/cli/index.ts | 34 ++++++++++++++++++++++++++++++++++ src/index.ts | 8 ++------ src/types/cli.ts | 4 ++++ src/types/index.ts | 3 ++- 4 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 src/cli/index.ts create mode 100644 src/types/cli.ts diff --git a/src/cli/index.ts b/src/cli/index.ts new file mode 100644 index 0000000..cd76880 --- /dev/null +++ b/src/cli/index.ts @@ -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 ', 'sets the path to the YAML config file') + .requiredOption('-d, --date ', 'sets the date of measurement intended to be fetched (in YYYY-MM-DD format)') + .parse() + .opts(); + + 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); +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 308fe9c..d25b80d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); -} \ No newline at end of file +run(); \ No newline at end of file diff --git a/src/types/cli.ts b/src/types/cli.ts new file mode 100644 index 0000000..d8a4b7c --- /dev/null +++ b/src/types/cli.ts @@ -0,0 +1,4 @@ +export type CLIOptions = { + config: string; + date: string; +} \ No newline at end of file diff --git a/src/types/index.ts b/src/types/index.ts index b5e928b..8f3299b 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -3,4 +3,5 @@ export * from './consumer'; export * from './fetcher'; export * from './influxdb'; export * from './mqtt'; -export * from './tauron'; \ No newline at end of file +export * from './tauron'; +export * from './cli'; \ No newline at end of file