Skip to content

Commit

Permalink
create parseCliPullOtArguments helper
Browse files Browse the repository at this point in the history
  • Loading branch information
abrantesarthur committed Jan 8, 2025
1 parent 71c3f02 commit 8ab4d29
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 113 deletions.
116 changes: 3 additions & 113 deletions src/cli-pull-ot.ts
Original file line number Diff line number Diff line change
@@ -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
*
Expand All @@ -133,7 +23,7 @@ const parseCliArguments = (): OneTrustCliArguments => {
*/
async function main(): Promise<void> {
const { file, fileFormat, hostname, auth, resource, debug } =
parseCliArguments();
parseCliPullOtArguments();

// Sync to Disk
try {
Expand Down
1 change: 1 addition & 0 deletions src/oneTrust/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './getListOfAssessments';
export * from './createOneTrustGotInstance';
export * from './getAssessment';
export * from './writeOneTrustAssessment';
export * from './parseCliPullOtArguments';
114 changes: 114 additions & 0 deletions src/oneTrust/parseCliPullOtArguments.ts
Original file line number Diff line number Diff line change
@@ -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,
};
};

0 comments on commit 8ab4d29

Please sign in to comment.