ATC_SIMPLE/BACKEND/app/ultils/templates/index.ts

79 lines
2.3 KiB
TypeScript

import showInventory from './show_inventory.js'
import showVersion from './show_version.js'
import showLicense from './show_license.js'
import showLogging from './show_logging.js'
// const showPower = require("./show_power.js");
// Function to parse logs
function getStructuredDataTextfsm(output: string, command: string) {
switch (command) {
case 'show inventory':
case 'show inv':
case 'sh inventory':
case 'sh inv':
return showInventory(output)
case 'show version':
case 'show ver':
case 'sh version':
case 'sh ver':
return showVersion(output)
case 'show license':
case 'sh license':
return showLicense(output)
case 'show logging':
case 'sh logging':
return showLogging(output)
default:
return ''
}
// Call the parseLog method with log data and patterns
}
export const textfsmResults = (logContent: string, cmd: string) => {
let results = []
if (cmd) {
let structuredOutput = getStructuredDataTextfsm(logContent, cmd)
if (typeof structuredOutput === 'string') {
structuredOutput = {} // Convert to an empty object if it's a string
}
results = [
{
command: cmd,
output: logContent,
textfsm: JSON.stringify(structuredOutput).replace(/[\x00-\x1f\x7f-\x9f]/g, ''),
},
]
} else {
// Regular expression to parse commands and outputs inside the scoped content
const regexPattern =
/---send-command---"(?<command>.+?)"---\d+---(?<output>[\s\S]*?)(?=(---send-command---|---split-point---|---end-testing---|---end-scenarios---))/gms
// Parse commands and outputs
const matches = [...logContent.matchAll(regexPattern)]
// Process matches
results = matches
.map((match) => {
const command = match.groups?.command.trim() || ''
const output = match.groups?.output.trim() || ''
// Get structured output using the parser
let structuredOutput = getStructuredDataTextfsm(output, command)
if (typeof structuredOutput === 'string') {
structuredOutput = {} // Convert to an empty object if it's a string
}
return {
command,
output,
textfsm: JSON.stringify(structuredOutput).replace(/[\x00-\x1f\x7f-\x9f]/g, ''), // Clean special characters
}
})
.filter((el) => el.command)
}
return results
}