fix: use env only
This commit is contained in:
@@ -1,16 +1,8 @@
|
||||
import scripts from "../config/backup-script.json"
|
||||
|
||||
type BackupScript = {
|
||||
key: string
|
||||
label: string
|
||||
icon?: string
|
||||
downloadFolders?: string[]
|
||||
command: string
|
||||
}
|
||||
import { backupScripts } from "../utils/backup-scripts"
|
||||
|
||||
export default defineEventHandler(() => {
|
||||
return {
|
||||
scripts: (scripts as BackupScript[]).map(({ key, label, icon, downloadFolders }) => ({
|
||||
scripts: backupScripts.map(({ key, label, icon, downloadFolders }) => ({
|
||||
key,
|
||||
label,
|
||||
icon: icon || "mdi:play-circle-outline",
|
||||
|
||||
@@ -1,17 +1,9 @@
|
||||
import { execFile } from "node:child_process"
|
||||
import scripts from "../config/backup-script.json"
|
||||
import { exec } from "node:child_process"
|
||||
import { backupScripts, getBackupScriptCommand } from "../utils/backup-scripts"
|
||||
|
||||
type BackupScript = {
|
||||
key: string
|
||||
label: string
|
||||
downloadFolders?: string[]
|
||||
command: string
|
||||
args?: string[]
|
||||
}
|
||||
|
||||
function runCommand(command: string, args: string[] = []): Promise<string> {
|
||||
function runCommand(command: string): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
execFile(command, args, { timeout: 10 * 60 * 1000 }, (error, stdout, stderr) => {
|
||||
exec(command, { timeout: 10 * 60 * 1000 }, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
reject(stderr || error.message)
|
||||
return
|
||||
@@ -32,7 +24,7 @@ export default defineEventHandler(async (event) => {
|
||||
})
|
||||
}
|
||||
|
||||
const script = (scripts as BackupScript[]).find((item) => item.key === key)
|
||||
const script = backupScripts.find((item) => item.key === key)
|
||||
if (!script) {
|
||||
throw createError({
|
||||
statusCode: 404,
|
||||
@@ -41,7 +33,15 @@ export default defineEventHandler(async (event) => {
|
||||
}
|
||||
|
||||
try {
|
||||
const output = await runCommand(script.command, script.args || [])
|
||||
const command = getBackupScriptCommand(script.key)
|
||||
if (!command) {
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: "Commande de script manquante"
|
||||
})
|
||||
}
|
||||
|
||||
const output = await runCommand(command)
|
||||
return {
|
||||
ok: true,
|
||||
key: script.key,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { exec, execFile } from "child_process"
|
||||
import diskSources from "../config/disk-commands.json"
|
||||
import { exec } from "child_process"
|
||||
|
||||
type DiskSource = {
|
||||
key: string
|
||||
@@ -8,6 +7,21 @@ type DiskSource = {
|
||||
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 =
|
||||
@@ -16,18 +30,6 @@ function getEnvCommand(source: DiskSource) {
|
||||
return process.env[envKey] || (legacyEnvKey ? process.env[legacyEnvKey] : undefined) || null
|
||||
}
|
||||
|
||||
function runCommand(command: string, args: string[] = []): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
execFile(command, args, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
reject(stderr || error.message)
|
||||
return
|
||||
}
|
||||
resolve(stdout)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function runShellCommand(command: string): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec(command, (error, stdout, stderr) => {
|
||||
@@ -42,12 +44,14 @@ function runShellCommand(command: string): Promise<string> {
|
||||
|
||||
export default defineEventHandler(async () => {
|
||||
const results = await Promise.all(
|
||||
(diskSources as DiskSource[]).map(async (source) => {
|
||||
diskSources.map(async (source) => {
|
||||
try {
|
||||
const envCommand = getEnvCommand(source)
|
||||
const output = envCommand
|
||||
? await runShellCommand(envCommand)
|
||||
: await runCommand(source.command, source.args || [])
|
||||
if (!envCommand) {
|
||||
throw new Error(`Commande disque manquante pour ${source.key}`)
|
||||
}
|
||||
|
||||
const output = await runShellCommand(envCommand)
|
||||
return {
|
||||
key: source.key,
|
||||
label: source.label,
|
||||
|
||||
Reference in New Issue
Block a user