Skip to content

Commit

Permalink
feat: read also from file path for bulk commands
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Dec 28, 2023
1 parent 3d562c7 commit 80f92ac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
20 changes: 12 additions & 8 deletions cli/src/cli-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {

Check warning on line 6 in cli/src/cli-types.ts

View workflow job for this annotation

GitHub Actions / build-publish

Unexpected any. Specify a different type
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<string, string> = {
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)
}
}

Expand Down
17 changes: 9 additions & 8 deletions cli/src/datafeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
IAggregator,
IDatafeedBulk,
IDatafeedBulkInsertElement,
ReadFile
ReadFile,
readFileFromSource
} from './cli-types'
import {
contractConnectHandler,
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 80f92ac

Please sign in to comment.