From a6b99b5dcf721a97f04d45f7cb2e5b9612ffed96 Mon Sep 17 00:00:00 2001 From: Kaden Emley Date: Wed, 31 Jul 2024 11:24:36 -0400 Subject: [PATCH] change return type of string | undefined to string | null Signed-off-by: Kaden Emley --- src/commands/generate/ckl_metadata.ts | 50 +++++++++++++-------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/commands/generate/ckl_metadata.ts b/src/commands/generate/ckl_metadata.ts index b30da1d65..3239473ee 100644 --- a/src/commands/generate/ckl_metadata.ts +++ b/src/commands/generate/ckl_metadata.ts @@ -8,14 +8,14 @@ const prompt = promptSync() // Ensures that no empty strings are passed into the metadata // by returning undefined instead -function enforceNonEmptyString(ask: string) : string | undefined { +function enforceNonEmptyString(ask: string) : string | null { const response = prompt({ask}) if (response) return response - return undefined + return null } -function enforceInteger(ask: string): number | undefined { +function enforceInteger(ask: string): number | null { let response = prompt({ask}) let intRep: number while (true) { @@ -24,7 +24,7 @@ function enforceInteger(ask: string): number | undefined { if (intRep === floatRep && intRep >= 0 && !Number.isNaN(intRep)) break if (!response) - return + return null console.log(`${response} is not a valid non-negative integer. Please try again`) response = prompt({ask}) } @@ -32,7 +32,7 @@ function enforceInteger(ask: string): number | undefined { return intRep } -function enforceEnum(ask: string, options: string[]): string | undefined { +function enforceEnum(ask: string, options: string[]): string | null { // format prompt to show valid options (removes empty string options) ask = `${ask} (${options.filter(Boolean).join('/')}) ` let response = prompt({ask}) @@ -40,7 +40,7 @@ function enforceEnum(ask: string, options: string[]): string | undefined { if (options.includes(response)) break if (!response) - return + return null console.log(`${response} is not a valid option. Spelling and letter casing matters. Please try again.`) response = prompt({ask}) } @@ -66,30 +66,30 @@ export default class GenerateCKLMetadata extends Command { const cklMetadata = { profiles: [ { - name: enforceNonEmptyString('What is the benchmark name? (Must match with profile name listed in HDF) '), - title: enforceNonEmptyString('What is the benchmark title? '), - version: enforceInteger('What is the benchmark version? '), - releasenumber: enforceInteger('What is the benchmark release number? '), - releasedate: enforceNonEmptyString('What is the benchmark release date (YYYY/MM/DD)? '), + name: enforceNonEmptyString('What is the benchmark name? (Must match with profile name listed in HDF) ') || undefined, + title: enforceNonEmptyString('What is the benchmark title? ') || undefined, + version: enforceInteger('What is the benchmark version? ') || undefined, + releasenumber: enforceInteger('What is the benchmark release number? ') || undefined, + releasedate: enforceNonEmptyString('What is the benchmark release date (YYYY/MM/DD)? ') || undefined, showCalendar: true, }, ], - marking: enforceNonEmptyString('What is the marking? '), - hostname: enforceNonEmptyString('What is the asset hostname? '), - hostip: enforceNonEmptyString('What is the asset IP address? '), - hostmac: enforceNonEmptyString('What is the asset MAC address? '), - hostfqdn: enforceNonEmptyString('What is the asset FQDN? '), - targetcomment: enforceNonEmptyString('What are the target comments? '), - role: enforceEnum('What is the computing role?', Object.values(Role)), - assettype: enforceEnum('What is the asset type?', Object.values(Assettype)), + marking: enforceNonEmptyString('What is the marking? ') || undefined, + hostname: enforceNonEmptyString('What is the asset hostname? ') || undefined, + hostip: enforceNonEmptyString('What is the asset IP address? ') || undefined, + hostmac: enforceNonEmptyString('What is the asset MAC address? ') || undefined, + hostfqdn: enforceNonEmptyString('What is the asset FQDN? ') || undefined, + targetcomment: enforceNonEmptyString('What are the target comments? ') || undefined, + role: enforceEnum('What is the computing role?', Object.values(Role)) || undefined, + assettype: enforceEnum('What is the asset type?', Object.values(Assettype)) || undefined, // Resulting techarea options have typos. Yes, these typos really are how the enumerations are defined in STIG viewer's source code - techarea: enforceEnum('What is the tech area? ', Object.values(Techarea)), - stigguid: enforceNonEmptyString('What is the STIG ID? '), - targetkey: enforceNonEmptyString('What is the target key? '), + techarea: enforceEnum('What is the tech area? ', Object.values(Techarea)) || undefined, + stigguid: enforceNonEmptyString('What is the STIG ID? ') || undefined, + targetkey: enforceNonEmptyString('What is the target key? ') || undefined, webordatabase: String(enforceEnum('Is the target a web or database?', ['y', 'n']) === 'y'), - webdbsite: enforceNonEmptyString('What is the Web or DB site? '), - webdbinstance: enforceNonEmptyString('What is the Web or DB instance? '), - vulidmapping: enforceEnum('Use gid or id for vuln number?', ['gid', 'id']), + webdbsite: enforceNonEmptyString('What is the Web or DB site? ') || undefined, + webdbinstance: enforceNonEmptyString('What is the Web or DB instance? ') || undefined, + vulidmapping: enforceEnum('Use gid or id for vuln number?', ['gid', 'id']) || undefined, } const validationResults = validateChecklistMetadata(cklMetadata as ChecklistMetadata) if (validationResults.ok) {