From 29e5d63202fb266e02c0ded30613942a9ce8c33e Mon Sep 17 00:00:00 2001 From: Hexagon Date: Sun, 31 Mar 2024 03:17:57 +0200 Subject: [PATCH] Fixes. Wip. --- korv.ts | 1 + lib/cli/main.ts | 6 +++--- lib/managers/systemd.ts | 38 +++++++++++++++++++++++++++++--------- 3 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 korv.ts diff --git a/korv.ts b/korv.ts new file mode 100644 index 0000000..4e1acd8 --- /dev/null +++ b/korv.ts @@ -0,0 +1 @@ +console.log("Korv!"); diff --git a/lib/cli/main.ts b/lib/cli/main.ts index 11e23c4..e8e50ab 100644 --- a/lib/cli/main.ts +++ b/lib/cli/main.ts @@ -68,7 +68,7 @@ async function main(inputArgs: string[]) { if (baseArgument === "generate") { console.log(result.serviceFileContent); } else { - if (result.manualSteps) { + if (result.manualSteps && result.manualSteps.length) { console.log("To complete the installation, carry out these manual steps:"); console.log(result.manualSteps); } else { @@ -86,7 +86,7 @@ async function main(inputArgs: string[]) { } else if (baseArgument === "uninstall") { try { const result = await uninstallService({ system, name, home }); - if (result.manualSteps) { + if (result.manualSteps && result.manualSteps.length) { console.log(`Carry out these manual steps to complete the unistallation of '${name}'`); console.log(result.manualSteps); } else { @@ -94,7 +94,7 @@ async function main(inputArgs: string[]) { } exit(0); } catch (e) { - console.error(`Could not install service, error: ${e.message}`); + console.error(`Could not uninstall service, error: ${e.message}`); exit(1); } } else { diff --git a/lib/managers/systemd.ts b/lib/managers/systemd.ts index 648f131..3106213 100644 --- a/lib/managers/systemd.ts +++ b/lib/managers/systemd.ts @@ -74,11 +74,12 @@ class SystemdService { } else if (config.system) { // Store temporary file const tempFilePath = await mktempdir("svcinstall"); - await writeFile(join(tempFilePath, "cfg"), serviceFileContent); + const tempFileFullPath = join(tempFilePath, "cfg"); + await writeFile(tempFileFullPath, serviceFileContent); const manualSteps: ServiceManualStep[] = []; manualSteps.push({ text: "The systemd configuration has been saved to a temporary file. Copy this file to the correct location using the following command:", - command: `sudo cp ${tempFilePath} ${servicePath}`, + command: `sudo cp ${tempFileFullPath} ${servicePath}`, }); manualSteps.push({ text: "Reload the systemd configuration:", @@ -153,15 +154,34 @@ class SystemdService { if (!await exists(servicePath)) { throw new Error(`Service '${config.name}' does not exist.`); } - try { - await unlink(servicePath); const manualSteps: ServiceManualStep[] = []; - const reloadCommand = config.system ? "sudo systemctl daemon-reload" : "systemctl --user daemon-reload"; - manualSteps.push({ - text: `Please run the following command to reload the systemctl daemon:`, - command: reloadCommand, - }); + if (config.system) { + const removeCommand = `sudo rm ${servicePathSystem}`; + const stopCommand = `sudo systemctl stop ${config.name}`; + const reloadCommand = "sudo systemctl daemon-reload"; + manualSteps.push( + { + text: `Please run this command to stop the service:`, + command: stopCommand, + }, + { + text: `Please run the following command to remove the service:`, + command: removeCommand, + }, + { + text: `And this command to reload the systemctl daemon:`, + command: reloadCommand, + }, + ); + } else { + const stopServiceCommand = await spawn(["systemctl", "--user", "stop", config.name]); + await unlink(servicePath); + const reloadServiceCommand = await spawn(["systemctl", "--user", "daemon-reload"]); + if (!(stopServiceCommand.code === 0 && reloadServiceCommand.code === 0)) { + throw new Error("Could not remove the service."); + } + } return { servicePath, manualSteps: manualSteps,