fix: 增加脚本的 prompt

aiops_dev
时启龙 2025-08-08 11:11:59 +08:00
parent 43493c6542
commit 420aedf83e
2 changed files with 55 additions and 14 deletions

View File

@ -40,7 +40,52 @@
import CodeMirror from 'cmp-element/components/code-mirror'
import { last } from 'lodash-es'
import { getGroup, getScriptBySimple, getScriptDetail } from 'services/task/script'
/**
* 使用管道符'|'作为分隔符从脚本内容中解析出参数
* @param {string} scriptContent 用户输入的脚本内容例如 "echo ${{name|请输入姓名}}}"
* @returns {Array<{key: string, value: string, prompt: string}>} 解析并去重后的参数数组
*/
function parseScriptWithPipe(scriptContent) {
// :
// \${{ - "${{"
// \s* - 0
// (\w+) - 1: key (线)
// \s* - 0
// ( - (?: ... )?
// \| - '|'
// \s* - 0
// (.*?) - 2: prompt ( "}}" )
// )? - prompt
// \s* - 0
// }} - "}}"
const regex = /\${{\s*(\w+)\s*(?:\|\s*(.*?))?\s*}}/g
// 使 matchAll
const matches = scriptContent.matchAll(regex)
// 使 Map key key
const paramsMap = new Map()
for (const match of matches) {
// match[1] key
const key = match[1]
// key map
if (!paramsMap.has(key)) {
// match[2] (prompt) undefined
const prompt = match[2] || '' // 使
paramsMap.set(key, {
key: key,
value: '', // value key ''
prompt: prompt.trim() // prompt
})
}
}
// Map
return Array.from(paramsMap.values())
}
export default {
components: { CodeMirror },
props: {
@ -91,19 +136,15 @@ export default {
const data = await getScriptDetail(this.scriptItem.scriptId)
if (data.success) {
const { category, content, name: scriptName, filename } = data.data
const reg = new RegExp(/\$\{\{(.+)\}\}/g)
const res = content.match(reg)
const params = []
if (res) {
// ['${{hostname}}'] => ['hostname']
const keys = [...new Set(res.map(item => item.slice(3, item.length - 2)))]
keys.forEach(element => {
params.push({
key: element,
value: ''
})
})
}
// '${{hostname|hostname}}' =>
// [
// {
// key: 'hostname',
// value: '',
// prompt: 'hostname'
// }
// ]
const params = parseScriptWithPipe(content)
this.scriptItem.category = category
this.scriptItem.content = content
this.scriptItem.scriptName = scriptName

View File

@ -4,7 +4,7 @@
<el-row :gutter="10">
<el-col :span="12" v-for="(cell, index) in itemData.params" :key="index">
<basic-form-item :label="`${cell.key}`" show-overflow-tooltip>
<el-input v-model="cell.value"></el-input>
<el-input v-model="cell.value" :placeholder="cell.prompt"></el-input>
</basic-form-item>
</el-col>
</el-row>