Verify the number of arguments for ultimate commands

This commit is contained in:
Sander Declerck 2026-02-02 15:28:44 +01:00
parent 78e4a43916
commit e01b06b238
No known key found for this signature in database
2 changed files with 35 additions and 4 deletions

View file

@ -20,7 +20,10 @@ import {
installUltimate,
uninstallUltimate,
} from "../src/installation/installUltimate.js";
import {printUltimateLogs, troubleshootingExport } from "../src/ultimate/ultimateTroubleshooting.js";
import {
printUltimateLogs,
troubleshootingExport,
} from "../src/ultimate/ultimateTroubleshooting.js";
/** @type {string} */
// This checks the current file's dirname in a way that's compatible with:
@ -68,20 +71,34 @@ if (tool) {
} else if (command === "setup") {
setup();
} else if (command === "ultimate") {
const subCommand = process.argv[3];
const cliArgs = initializeCliArguments(process.argv.slice(2));
const subCommand = cliArgs[1];
if (subCommand === "uninstall") {
guardCliArgsMaxLenght(2, cliArgs, "safe-chain ultimate uninstall");
(async () => {
await uninstallUltimate();
})();
} else if (subCommand === "troubleshooting-logs") {
guardCliArgsMaxLenght(
2,
cliArgs,
"safe-chain ultimate troubleshooting-logs",
);
(async () => {
await printUltimateLogs();
})();
} else if (subCommand === "troubleshooting-export") {
guardCliArgsMaxLenght(
2,
cliArgs,
"safe-chain ultimate troubleshooting-export",
);
(async () => {
await troubleshootingExport();
})();
} else {
guardCliArgsMaxLenght(1, cliArgs, "safe-chain ultimate");
// Install command = when no subcommand is provided (safe-chain ultimate)
(async () => {
await installUltimate();
})();
@ -104,6 +121,22 @@ if (tool) {
process.exit(1);
}
/**
* @param {Number} maxLength
* @param {String[]} args
* @param {String} command
*/
function guardCliArgsMaxLenght(maxLength, args, command) {
if (args.length > maxLength) {
ui.writeError(`Unexpected number of arguments for command ${command}.`);
ui.emptyLine();
writeHelp();
process.exit(1);
}
}
function writeHelp() {
ui.writeInformation(
chalk.bold("Usage: ") + chalk.cyan("safe-chain <command>"),

View file

@ -21,8 +21,6 @@ export async function uninstallUltimate() {
}
export async function installUltimate() {
initializeCliArguments(process.argv);
const operatingSystem = platform();
if (operatingSystem === "win32") {