From 6d99f48a7e613321ff48bf3bff9eef7df7e69803 Mon Sep 17 00:00:00 2001 From: Jason Spafford Date: Wed, 13 Nov 2024 15:45:06 -0800 Subject: [PATCH] Add new date flag and use it in transactions cmd --- .../src/commands/wallet/transactions/index.ts | 15 +++++---------- ironfish-cli/src/flags.ts | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/ironfish-cli/src/commands/wallet/transactions/index.ts b/ironfish-cli/src/commands/wallet/transactions/index.ts index 98e21039f2..31a34615c3 100644 --- a/ironfish-cli/src/commands/wallet/transactions/index.ts +++ b/ironfish-cli/src/commands/wallet/transactions/index.ts @@ -14,7 +14,7 @@ import { } from '@ironfish/sdk' import { Flags } from '@oclif/core' import { IronfishCommand } from '../../../command' -import { RemoteFlags } from '../../../flags' +import { DateFlag, RemoteFlags } from '../../../flags' import * as ui from '../../../ui' import { getAssetsByIDs, useAccount } from '../../../utils' import { extractChainportDataFromTransaction } from '../../../utils/chainport' @@ -75,13 +75,11 @@ export class TransactionsCommand extends IronfishCommand { options: ['notes', 'transactions', 'transfers'], helpGroup: 'OUTPUT', }), - 'filter.start': Flags.string({ + 'filter.start': DateFlag({ description: 'include transactions after this date (inclusive). Example: 2023-04-01', - parse: (input) => Promise.resolve(new Date(input).toISOString()), }), - 'filter.end': Flags.string({ + 'filter.end': DateFlag({ description: 'include transactions before this date (exclusive). Example: 2023-05-01', - parse: (input) => Promise.resolve(new Date(input).toISOString()), }), } @@ -142,19 +140,16 @@ export class TransactionsCommand extends IronfishCommand { let hasTransactions = false let transactionRows: PartialRecursive[] = [] - const filterStart = flags['filter.start'] && new Date(flags['filter.start']).valueOf() - const filterEnd = flags['filter.end'] && new Date(flags['filter.end']).valueOf() - for await (const { account, transaction } of transactions) { if (transactionRows.length >= flags.limit) { break } - if (filterStart && transaction.timestamp < filterStart.valueOf()) { + if (flags['filter.start'] && transaction.timestamp < flags['filter.start'].valueOf()) { continue } - if (filterEnd && transaction.timestamp >= filterEnd.valueOf()) { + if (flags['filter.end'] && transaction.timestamp >= flags['filter.end'].valueOf()) { continue } diff --git a/ironfish-cli/src/flags.ts b/ironfish-cli/src/flags.ts index c1b0eaeab2..6f6c4b8c5e 100644 --- a/ironfish-cli/src/flags.ts +++ b/ironfish-cli/src/flags.ts @@ -212,3 +212,17 @@ export const EnumLanguageKeyFlag = Flags.custom({ + parse: async (input, _ctx, opts) => { + const parsed = new Date(input) + + if (Number.isNaN(parsed.valueOf())) { + throw new Error( + `The value provided for ${opts.name} is an invalid format. It must be a valid date.`, + ) + } + + return Promise.resolve(parsed) + }, +})