Skip to content

Commit

Permalink
move stringifyArgv method to flags to avoid circular dep
Browse files Browse the repository at this point in the history
Signed-off-by: instamenta <[email protected]>
  • Loading branch information
instamenta committed Jan 24, 2025
1 parent 27fffc5 commit 21d0ba1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 47 deletions.
37 changes: 37 additions & 0 deletions src/commands/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(' ');
}
}
3 changes: 1 addition & 2 deletions src/core/config/remote/remote_config_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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());

Expand Down
47 changes: 5 additions & 42 deletions src/core/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>(resolve => {
Expand Down Expand Up @@ -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(' ');
}
6 changes: 3 additions & 3 deletions test/unit/core/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
});

0 comments on commit 21d0ba1

Please sign in to comment.