75 lines
1.7 KiB
TypeScript
75 lines
1.7 KiB
TypeScript
import { exec } from "child_process"
|
|
|
|
type DiskSource = {
|
|
key: string
|
|
label: string
|
|
command: string
|
|
args?: string[]
|
|
}
|
|
|
|
const diskSources: DiskSource[] = [
|
|
{
|
|
key: "remote",
|
|
label: "Serveur distant",
|
|
command: "ssh",
|
|
args: []
|
|
},
|
|
{
|
|
key: "local",
|
|
label: "Machine locale",
|
|
command: "bash",
|
|
args: []
|
|
}
|
|
]
|
|
|
|
function getEnvCommand(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 runShellCommand(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.map(async (source) => {
|
|
try {
|
|
const envCommand = getEnvCommand(source)
|
|
if (!envCommand) {
|
|
throw new Error(`Commande disque manquante pour ${source.key}`)
|
|
}
|
|
|
|
const output = await runShellCommand(envCommand)
|
|
return {
|
|
key: source.key,
|
|
label: source.label,
|
|
ok: true,
|
|
output
|
|
}
|
|
} catch (error) {
|
|
console.error(`Erreur disk source ${source.key}:`, error)
|
|
return {
|
|
key: source.key,
|
|
label: source.label,
|
|
ok: false,
|
|
output: "Erreur lors de l'opération"
|
|
}
|
|
}
|
|
})
|
|
)
|
|
|
|
return { results }
|
|
})
|