From 6a241069791fe98f861e6011b47696dd86b746e7 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Fri, 7 Oct 2022 11:21:56 -0600 Subject: [PATCH] feat: support varargs --- messages/messages.md | 8 ++++++++ src/exported.ts | 2 +- src/util.ts | 36 ++++++++++++++++++++++++++++++++++++ test/unit/util.test.ts | 31 ++++++++++++++++++++++++++++++- 4 files changed, 75 insertions(+), 2 deletions(-) diff --git a/messages/messages.md b/messages/messages.md index 2326347c4..405076fc1 100644 --- a/messages/messages.md +++ b/messages/messages.md @@ -73,3 +73,11 @@ Try this: # warning.CommandInBeta This command is currently in beta. Any aspect of this command can change without advanced notice. Don't use beta commands in your scripts. + +# error.InvalidArgumentFormat + +Set varargs with this format: key=value or key="value with spaces". + +# error.DuplicateArgument + +Found duplicate argument %s. diff --git a/src/exported.ts b/src/exported.ts index 8372c8238..446d1808d 100644 --- a/src/exported.ts +++ b/src/exported.ts @@ -7,7 +7,7 @@ import { Flags as OclifFlags } from '@oclif/core'; -export { toHelpSection } from './util'; +export { toHelpSection, parseVarArgs } from './util'; export { Deployable, Deployer, DeployerResult } from './deployer'; export { Deauthorizer } from './deauthorizer'; export { Progress, Prompter, generateTableChoices } from './ux'; diff --git a/src/util.ts b/src/util.ts index 9bf61d44b..56f1c2cc6 100644 --- a/src/util.ts +++ b/src/util.ts @@ -12,8 +12,12 @@ import { SfdxPropertyKeys, SFDX_ALLOWED_PROPERTIES, SUPPORTED_ENV_VARS, + Messages, } from '@salesforce/core'; +Messages.importMessagesDirectory(__dirname); +const messages = Messages.loadMessages('@salesforce/sf-plugins-core', 'messages'); + export type HelpSection = { header: string; body: Array<{ name: string; description: string } | undefined>; @@ -59,3 +63,35 @@ export function toHelpSection( .filter((b) => b); return { header, body }; } + +export function parseVarArgs(args: Record, argv: string[]): Record { + const final: Record = {}; + const argVals = Object.values(args); + + // Remove arguments from varargs + const varargs = argv.filter((val) => !argVals.includes(val)); + + // Support `config set key value` + if (varargs.length === 2 && !varargs[0].includes('=')) { + return { [varargs[0]]: varargs[1] }; + } + + // Ensure that all args are in the right format (e.g. key=value key1=value1) + varargs.forEach((arg) => { + const split = arg.split('='); + + if (split.length !== 2) { + throw messages.createError('error.InvalidArgumentFormat', [arg]); + } + + const [name, value] = split; + + if (final[name]) { + throw messages.createError('error.DuplicateArgument', [name]); + } + + final[name] = value || undefined; + }); + + return final; +} diff --git a/test/unit/util.test.ts b/test/unit/util.test.ts index d9b982d3a..95e163c99 100644 --- a/test/unit/util.test.ts +++ b/test/unit/util.test.ts @@ -14,7 +14,7 @@ import { SfdxPropertyKeys, SFDX_ALLOWED_PROPERTIES, } from '@salesforce/core'; -import { toHelpSection } from '../../src/util'; +import { parseVarArgs, toHelpSection } from '../../src/util'; describe('toHelpSection', () => { it('should produce help section for env vars', () => { @@ -86,3 +86,32 @@ describe('toHelpSection', () => { }); }); }); + +describe('parseVarArgs', () => { + it('should parse varargs', () => { + const varargs = parseVarArgs({}, ['key1=value1']); + expect(varargs).to.deep.equal({ key1: 'value1' }); + }); + + it('should parse varargs and not arguments', () => { + const varargs = parseVarArgs({ arg1: 'foobar' }, ['foobar', 'key1=value1']); + expect(varargs).to.deep.equal({ key1: 'value1' }); + }); + + it('should parse single set of varargs', () => { + const varargs = parseVarArgs({ arg1: 'foobar' }, ['foobar', 'key1', 'value1']); + expect(varargs).to.deep.equal({ key1: 'value1' }); + }); + + it('should throw if invalid format', () => { + expect(() => parseVarArgs({ arg1: 'foobar' }, ['foobar', 'key1=value1', 'key2:value2'])).to.throw( + 'Set varargs with this format: key=value or key="value with spaces". key2:value2' + ); + }); + + it('should throw if duplicates exist', () => { + expect(() => parseVarArgs({ arg1: 'foobar' }, ['foobar', 'key1=value1', 'key1=value1'])).to.throw( + 'Found duplicate argument key1.' + ); + }); +});