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:
"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>" +
"theme [preset] | bg txt hlgt Change the color scheme of the UI<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> {
[key: string]: T;
@ -214,5 +215,11 @@ export const HelpTexts: IMap<string> = {
"Then it could be removed using:<br><br>" +
'unalias "r"<br><br>' +
"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 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"
});
var gotitBtn = createElement("a", {

@ -741,7 +741,56 @@ let Terminal = {
$('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();
//Replace all extra whitespace in command with a single space
command = command.replace(/\s\s+/g, ' ');
@ -1395,8 +1444,7 @@ let Terminal = {
}
}
destServer.messages.push(scriptname);
post(scriptname + " copied over to " + destServer.hostname);
return;
return post(scriptname + " copied over to " + destServer.hostname);
}
//Scp for txt files
@ -1412,18 +1460,15 @@ let Terminal = {
if (!found) {return post("Error: no such file exists!");}
for (var i = 0; i < destServer.textFiles.length; ++i) {
if (destServer.textFiles[i].fn === scriptname) {
//Overwrite
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);
let tRes = Terminal.writeToTextFile(destServer, txtFile.fn, txtFile.text);
if (!tRes.success) {
return post("Error: scp failed");
}
if (tRes.overwritten) {
post(`WARNING: ${scriptname} already exists on ${destServer.hostname} and will be overwriten`);
return post(`${scriptname} overwritten on ${destServer.hostname}`);
}
var newFile = new TextFile(txtFile.fn, txtFile.text);
destServer.textFiles.push(newFile);
return post(scriptname + " copied over to " + destServer.hostname);
return post(`${scriptname} copied over to ${destServer.hostname}`);
}
//Get the current script
@ -1439,26 +1484,15 @@ let Terminal = {
return;
}
//Overwrite script if it exists
for (var i = 0; i < destServer.scripts.length; ++i) {
if (scriptname == destServer.scripts[i].filename) {
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;
let sRes = Terminal.writeToScriptFile(destServer, scriptname, sourceScript.code);
if (!sRes.success) {
return post(`Error: scp failed`);
}
if (sRes.overwritten) {
post(`WARNING: ${scriptname} already exists on ${destServer.hostname} and will be overwritten`);
return post(`${scriptname} overwritten on ${destServer.hostname}`);
}
var newScript = new Script();
newScript.filename = scriptname;
newScript.code = sourceScript.code;
newScript.ramUsage = sourceScript.ramUsage;
newScript.destServer = ip;
destServer.scripts.push(newScript);
post(scriptname + " copied over to " + destServer.hostname);
post(`${scriptname} copied over to ${destServer.hostname}`);
break;
case "sudov":
if (commandArray.length != 1) {
@ -1594,10 +1628,25 @@ let Terminal = {
let url = args[0];
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) {
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) {
post("wget failed: " + JSON.stringify(e));
return post("wget failed: " + JSON.stringify(e));
})
break;
default: