import * as fs from 'fs' ; import * as core from '@actions/core' ; import { TrivyCmdOption , Validator } from './interface' ; export class TrivyCmdOptionValidator implements Validator { option : TrivyCmdOption ; constructor ( option ) { this . option = option ; } validate ( ) { this . validateSeverity ( ) ; this . validateVulnType ( ) ; this . validateTemplate ( ) ; } private validateSeverity ( ) { const severities = this . option . severity . split ( ',' ) ; const allowedSeverities = /UNKNOWN|LOW|MEDIUM|HIGH|CRITICAL/ ; if ( ! this . validateArrayOption ( allowedSeverities , severities ) ) { throw new Error ( `Trivy option error: ${ severities . join ( ',' ) } is unknown severity.
        Trivy supports UNKNOWN, LOW, MEDIUM, HIGH and CRITICAL.` ) ; } } private validateVulnType ( ) { const vulnTypes = this . option . vulnType . split ( ',' ) ; const allowedVulnTypes = /os|library/ ; if ( ! this . validateArrayOption ( allowedVulnTypes , vulnTypes ) ) { throw new Error ( `Trivy option error: ${ vulnTypes . join ( ',' ) } is unknown vuln-type.
        Trivy supports os and library.` ) ; } } private validateArrayOption ( allowedValue , options ) { for ( const option of options ) { if ( ! allowedValue . test ( option ) ) { return false ; } } return true ; } private validateTemplate ( ) { if ( ! this . option . template . startsWith ( '@' ) ) { core . warning ( 'template parameter prefix must be "@"' ) ; this . option . template = `@${ this . option . template }` ; } const template = this . option . template . replace ( '@' , '' ) ; const exists = fs . existsSync ( template ) ; if ( ! exists ) { throw new Error ( `Could not find ${ template }` ) ; } const isFile = fs . statSync ( template ) . isFile ( ) ; if ( ! isFile ) { throw new Error ( `${ template } is not a file` ) ; } } }