From 8ab4d2907d940a12d029eb69afdb4817a917b751 Mon Sep 17 00:00:00 2001 From: Arthur Date: Wed, 8 Jan 2025 17:59:48 +0000 Subject: [PATCH] create parseCliPullOtArguments helper --- src/cli-pull-ot.ts | 116 +----------------------- src/oneTrust/index.ts | 1 + src/oneTrust/parseCliPullOtArguments.ts | 114 +++++++++++++++++++++++ 3 files changed, 118 insertions(+), 113 deletions(-) create mode 100644 src/oneTrust/parseCliPullOtArguments.ts diff --git a/src/cli-pull-ot.ts b/src/cli-pull-ot.ts index 9c2c76ef..7f67aaa0 100644 --- a/src/cli-pull-ot.ts +++ b/src/cli-pull-ot.ts @@ -1,126 +1,16 @@ #!/usr/bin/env node -import yargs from 'yargs-parser'; import { logger } from './logger'; import colors from 'colors'; import { getListOfAssessments, getAssessment, writeOneTrustAssessment, + parseCliPullOtArguments, } from './oneTrust'; -import { OneTrustFileFormat, OneTrustPullResource } from './enums'; +import { OneTrustPullResource } from './enums'; import { createOneTrustGotInstance } from './oneTrust/createOneTrustGotInstance'; import { mapSeries } from 'bluebird'; -const VALID_RESOURCES = Object.values(OneTrustPullResource); -const VALID_FILE_FORMATS = Object.values(OneTrustFileFormat); - -interface OneTrustCliArguments { - /** The name of the file to write the resources to without extensions */ - file: string; - /** The OneTrust hostname to send the requests to */ - hostname: string; - /** The OAuth Bearer token used to authenticate the requests */ - auth: string; - /** The resource to pull from OneTrust */ - resource: OneTrustPullResource; - /** Whether to enable debugging while reporting errors */ - debug: boolean; - /** The export format of the file where to save the resources */ - fileFormat: OneTrustFileFormat; -} - -/** - * Parse the command line arguments - * - * @returns the parsed arguments - */ -const parseCliArguments = (): OneTrustCliArguments => { - const { file, hostname, auth, resource, debug, fileFormat } = yargs( - process.argv.slice(2), - { - string: ['file', 'hostname', 'auth', 'resource', 'fileFormat'], - boolean: ['debug'], - default: { - resource: OneTrustPullResource.Assessments, - fileFormat: OneTrustFileFormat.Json, - }, - }, - ); - - if (!file) { - logger.error( - colors.red( - 'Missing required parameter "file". e.g. --file=./oneTrustAssessments.json', - ), - ); - return process.exit(1); - } - const splitFile = file.split('.'); - if (splitFile.length < 2) { - logger.error( - colors.red( - 'The "file" parameter has an invalid format. Expected a path with extensions. e.g. --file=./pathToFile.json.', - ), - ); - return process.exit(1); - } - if (splitFile.at(-1) !== fileFormat) { - logger.error( - colors.red( - `The "file" and "fileFormat" parameters must specify the same format! Got file=${file} and fileFormat=${fileFormat}`, - ), - ); - return process.exit(1); - } - - if (!hostname) { - logger.error( - colors.red( - 'Missing required parameter "hostname". e.g. --hostname=customer.my.onetrust.com', - ), - ); - return process.exit(1); - } - - if (!auth) { - logger.error( - colors.red( - 'Missing required parameter "auth". e.g. --auth=$ONE_TRUST_AUTH_TOKEN', - ), - ); - return process.exit(1); - } - if (!VALID_RESOURCES.includes(resource)) { - logger.error( - colors.red( - `Received invalid resource value: "${resource}". Allowed: ${VALID_RESOURCES.join( - ',', - )}`, - ), - ); - return process.exit(1); - } - if (!VALID_FILE_FORMATS.includes(fileFormat)) { - logger.error( - colors.red( - `Received invalid fileFormat value: "${fileFormat}". Allowed: ${VALID_FILE_FORMATS.join( - ',', - )}`, - ), - ); - return process.exit(1); - } - - return { - file, - hostname, - auth, - resource, - debug: debug === undefined ? false : debug, - fileFormat, - }; -}; - /** * Pull configuration from OneTrust down locally to disk * @@ -133,7 +23,7 @@ const parseCliArguments = (): OneTrustCliArguments => { */ async function main(): Promise { const { file, fileFormat, hostname, auth, resource, debug } = - parseCliArguments(); + parseCliPullOtArguments(); // Sync to Disk try { diff --git a/src/oneTrust/index.ts b/src/oneTrust/index.ts index afcbd68f..24486cc7 100644 --- a/src/oneTrust/index.ts +++ b/src/oneTrust/index.ts @@ -2,3 +2,4 @@ export * from './getListOfAssessments'; export * from './createOneTrustGotInstance'; export * from './getAssessment'; export * from './writeOneTrustAssessment'; +export * from './parseCliPullOtArguments'; diff --git a/src/oneTrust/parseCliPullOtArguments.ts b/src/oneTrust/parseCliPullOtArguments.ts new file mode 100644 index 00000000..b067eb38 --- /dev/null +++ b/src/oneTrust/parseCliPullOtArguments.ts @@ -0,0 +1,114 @@ +import { logger } from '../logger'; +import colors from 'colors'; +import yargs from 'yargs-parser'; +import { OneTrustFileFormat, OneTrustPullResource } from '../enums'; + +const VALID_RESOURCES = Object.values(OneTrustPullResource); +const VALID_FILE_FORMATS = Object.values(OneTrustFileFormat); + +interface OneTrustCliArguments { + /** The name of the file to write the resources to without extensions */ + file: string; + /** The OneTrust hostname to send the requests to */ + hostname: string; + /** The OAuth Bearer token used to authenticate the requests */ + auth: string; + /** The resource to pull from OneTrust */ + resource: OneTrustPullResource; + /** Whether to enable debugging while reporting errors */ + debug: boolean; + /** The export format of the file where to save the resources */ + fileFormat: OneTrustFileFormat; +} + +/** + * Parse the command line arguments + * + * @returns the parsed arguments + */ +export const parseCliPullOtArguments = (): OneTrustCliArguments => { + const { file, hostname, auth, resource, debug, fileFormat } = yargs( + process.argv.slice(2), + { + string: ['file', 'hostname', 'auth', 'resource', 'fileFormat'], + boolean: ['debug'], + default: { + resource: OneTrustPullResource.Assessments, + fileFormat: OneTrustFileFormat.Json, + }, + }, + ); + + if (!file) { + logger.error( + colors.red( + 'Missing required parameter "file". e.g. --file=./oneTrustAssessments.json', + ), + ); + return process.exit(1); + } + const splitFile = file.split('.'); + if (splitFile.length < 2) { + logger.error( + colors.red( + 'The "file" parameter has an invalid format. Expected a path with extensions. e.g. --file=./pathToFile.json.', + ), + ); + return process.exit(1); + } + if (splitFile.at(-1) !== fileFormat) { + logger.error( + colors.red( + `The "file" and "fileFormat" parameters must specify the same format! Got file=${file} and fileFormat=${fileFormat}`, + ), + ); + return process.exit(1); + } + + if (!hostname) { + logger.error( + colors.red( + 'Missing required parameter "hostname". e.g. --hostname=customer.my.onetrust.com', + ), + ); + return process.exit(1); + } + + if (!auth) { + logger.error( + colors.red( + 'Missing required parameter "auth". e.g. --auth=$ONE_TRUST_AUTH_TOKEN', + ), + ); + return process.exit(1); + } + if (!VALID_RESOURCES.includes(resource)) { + logger.error( + colors.red( + `Received invalid resource value: "${resource}". Allowed: ${VALID_RESOURCES.join( + ',', + )}`, + ), + ); + return process.exit(1); + } + if (!VALID_FILE_FORMATS.includes(fileFormat)) { + logger.error( + colors.red( + `Received invalid fileFormat value: "${fileFormat}". Allowed: ${VALID_FILE_FORMATS.join( + ',', + )}`, + ), + ); + return process.exit(1); + } + + return { + file, + hostname, + auth, + resource, + debug: debug === undefined ? false : debug, + fileFormat, + }; +};