Skip to content

Commit

Permalink
change return type of string | undefined to string | null
Browse files Browse the repository at this point in the history
Signed-off-by: Kaden Emley <kemley@mitre.org>
  • Loading branch information
kemley76 committed Jul 31, 2024
1 parent 61e1dff commit a6b99b5
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions src/commands/generate/ckl_metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -24,23 +24,23 @@ 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})
}

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})
while (true) {
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})
}
Expand All @@ -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) {
Expand Down

0 comments on commit a6b99b5

Please sign in to comment.