TERMINAL: Add --ram-override flag to the run command (#1055)

* Add --ram-override flag to run command
* Update help command
* Fix whitespace
* Update run usage message
* Update autocomplete for run command
* Format
This commit is contained in:
LJ 2024-02-15 06:51:37 -07:00 committed by GitHub
parent 733f2ccb5b
commit ddb10f833c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 68 additions and 55 deletions

@ -1,53 +1,54 @@
export const TerminalHelpText: string[] = [ export const TerminalHelpText: string[] = [
"Type 'help name' to learn more about the command ", "Type 'help name' to learn more about the command ",
" ", " ",
' alias [-g] [name="value"] Create or display Terminal aliases', ' alias [-g] [name="value"] Create or display Terminal aliases',
" analyze Get information about the current machine ", " analyze Get information about the current machine ",
" backdoor Install a backdoor on the current machine ", " backdoor Install a backdoor on the current machine ",
" buy [-l/-a/program] Purchase a program through the Dark Web", " buy [-l/-a/program] Purchase a program through the Dark Web",
" cat [file] Display a .msg, .lit, or .txt file", " cat [file] Display a .msg, .lit, or .txt file",
" cd [dir] Change to a new directory", " cd [dir] Change to a new directory",
" check [script] [args...] Print a script's logs to Terminal", " check [script] [args...] Print a script's logs to Terminal",
" clear Clear all text on the terminal ", " clear Clear all text on the terminal ",
" cls See 'clear' command ", " cls See 'clear' command ",
" connect [hostname] Connects to a remote server", " connect [hostname] Connects to a remote server",
" cp [src] [dst] Copy a file", " cp [src] [dest] Copy a file",
" download [script/text file] Downloads scripts or text files to your computer", " download [script/text file] Downloads scripts or text files to your computer",
" expr [math expression] Evaluate a mathematical expression", " expr [math expression] Evaluate a mathematical expression",
" free Check the machine's memory (RAM) usage", " free Check the machine's memory (RAM) usage",
" grow Spoof money in a servers bank account, increasing the amount available.", " grow Spoof money in a servers bank account, increasing the amount available.",
" hack Hack the current machine", " hack Hack the current machine",
" help [command] Display this help text, or the help text for a command", " help [command] Display this help text, or the help text for a command",
" history [-c] Display the terminal history", " history [-c] Display the terminal history",
" home Connect to home computer", " home Connect to home computer",
" hostname Displays the hostname of the machine", " hostname Displays the hostname of the machine",
" kill [script/pid] [args...] Stops the specified script on the current server ", " kill [script/pid] [args...] Stops the specified script on the current server ",
" killall Stops all running scripts on the current machine", " killall Stops all running scripts on the current machine",
" ls [dir] [--grep pattern] Displays all files on the machine", " ls [dir] [--grep pattern] Displays all files on the machine",
" lscpu Displays the number of CPU cores on the machine", " lscpu Displays the number of CPU cores on the machine",
" mem [script] [-t n] Displays the amount of RAM required to run the script", " mem [script] [-t n] Displays the amount of RAM required to run the script",
" mv [src] [dest] Move/rename a text or script file", " mv [src] [dest] Move/rename a text or script file",
" nano [file ...] Text editor - Open up and edit one or more scripts or text files", " nano [files...] Text editor - Open up and edit one or more scripts or text files",
" ps Display all scripts that are currently running", " ps Display all scripts that are currently running",
" rm [file] Delete a file from the server", " rm [file] Delete a file from the server",
" run [name] [-t n] [--tail] [args...] Execute a program or script", " run [script] [-t n] [--tail] Execute a program or script",
" scan Prints all immediately-available network connections", " [--ram-override n] [args...]",
" scan-analyze [d] [-a] Prints info for all servers up to d nodes away", " scan Prints all immediately-available network connections",
" scp [file ...] [server] Copies a file to a destination server", " scan-analyze [d] [-a] Prints info for all servers up to d nodes away",
" sudov Shows whether you have root access on this computer", " scp [files...] [server] Copies a file to a destination server",
" tail [script] [args...] Displays dynamic logs for the specified script", " sudov Shows whether you have root access on this computer",
" top Displays all running scripts and their RAM usage", " tail [script] [args...] Displays dynamic logs for the specified script",
" unalias [alias name] Deletes the specified alias", " top Displays all running scripts and their RAM usage",
" vim [file ...] Text editor - Open up and edit one or more scripts or text files in vim mode", " unalias [alias name] Deletes the specified alias",
" weaken Reduce the security of the current machine", " vim [files...] Text editor - Open up and edit one or more scripts or text files in vim mode",
" wget [url] [target file] Retrieves code/text from a web server", " weaken Reduce the security of the current machine",
" wget [url] [target file] Retrieves code/text from a web server",
" ", " ",
]; ];
const TemplatedHelpTexts: Record<string, (command: string) => string[]> = { const TemplatedHelpTexts: Record<string, (command: string) => string[]> = {
scriptEditor: (command) => { scriptEditor: (command) => {
return [ return [
`Usage: ${command} [file ...] | [glob]`, `Usage: ${command} [file names...] | [glob]`,
` `, ` `,
`Opens up the specified file(s) in the Script Editor. Only scripts (.js, or .script) or text files (.txt) `, `Opens up the specified file(s) in the Script Editor. Only scripts (.js, or .script) or text files (.txt) `,
`can be edited using the Script Editor. If a file does not exist a new one will be created`, `can be edited using the Script Editor. If a file does not exist a new one will be created`,
@ -133,7 +134,7 @@ export const HelpTexts: Record<string, string[]> = {
" ", " ",
], ],
cat: [ cat: [
"Usage: cat [file]", "Usage: cat [file name]",
" ", " ",
"Display message (.msg), literature (.lit), or text (.txt) files. Examples:", "Display message (.msg), literature (.lit), or text (.txt) files. Examples:",
" ", " ",
@ -193,7 +194,7 @@ export const HelpTexts: Record<string, string[]> = {
"a backdoor installed can be connected to. To see which servers can be connected to, use the 'scan' command.", "a backdoor installed can be connected to. To see which servers can be connected to, use the 'scan' command.",
" ", " ",
], ],
cp: ["Usage: cp [src] [dst]", " ", "Copy a file on this server. To copy a file to another server use scp.", " "], cp: ["Usage: cp [src] [dest]", " ", "Copy a file on this server. To copy a file to another server use scp.", " "],
download: [ download: [
"Usage: download [script/text file]", "Usage: download [script/text file]",
" ", " ",
@ -358,7 +359,7 @@ export const HelpTexts: Record<string, string[]> = {
nano: TemplatedHelpTexts.scriptEditor("nano"), nano: TemplatedHelpTexts.scriptEditor("nano"),
ps: ["Usage: ps", " ", "Prints all scripts that are running on the current server", " "], ps: ["Usage: ps", " ", "Prints all scripts that are running on the current server", " "],
rm: [ rm: [
"Usage: rm [file]", "Usage: rm [file name]",
" ", " ",
"Removes the specified file from the current server. A file can be a script, a program, or a message file. ", "Removes the specified file from the current server. A file can be a script, a program, or a message file. ",
" ", " ",
@ -366,15 +367,17 @@ export const HelpTexts: Record<string, string[]> = {
" ", " ",
], ],
run: [ run: [
"Usage: run [file name] [-t] [num threads] [args...]", "Usage: run [file name] [-t num_threads] [--tail] [--ram-override ram_in_GBs] [args...]",
" ", " ",
"Execute a program, script or coding contract.", "Execute a program, script or coding contract.",
" ", " ",
"The '[-t]', '[num threads]', and '[args...]' arguments are only valid when running a script. The '-t' flag is used ", "The '[-t num_threads]', '[--tail]', `[--ram-override ram_in_GBs]`, and '[args...]' arguments are only valid",
"to indicate that the script should be run with the specified number of threads. If the flag is omitted, ", "when running a script. The '-t' flag is used to indicate that the script should be run with the specified",
"then the script will be run with a single thread by default. ", "number of threads. If the flag is omitted, then the script will be run with a single thread by default. The",
"If the '-t' flag is used, then it MUST come immediately ", "'--tail' flag is used to immediately open a tail window for the script being ran. And the '--ram-override'",
"after the script name, and the [num threads] argument MUST come immediately afterwards. ", "flag is used to override the amount of ram (per thread) the script is ran with. If the script ends up using",
"more than that amount of ram it will crash. If any of the flags are used, then they MUST come immediately",
"after the script name.",
" ", " ",
"[args...] represents a variable number of arguments that will be passed into the script. See the documentation ", "[args...] represents a variable number of arguments that will be passed into the script. See the documentation ",
"about script arguments. Each specified argument must be separated by a space. ", "about script arguments. Each specified argument must be separated by a space. ",
@ -407,7 +410,7 @@ export const HelpTexts: Record<string, string[]> = {
" ", " ",
], ],
scp: [ scp: [
"Usage: scp [filename ...] [target server]", "Usage: scp [file names...] [target server]",
" ", " ",
"Copies the specified file(s) from the current server to the target server. ", "Copies the specified file(s) from the current server to the target server. ",
"This command only works for script files (.script or .js extension), literature files (.lit extension), ", "This command only works for script files (.script or .js extension), literature files (.lit extension), ",

@ -9,7 +9,10 @@ import { hasProgramExtension } from "../../Paths/ProgramFilePath";
export function run(args: (string | number | boolean)[], server: BaseServer): void { export function run(args: (string | number | boolean)[], server: BaseServer): void {
// Run a program or a script // Run a program or a script
const arg = args.shift(); const arg = args.shift();
if (!arg) return Terminal.error("Usage: run [program/script] [-t] [num threads] [arg1] [arg2]..."); if (!arg)
return Terminal.error(
"Usage: run [program/script] [-t num_threads] [--tail] [--ram-override ram_in_GBs] [args...]",
);
const path = Terminal.getFilepath(String(arg)); const path = Terminal.getFilepath(String(arg));
if (!path) return Terminal.error(`${arg} is not a valid filepath.`); if (!path) return Terminal.error(`${arg} is not a valid filepath.`);

@ -9,28 +9,35 @@ import { ScriptArg } from "@nsdefs";
import { isPositiveInteger } from "../../types"; import { isPositiveInteger } from "../../types";
import { ScriptFilePath } from "../../Paths/ScriptFilePath"; import { ScriptFilePath } from "../../Paths/ScriptFilePath";
import { sendDeprecationNotice } from "./common/deprecation"; import { sendDeprecationNotice } from "./common/deprecation";
import { RamCostConstants } from "../../Netscript/RamCostGenerator";
export function runScript(path: ScriptFilePath, commandArgs: (string | number | boolean)[], server: BaseServer): void { export function runScript(path: ScriptFilePath, commandArgs: (string | number | boolean)[], server: BaseServer): void {
// This takes in the absolute filepath, see "run.ts" // This takes in the absolute filepath, see "run.ts"
const script = server.scripts.get(path); const script = server.scripts.get(path);
if (!script) return Terminal.error(`Script ${path} does not exist on this server.`); if (!script) return Terminal.error(`Script ${path} does not exist on this server.`);
const runArgs = { "--tail": Boolean, "-t": Number }; const runArgs = { "--tail": Boolean, "-t": Number, "--ram-override": Number };
const flags = libarg(runArgs, { const flags = libarg(runArgs, {
permissive: true, permissive: true,
argv: commandArgs, argv: commandArgs,
}); });
const tailFlag = flags["--tail"] === true; const tailFlag = flags["--tail"] === true;
const numThreads = parseFloat(flags["-t"] ?? 1); const numThreads = parseFloat(flags["-t"] ?? 1);
const ramOverride = flags["--ram-override"] != null ? parseFloat(flags["--ram-override"]) : null;
if (!isPositiveInteger(numThreads)) { if (!isPositiveInteger(numThreads)) {
return Terminal.error("Invalid number of threads specified. Number of threads must be an integer greater than 0"); return Terminal.error("Invalid number of threads specified. Number of threads must be an integer greater than 0");
} }
if (ramOverride != null && (isNaN(ramOverride) || ramOverride < RamCostConstants.Base)) {
return Terminal.error(
`Invalid ram override specified. Ram override must be a number greater than ${RamCostConstants.Base}`,
);
}
if (!server.hasAdminRights) return Terminal.error("Need root access to run script"); if (!server.hasAdminRights) return Terminal.error("Need root access to run script");
// Todo: Switch out arg for something with typescript support // Todo: Switch out arg for something with typescript support
const args = flags._ as ScriptArg[]; const args = flags._ as ScriptArg[];
const singleRamUsage = script.getRamUsage(server.scripts); const singleRamUsage = ramOverride ?? script.getRamUsage(server.scripts);
if (!singleRamUsage) { if (!singleRamUsage) {
return Terminal.error(`Error while calculating ram usage for this script. ${script.ramCalculationError}`); return Terminal.error(`Error while calculating ram usage for this script. ${script.ramCalculationError}`);
} }

@ -312,7 +312,7 @@ export async function getTabCompletionPossibilities(terminalText: string, baseDi
} }
if (!loadedModule || !loadedModule.autocomplete) return; // Doesn't have an autocomplete function. if (!loadedModule || !loadedModule.autocomplete) return; // Doesn't have an autocomplete function.
const runArgs = { "--tail": Boolean, "-t": Number }; const runArgs = { "--tail": Boolean, "-t": Number, "--ram-override": Number };
const flags = libarg(runArgs, { const flags = libarg(runArgs, {
permissive: true, permissive: true,
argv: command.slice(2), argv: command.slice(2),