import XRegExp from 'xregexp' // Parse the log data const parseLog = (data: string) => { const patterns = [ XRegExp('^NAME:\\s+"(?.*)",\\s+DESCR:\\s+"(?.*)"'), XRegExp('^PID:\\s+(?[\\S+]+|.*),.*VID:\\s+(?.*),.*SN:\\s+(?[\\w+\\d+]*)'), XRegExp('^PID:\\s+,.*VID:\\s+(?.*),.*SN:\\s+(?[\\w+\\d+]*)'), XRegExp('^PID:\\s+(?[\\S+]+|.*),.*VID:\\s+(?.*),.*SN:'), XRegExp('^PID:\\s+(?\\S+)(?:,|\\s+)VID:\\s+(?\\S+)(?:,|\\s+)SN:\\s+(?\\w+)'), // License info XRegExp('^License Level:\\s+(?.*)'), XRegExp('^License Type:\\s+(?.*)'), XRegExp('^Next reload license Level:\\s+(?.*)'), ] const lines = data.split('\n') const licenseLog = data.split('show version | include License') let records: any = [] let currentRecord: any = { name: '', descr: '', pid: '', vid: '', sn: '', licenseLevel: '', licenseType: '', nextLicenseLevel: '', } for (const line of lines) { for (const pattern of patterns) { const match = XRegExp.exec(line, pattern) if (match) { const item = match?.groups // Update current record with matched fields Object.keys(currentRecord).forEach((key) => { if (item && item[key] !== undefined) { currentRecord[key] = item[key].trim() } }) // If "pid", "vid", or "sn" are matched, push a completed record if (currentRecord.pid || currentRecord.vid || currentRecord.sn) { records.push({ ...currentRecord }) currentRecord = { name: '', descr: '', pid: '', vid: '', sn: '', licenseLevel: '', licenseType: '', nextLicenseLevel: '', } // Reset for the next record } if ((currentRecord.licenseLevel || currentRecord.licenseType) && records.length > 0) { const value = records[0] value.licenseLevel = currentRecord.licenseLevel ? currentRecord.licenseLevel : value.licenseLevel value.licenseType = currentRecord.licenseType ? currentRecord.licenseType : value.licenseType value.nextLicenseLevel = currentRecord.nextLicenseLevel ? currentRecord.nextLicenseLevel : value.nextLicenseLevel records = [value] currentRecord = { name: '', descr: '', pid: '', vid: '', sn: '', licenseLevel: '', licenseType: '', nextLicenseLevel: '', } // Reset for the next record } break // Stop checking other patterns for this line } } } if (records.length > 0) { let extend = null const firstRecord = records[0] // check license and last 2 chars of pid if ( firstRecord.licenseLevel && typeof firstRecord.licenseLevel === 'string' && firstRecord.pid.length >= 2 ) { switch (firstRecord.licenseLevel.toLowerCase()) { case 'network essentials': case 'network-essentials': case 'dna essentials': case 'dna-essentials': extend = '-E' break case 'network advantage': case 'network-advantage': case 'dna advantage': case 'dna-advantage': extend = '-A' break default: break } } if (licenseLog[1] && !firstRecord.licenseLevel) { if (licenseLog[1]?.includes('essentials')) { extend = '-E' firstRecord.licenseLevel = 'network-essentials' firstRecord.licenseType = 'Smart License' } else if (licenseLog[1]?.includes('advantage')) { extend = '-A' firstRecord.licenseLevel = 'network-advantage' firstRecord.licenseType = 'Smart License' } } if (extend) { const key = firstRecord.pid.slice(-2) if (key !== extend) firstRecord.pid = firstRecord.pid + extend records = [firstRecord] } } return records } export default parseLog