mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: able to test files
This commit is contained in:
1 parent
af27780763
commit
625c1a80da
5 files changed
+99
-28
No files matched your search
@@ -3,6 +3,7 @@ import { FileGraph } from '@linux-odyssey/file-graph'
|
||||
import { Quest } from '@linux-odyssey/models'
|
||||
import { pushToSession } from '../api/socket.js'
|
||||
import SessionHandler from './sessionHandler.js'
|
||||
import { checkFiles } from '../containers/docker.js'
|
||||
|
||||
// 檢查 pattern 是否符合 input
|
||||
const checkMatch = (pattern, input) => {
|
||||
@@ -53,15 +54,31 @@ export default class CommandHandler extends SessionHandler {
|
||||
this.session.graph = graph
|
||||
}
|
||||
|
||||
isMatch(condition) {
|
||||
async isMatch(condition) {
|
||||
const keys = ['command', 'output', 'error', 'pwd']
|
||||
console.log('condition', condition)
|
||||
return (
|
||||
keys.every((k) => checkMatch(condition[k], this.commandInput[k])) &&
|
||||
(condition.$or.length === 0 ||
|
||||
condition.$or.some((c) => this.isMatch(c))) &&
|
||||
(!condition.$not || !this.isMatch(condition.$not))
|
||||
)
|
||||
if (keys.some((k) => !checkMatch(condition[k], this.commandInput[k]))) {
|
||||
return false
|
||||
}
|
||||
if (condition.files.length > 0) {
|
||||
const match = await this.checkFiles(condition.files)
|
||||
if (!match) return false
|
||||
}
|
||||
if (condition.$not) {
|
||||
const match = await this.isMatch(condition.$not)
|
||||
if (match) return false
|
||||
}
|
||||
if (condition.$or.length > 0) {
|
||||
const matches = await Promise.all(
|
||||
condition.$or.map((c) => this.isMatch(c))
|
||||
)
|
||||
if (matches.every((m) => m === false)) return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
checkFiles(files) {
|
||||
console.log('checkFiles', files)
|
||||
return checkFiles(this.session.containerId, files)
|
||||
}
|
||||
|
||||
async run() {
|
||||
@@ -75,9 +92,18 @@ export default class CommandHandler extends SessionHandler {
|
||||
this.handleEvent()
|
||||
this.handleCommand()
|
||||
|
||||
const response = stages
|
||||
.filter((s) => this.isMatch(s.condition))
|
||||
.reduce((r, s) => ({ ...r, ...this.execute(s) }), {})
|
||||
const matches = await Promise.all(
|
||||
stages.map((stage) => this.isMatch(stage.condition))
|
||||
)
|
||||
|
||||
console.log('matches', matches)
|
||||
|
||||
const filteredStages = stages.filter((_, i) => matches[i])
|
||||
console.log('filteredStages', filteredStages)
|
||||
const response = filteredStages.reduce(
|
||||
(r, s) => ({ ...r, ...this.execute(s) }),
|
||||
{}
|
||||
)
|
||||
|
||||
return {
|
||||
end: this.session.status === 'finished',
|
||||
|
||||
Reference in new issue
Block a user