From 21d0ba1d109604c17a76bac8a5bde29e5edfa0c7 Mon Sep 17 00:00:00 2001 From: instamenta Date: Fri, 24 Jan 2025 17:51:31 +0200 Subject: [PATCH] move stringifyArgv method to flags to avoid circular dep Signed-off-by: instamenta --- src/commands/flags.ts | 37 +++++++++++++++ .../config/remote/remote_config_manager.ts | 3 +- src/core/helpers.ts | 47 ++----------------- test/unit/core/helpers.test.ts | 6 +-- 4 files changed, 46 insertions(+), 47 deletions(-) diff --git a/src/commands/flags.ts b/src/commands/flags.ts index b84c3faf5..7d09b23de 100644 --- a/src/commands/flags.ts +++ b/src/commands/flags.ts @@ -24,6 +24,7 @@ import {IllegalArgumentError, SoloError} from '../core/errors.js'; import {ListrEnquirerPromptAdapter} from '@listr2/prompt-adapter-enquirer'; import * as helpers from '../core/helpers.js'; import validator from 'validator'; +import type {AnyObject} from '../types/aliases.js'; export class Flags { private static async prompt( @@ -1848,4 +1849,40 @@ export class Flags { requiredFlagsWithDisabledPrompt: [Flags.namespace, Flags.cacheDir, Flags.releaseTag], optionalFlags: [Flags.devMode, Flags.quiet], }; + + static stringifyArgv(argv: AnyObject): string { + const processedFlags: string[] = []; + + for (const [name, value] of Object.entries(argv)) { + // Remove non-flag data and boolean presence based flags that are false + if (name === '_' || name === '$0' || value === '' || value === false || value === undefined || value === null) { + continue; + } + + // remove flags that use the default value + const flag = Flags.allFlags.find(flag => flag.name === name); + if (!flag || (flag.definition.defaultValue && flag.definition.defaultValue === value)) { + continue; + } + + const flagName = flag.name; + + // if the flag is boolean based, render it without value + if (value === true) { + processedFlags.push(`--${flagName}`); + } + + // if the flag's data is masked, display it without the value + else if (flag.definition.dataMask) { + processedFlags.push(`--${flagName} ${flag.definition.dataMask}`); + } + + // else display the full flag data + else { + processedFlags.push(`--${flagName} ${value}`); + } + } + + return processedFlags.join(' '); + } } diff --git a/src/core/config/remote/remote_config_manager.ts b/src/core/config/remote/remote_config_manager.ts index d8ffb955d..4dfafc31b 100644 --- a/src/core/config/remote/remote_config_manager.ts +++ b/src/core/config/remote/remote_config_manager.ts @@ -35,7 +35,6 @@ import {StatusCodes} from 'http-status-codes'; import {inject, injectable} from 'tsyringe-neo'; import {patchInject} from '../../container_helper.js'; import {ErrorMessages} from '../../error_messages.js'; -import {stringifyArgv} from '../../helpers.js'; /** * Uses Kubernetes ConfigMaps to manage the remote configuration data by creating, loading, modifying, @@ -206,7 +205,7 @@ export class RemoteConfigManager { await RemoteConfigValidator.validateComponents(self.remoteConfig.components, self.k8); const currentCommand = argv._.join(' '); - const commandArguments = stringifyArgv(argv); + const commandArguments = flags.stringifyArgv(argv); self.remoteConfig!.addCommandToHistory((currentCommand + ' ' + commandArguments).trim()); diff --git a/src/core/helpers.ts b/src/core/helpers.ts index 54e335e94..201b5109c 100644 --- a/src/core/helpers.ts +++ b/src/core/helpers.ts @@ -23,12 +23,11 @@ import {Templates} from './templates.js'; import {ROOT_DIR} from './constants.js'; import * as constants from './constants.js'; import {PrivateKey, ServiceEndpoint} from '@hashgraph/sdk'; -import {type AnyObject, type NodeAlias, type NodeAliases} from '../types/aliases.js'; -import {type CommandFlag} from '../types/flag_types.js'; -import {type SoloLogger} from './logging.js'; -import {type Duration} from './time/duration.js'; -import {type NodeAddConfigClass} from '../commands/node/node_add_config.js'; -import {Flags as flags} from '../commands/flags.js'; +import type {NodeAlias, NodeAliases} from '../types/aliases.js'; +import type {CommandFlag} from '../types/flag_types.js'; +import type {SoloLogger} from './logging.js'; +import type {Duration} from './time/duration.js'; +import type {NodeAddConfigClass} from '../commands/node/node_add_config.js'; export function sleep(duration: Duration) { return new Promise(resolve => { @@ -385,39 +384,3 @@ export function resolveValidJsonFilePath(filePath: string, defaultPath?: string) throw new SoloError(`Invalid JSON data in file: ${filePath}`); } } - -export function stringifyArgv(argv: AnyObject): string { - const processedFlags: string[] = []; - - for (const [name, value] of Object.entries(argv)) { - // Remove non-flag data and boolean presence based flags that are false - if (name === '_' || name === '$0' || value === '' || value === false || value === undefined || value === null) { - continue; - } - - // remove flags that use the default value - const flag = flags.allFlags.find(flag => flag.name === name); - if (!flag || (flag.definition.defaultValue && flag.definition.defaultValue === value)) { - continue; - } - - const flagName = flag.name; - - // if the flag is boolean based, render it without value - if (value === true) { - processedFlags.push(`--${flagName}`); - } - - // if the flag's data is masked, display it without the value - else if (flag.definition.dataMask) { - processedFlags.push(`--${flagName} ${flag.definition.dataMask}`); - } - - // else display the full flag data - else { - processedFlags.push(`--${flagName} ${value}`); - } - } - - return processedFlags.join(' '); -} diff --git a/test/unit/core/helpers.test.ts b/test/unit/core/helpers.test.ts index 625c6814a..9984c4104 100644 --- a/test/unit/core/helpers.test.ts +++ b/test/unit/core/helpers.test.ts @@ -49,19 +49,19 @@ describe('Helpers', () => { it('Should parse argv to args with datamask correctly', () => { const argv = {[flags.googleCredential.name]: 'VALUE'}; - const result = helpers.stringifyArgv(argv); + const result = flags.stringifyArgv(argv); expect(result).to.equal(`--${flags.googleCredential.name} ${flags.googleCredential.definition.dataMask}`); }); it('Should parse argv to args with boolean flag correctly', () => { const argv = {[flags.quiet.name]: true}; - const result = helpers.stringifyArgv(argv); + const result = flags.stringifyArgv(argv); expect(result).to.equal(`--${flags.quiet.name}`); }); it('Should parse argv to args with flag correctly', () => { const argv = {[flags.namespace.name]: 'VALUE'}; - const result = helpers.stringifyArgv(argv); + const result = flags.stringifyArgv(argv); expect(result).to.equal(`--${flags.namespace.name} VALUE`); }); });