bitburner-src/src/Terminal/commands/scp.ts
Snarling e0272ad4af
FILES: Path rework & typesafety (#479)
* Added new types for various file paths, all in the Paths folder.
* TypeSafety and other helper functions related to these types
* Added basic globbing support with * and ?. Currently only implemented for Script/Text, on nano and download terminal commands
* Enforcing the new types throughout the codebase, plus whatever rewrites happened along the way
* Server.textFiles is now a map
* TextFile no longer uses a fn property, now it is filename
* Added a shared ContentFile interface for shared functionality between TextFile and Script.
* related to ContentFile change above, the player is now allowed to move a text file to a script file and vice versa.
* File paths no longer conditionally start with slashes, and all directory names other than root have ending slashes. The player is still able to provide paths starting with / but this now indicates that the player is specifying an absolute path instead of one relative to root.
* Singularized the MessageFilename and LiteratureName enums
* Because they now only accept correct types, server.writeToXFile functions now always succeed (the only reasons they could fail before were invalid filepath).
* Fix several issues with tab completion, which included pretty much a complete rewrite
* Changed the autocomplete display options so there's less chance it clips outside the display area.
* Turned CompletedProgramName into an enum.
* Got rid of programsMetadata, and programs and DarkWebItems are now initialized immediately instead of relying on initializers called from the engine.
* For any executable (program, cct, or script file) pathing can be used directly to execute without using the run command (previously the command had to start with ./ and it wasn't actually using pathing).
2023-04-24 10:26:57 -04:00

41 lines
1.9 KiB
TypeScript

import { Terminal } from "../../Terminal";
import { BaseServer } from "../../Server/BaseServer";
import { GetServer } from "../../Server/AllServers";
import { hasScriptExtension } from "../../Paths/ScriptFilePath";
import { hasTextExtension } from "../../Paths/TextFilePath";
import { checkEnum } from "../../utils/helpers/enum";
import { LiteratureName } from "../../Literature/data/LiteratureNames";
export function scp(args: (string | number | boolean)[], server: BaseServer): void {
if (args.length !== 2) {
return Terminal.error("Incorrect usage of scp command. Usage: scp [source filename] [destination hostname]");
}
const [scriptname, destHostname] = args.map((arg) => arg + "");
const path = Terminal.getFilepath(scriptname);
if (!path) return Terminal.error(`Invalid file path: ${scriptname}`);
const destServer = GetServer(destHostname);
if (!destServer) return Terminal.error(`Invalid destination server: ${args[1]}`);
// Lit files
if (path.endsWith(".lit")) {
if (!checkEnum(LiteratureName, path) || !server.messages.includes(path)) {
return Terminal.error(`No file at path ${path}`);
}
if (destServer.messages.includes(path)) return Terminal.print(`${path} was already on ${destHostname}`);
destServer.messages.push(path);
return Terminal.print(`Copied ${path} to ${destHostname}`);
}
if (!hasScriptExtension(path) && !hasTextExtension(path)) {
return Terminal.error("scp only works for scripts, text files (.txt), and literature files (.lit)");
}
// Text or script
const source = server.getContentFile(path);
if (!source) return Terminal.error(`No file at path ${path}`);
const { overwritten } = destServer.writeToContentFile(path, source.content);
if (overwritten) Terminal.warn(`${path} already exists on ${destHostname} and will be overwritten`);
Terminal.print(`${path} copied to ${destHostname}`);
}