Files
Supervisor/server/api/disk.get.ts
2026-03-12 09:42:28 +01:00

60 lines
1.4 KiB
TypeScript

import { exec } from "child_process"
import diskSources from "../config/disk-commands.json"
type DiskSource = {
key: string
label: string
command: string
}
function getCommand(source: DiskSource) {
const envKey = `DISK_COMMAND_${source.key.toUpperCase()}`
const legacyEnvKey =
source.key === "remote" ? "DISK_REMOTE_COMMAND" : source.key === "local" ? "DISK_LOCAL_COMMAND" : ""
return process.env[envKey] || (legacyEnvKey ? process.env[legacyEnvKey] : undefined) || null
}
function runCommand(command: string): Promise<string> {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(stderr || error.message)
return
}
resolve(stdout)
})
})
}
export default defineEventHandler(async () => {
const results = await Promise.all(
(diskSources as DiskSource[]).map(async (source) => {
try {
const command = getCommand(source)
if (!command) {
throw new Error(`Commande manquante pour ${source.key}`)
}
const output = await runCommand(command)
return {
key: source.key,
label: source.label,
ok: true,
output
}
} catch (error) {
return {
key: source.key,
label: source.label,
ok: false,
output: `Erreur: ${String(error)}`
}
}
})
)
return { results }
})