Implemented wget Terminal command

This commit is contained in:
danielyxie 2018-09-07 20:53:11 -05:00
parent 58d48f19ef
commit 98598b103d
4 changed files with 96 additions and 40 deletions

@ -494,7 +494,7 @@ let CONSTANTS = {
LatestUpdate: LatestUpdate:
"v0.40.3<br>" + "v0.40.3<br>" +
"" "* Added a setting in .fconf for enabling line-wrap in the Terminal input<br>"
} }

@ -32,7 +32,8 @@ export const TerminalHelpText: string =
"tail [script] [args...] Displays dynamic logs for the specified script<br>" + "tail [script] [args...] Displays dynamic logs for the specified script<br>" +
"theme [preset] | bg txt hlgt Change the color scheme of the UI<br>" + "theme [preset] | bg txt hlgt Change the color scheme of the UI<br>" +
"top Displays all running scripts and their RAM usage<br>" + "top Displays all running scripts and their RAM usage<br>" +
'unalias "[alias name]" Deletes the specified alias<br>'; 'unalias "[alias name]" Deletes the specified alias<br>' +
'wget [url] [target file] Retrieves code/text from a web server<br>';
interface IMap<T> { interface IMap<T> {
[key: string]: T; [key: string]: T;
@ -214,5 +215,11 @@ export const HelpTexts: IMap<string> = {
"Then it could be removed using:<br><br>" + "Then it could be removed using:<br><br>" +
'unalias "r"<br><br>' + 'unalias "r"<br><br>' +
"It is not necessary to differentiate between global and non-global aliases when using 'unalias'", "It is not necessary to differentiate between global and non-global aliases when using 'unalias'",
wget: "wget [url] [target file]<br>" +
"Retrieves data from a URL and downloads it to a file on the current server. The data can only " +
"be downloaded to a script (.script, .ns, .js) or a text file (.txt). If the file already exists, " +
"it will be overwritten by this command.<br><br>" +
"Note that it will not be possible to download data from many websites because they do not allow " +
"cross-origin resource sharing (CORS). Example:<br><br>" +
"wget https://raw.githubusercontent.com/danielyxie/bitburner/master/README.md game_readme.txt",
}; };

@ -451,7 +451,7 @@ function loadImportedGame(saveObj, saveString) {
var popupId = "import-game-restart-game-notice"; var popupId = "import-game-restart-game-notice";
var txt = createElement("p", { var txt = createElement("p", {
innerText:"Imported game! I would suggest saving the game and then reloading the page " + innerText:"Imported game! You need to SAVE the game and then RELOAD the page " +
"to make sure everything runs smoothly" "to make sure everything runs smoothly"
}); });
var gotitBtn = createElement("a", { var gotitBtn = createElement("a", {

@ -741,7 +741,56 @@ let Terminal = {
$('input[class=terminal-input]').prop('disabled', false); $('input[class=terminal-input]').prop('disabled', false);
}, },
executeCommand: function(command) { writeToScriptFile : function(server, fn, code) {
var ret = {success: false, overwritten: false};
if (!isScriptFilename(fn) || !(server instanceof Server)) { return ret; }
//Check if the script already exists, and overwrite it if it does
for (let i = 0; i < server.scripts.length; ++i) {
if (fn === server.scripts[i].filename) {
let script = server.scripts[i];
script.code = code;
script.updateRamUsage();
script.module = "";
ret.overwritten = true;
ret.success = true;
return ret;
}
}
//Otherwise, create a new script
var newScript = new Script();
newScript.filename = fn;
newScript.code = code;
newScript.updateRamUsage();
newScript.server = server.ip;
server.scripts.push(newScript);
ret.success = true;
return ret;
},
writeToTextFile : function(server, fn, txt) {
var ret = {success: false, overwritten: false};
if (!fn.endsWith("txt") || !(server instanceof Server)) { return ret; }
//Check if the text file already exists, and overwrite if it does
for (let i = 0; i < server.textFiles.length; ++i) {
if (server.textFiles[i].fn === fn) {
ret.overwritten = true;
server.textFiles[i].text = txt;
ret.success = true;
return ret;
}
}
//Otherwise create a new text file
var newFile = new TextFile(fn, txt);
server.textFiles.push(newFile);
ret.success = true;
return ret;
},
executeCommand : function(command) {
command = command.trim(); command = command.trim();
//Replace all extra whitespace in command with a single space //Replace all extra whitespace in command with a single space
command = command.replace(/\s\s+/g, ' '); command = command.replace(/\s\s+/g, ' ');
@ -1395,8 +1444,7 @@ let Terminal = {
} }
} }
destServer.messages.push(scriptname); destServer.messages.push(scriptname);
post(scriptname + " copied over to " + destServer.hostname); return post(scriptname + " copied over to " + destServer.hostname);
return;
} }
//Scp for txt files //Scp for txt files
@ -1412,18 +1460,15 @@ let Terminal = {
if (!found) {return post("Error: no such file exists!");} if (!found) {return post("Error: no such file exists!");}
for (var i = 0; i < destServer.textFiles.length; ++i) { let tRes = Terminal.writeToTextFile(destServer, txtFile.fn, txtFile.text);
if (destServer.textFiles[i].fn === scriptname) { if (!tRes.success) {
//Overwrite return post("Error: scp failed");
destServer.textFiles[i].text = txtFile.text;
post("WARNING: " + scriptname + " already exists on " + destServer.hostname +
"and will be overwriten");
return post(scriptname + " copied over to " + destServer.hostname);
}
} }
var newFile = new TextFile(txtFile.fn, txtFile.text); if (tRes.overwritten) {
destServer.textFiles.push(newFile); post(`WARNING: ${scriptname} already exists on ${destServer.hostname} and will be overwriten`);
return post(scriptname + " copied over to " + destServer.hostname); return post(`${scriptname} overwritten on ${destServer.hostname}`);
}
return post(`${scriptname} copied over to ${destServer.hostname}`);
} }
//Get the current script //Get the current script
@ -1439,27 +1484,16 @@ let Terminal = {
return; return;
} }
//Overwrite script if it exists let sRes = Terminal.writeToScriptFile(destServer, scriptname, sourceScript.code);
for (var i = 0; i < destServer.scripts.length; ++i) { if (!sRes.success) {
if (scriptname == destServer.scripts[i].filename) { return post(`Error: scp failed`);
post("WARNING: " + scriptname + " already exists on " + destServer.hostname + " and will be overwritten");
var oldScript = destServer.scripts[i];
oldScript.code = sourceScript.code;
oldScript.ramUsage = sourceScript.ramUsage;
oldScript.module = "";
post(scriptname + " overwriten on " + destServer.hostname);
return;
}
} }
if (sRes.overwritten) {
var newScript = new Script(); post(`WARNING: ${scriptname} already exists on ${destServer.hostname} and will be overwritten`);
newScript.filename = scriptname; return post(`${scriptname} overwritten on ${destServer.hostname}`);
newScript.code = sourceScript.code; }
newScript.ramUsage = sourceScript.ramUsage; post(`${scriptname} copied over to ${destServer.hostname}`);
newScript.destServer = ip; break;
destServer.scripts.push(newScript);
post(scriptname + " copied over to " + destServer.hostname);
break;
case "sudov": case "sudov":
if (commandArray.length != 1) { if (commandArray.length != 1) {
post("Incorrect number of arguments. Usage: sudov"); return; post("Incorrect number of arguments. Usage: sudov"); return;
@ -1594,10 +1628,25 @@ let Terminal = {
let url = args[0]; let url = args[0];
let target = args[1]; let target = args[1];
if (!isScriptFilename(target) && !target.endsWith(".txt")) {
return post(`wget failed: Invalid target file. Target file must be script or text file`);
}
$.get(url, function(data) { $.get(url, function(data) {
post(data); let res;
if (isScriptFilename(target)) {
res = Terminal.writeToScriptFile(s, target, data);
} else {
res = Terminal.writeToTextFile(s, target, data);
}
if (!res.success) {
return post("wget failed");
}
if (res.overwritten) {
return post(`wget successfully retrieved content and overwrote ${target}`);
}
return post(`wget successfully retrieved content to new file ${target}`);
}, 'text').fail(function(e) { }, 'text').fail(function(e) {
post("wget failed: " + JSON.stringify(e)); return post("wget failed: " + JSON.stringify(e));
}) })
break; break;
default: default: