From 80f92acb0caf6373f7377d85a70536ab2d3a5954 Mon Sep 17 00:00:00 2001 From: nick Date: Thu, 28 Dec 2023 16:35:36 +0900 Subject: [PATCH] feat: read also from file path for bulk commands --- cli/src/cli-types.ts | 20 ++++++++++++-------- cli/src/datafeed.ts | 17 +++++++++-------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/cli/src/cli-types.ts b/cli/src/cli-types.ts index 4fb836514..a3e9df4b5 100644 --- a/cli/src/cli-types.ts +++ b/cli/src/cli-types.ts @@ -3,18 +3,22 @@ import { existsSync } from 'node:fs' import { CliError, CliErrorCode } from './errors' import { isValidUrl, loadFile, loadJsonFromUrl } from './utils' +export async function readFileFromSource(source: string): Promise { + if (await isValidUrl(source)) { + return await loadJsonFromUrl(source) + } else { + if (!existsSync(source)) { + throw new CliError(CliErrorCode.FileNotFound) + } + return JSON.parse((await loadFile(source)).toString()) + } +} + // ReadFile function is to load json file from // url-link, or from local file directory export const ReadFile: Type = { async from(source) { - if (await isValidUrl(source)) { - return await loadJsonFromUrl(source) - } else { - if (!existsSync(source)) { - throw new CliError(CliErrorCode.FileNotFound) - } - return JSON.parse((await loadFile(source)).toString()) - } + return await readFileFromSource(source) } } diff --git a/cli/src/datafeed.ts b/cli/src/datafeed.ts index 43a7b9194..de0400041 100644 --- a/cli/src/datafeed.ts +++ b/cli/src/datafeed.ts @@ -10,7 +10,8 @@ import { IAggregator, IDatafeedBulk, IDatafeedBulkInsertElement, - ReadFile + ReadFile, + readFileFromSource } from './cli-types' import { contractConnectHandler, @@ -50,7 +51,7 @@ import { WORKER_SERVICE_HOST, WORKER_SERVICE_PORT } from './settings' -import { isValidUrl, loadJsonFromUrl } from './utils' +import { isValidUrl } from './utils' export function datafeedSub() { // datafeed bulk-insert --source ${source} @@ -128,12 +129,12 @@ export function bulkInsertHandler() { for (const insertElement of bulkData.bulk) { console.log(`inserting ${insertElement}`) - const adapterData = await loadJsonFromUrl(insertElement.adapterSource) + const adapterData = await readFileFromSource(insertElement.adapterSource) if (!checkAdapterSource(adapterData)) { console.error(`invalid adapterData: ${adapterData}, skipping insert`) continue } - const aggregatorData = await loadJsonFromUrl(insertElement.aggregatorSource) + const aggregatorData = await readFileFromSource(insertElement.aggregatorSource) if (!checkAggregatorSource(aggregatorData)) { console.error(`invalid aggregatorData: ${aggregatorData}, skipping insert`) continue @@ -193,12 +194,12 @@ export function bulkRemoveHandler() { for (const removeElement of bulkData.bulk) { console.log(`removing ${removeElement}`) - const adapterData = await loadJsonFromUrl(removeElement.adapterSource) + const adapterData = await readFileFromSource(removeElement.adapterSource) if (!checkAdapterSource(adapterData)) { console.error(`invalid adapterData: ${adapterData}, skipping removal`) continue } - const aggregatorData = await loadJsonFromUrl(removeElement.aggregatorSource) + const aggregatorData = await readFileFromSource(removeElement.aggregatorSource) if (!checkAggregatorSource(aggregatorData)) { console.error(`invalid aggregatorData: ${aggregatorData}, skipping removal`) continue @@ -260,7 +261,7 @@ export function bulkActivateHandler() { } for (const activateElement of data.bulk) { - const aggregatorData = await loadJsonFromUrl(activateElement.aggregatorSource) + const aggregatorData = await readFileFromSource(activateElement.aggregatorSource) if (!checkAggregatorSource(aggregatorData)) { console.error(`invalid aggregatorData: ${aggregatorData}, skipping activation`) continue @@ -338,7 +339,7 @@ export function bulkDeactivateHandler() { } for (const deactivateElement of data.bulk) { - const aggregatorData = await loadJsonFromUrl(deactivateElement.aggregatorSource) + const aggregatorData = await readFileFromSource(deactivateElement.aggregatorSource) if (!checkAggregatorSource(aggregatorData)) { console.error(`invalid aggregatorData: ${aggregatorData}, skipping deactivation`) continue