Added storage classes - work in progress III
This commit is contained in:
parent
99d0b95b29
commit
2e9a1efd23
@ -33,6 +33,7 @@ public interface Storage {
|
|||||||
public String cd(String path);
|
public String cd(String path);
|
||||||
|
|
||||||
default String cd() {
|
default String cd() {
|
||||||
|
cd("/");
|
||||||
mkdir("home");
|
mkdir("home");
|
||||||
cd("home");
|
cd("home");
|
||||||
mkdir("openeggbert");
|
mkdir("openeggbert");
|
||||||
@ -40,10 +41,14 @@ public interface Storage {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String mkdir(String name);
|
public String mkdir(String argument);
|
||||||
default String mkdir(String... name) {
|
default String mkdirmore(String... argument) {
|
||||||
|
// System.out.println("argumentCount=" + argument.length);
|
||||||
for(String n:name) {
|
// for(String z: argument){System.out.println(z);}
|
||||||
|
if(argument.length == 0) {
|
||||||
|
return "Missing argument";
|
||||||
|
}
|
||||||
|
for(String n:argument) {
|
||||||
String result = mkdir(n);
|
String result = mkdir(n);
|
||||||
if(!result.isEmpty()) {
|
if(!result.isEmpty()) {
|
||||||
return result;
|
return result;
|
||||||
|
@ -65,19 +65,16 @@ public class MapStorage implements Storage {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String cd(String path) {
|
public String cd(String path) {
|
||||||
String absolutePath = convertToAbsolutePathIfNeeded(path);
|
System.out.println("path="+path);
|
||||||
|
String absolutePath = path.equals(TWO_DOTS) ? getParentPath(workingDirectory) : convertToAbsolutePathIfNeeded(path);
|
||||||
|
|
||||||
if(path.equals(TWO_DOTS)) {
|
if (!exists(absolutePath)) {
|
||||||
getParentPath(workingDirectory);
|
final String msg = "Path does not exist: " + absolutePath;
|
||||||
}
|
|
||||||
|
|
||||||
if (!exists(path)) {
|
|
||||||
final String msg = "Path does not exist: " + path;
|
|
||||||
logError(msg);
|
logError(msg);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
if (!isdir(path)) {
|
if (!isdir(absolutePath)) {
|
||||||
final String msg = "Path is not directory: " + path;
|
final String msg = "Path is not directory: " + absolutePath;
|
||||||
logError(msg);
|
logError(msg);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
@ -95,6 +92,11 @@ public class MapStorage implements Storage {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String mkdir(String path) {
|
public String mkdir(String path) {
|
||||||
|
if(path.isEmpty()) {
|
||||||
|
String msg = "Missing argument";
|
||||||
|
logError(msg);
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
String absolutePath = convertToAbsolutePathIfNeeded(path);
|
String absolutePath = convertToAbsolutePathIfNeeded(path);
|
||||||
final String parentPath = getParentPath(absolutePath);
|
final String parentPath = getParentPath(absolutePath);
|
||||||
if (!path.equals(SLASH) && !exists(parentPath)) {
|
if (!path.equals(SLASH) && !exists(parentPath)) {
|
||||||
@ -120,11 +122,12 @@ public class MapStorage implements Storage {
|
|||||||
private static final String EIGHT_COLONS = "::::::::";
|
private static final String EIGHT_COLONS = "::::::::";
|
||||||
|
|
||||||
private static String getParentPath(String path) {
|
private static String getParentPath(String path) {
|
||||||
|
System.out.println("getParentPath()");
|
||||||
if (path == null) {
|
if (path == null) {
|
||||||
throw new OpenEggbertException("path is null");
|
throw new OpenEggbertException("Path is null");
|
||||||
}
|
}
|
||||||
if (path.trim().isEmpty()) {
|
if (path.trim().isEmpty()) {
|
||||||
throw new OpenEggbertException("path is empty");
|
throw new OpenEggbertException("Path is empty");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.equals("/")) {
|
if (path.equals("/")) {
|
||||||
|
@ -27,7 +27,7 @@ public interface StorageCommand {
|
|||||||
|
|
||||||
public String getName();
|
public String getName();
|
||||||
|
|
||||||
StorageCommandResult execute(String commandWithArguments);
|
StorageCommandResult execute(String arguments);
|
||||||
StorageCommandLine getStorageCommandLine();
|
StorageCommandLine getStorageCommandLine();
|
||||||
|
|
||||||
void setStorageCommandLine(StorageCommandLine storageCommandLine);
|
void setStorageCommandLine(StorageCommandLine storageCommandLine);
|
||||||
|
@ -20,12 +20,12 @@
|
|||||||
package com.openeggbert.lwjgl3.debugging.storage;
|
package com.openeggbert.lwjgl3.debugging.storage;
|
||||||
|
|
||||||
import com.openeggbert.storage.Storage;
|
import com.openeggbert.storage.Storage;
|
||||||
import com.openeggbert.storage.map.MemoryStorage;
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Scanner;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -36,23 +36,34 @@ import java.util.stream.Collectors;
|
|||||||
*/
|
*/
|
||||||
public class StorageCommandLine {
|
public class StorageCommandLine {
|
||||||
|
|
||||||
|
private String user;
|
||||||
|
private String hostname;
|
||||||
|
private Storage storage;
|
||||||
|
//
|
||||||
|
private boolean exited = false;
|
||||||
|
//
|
||||||
private long startNanoTime = System.nanoTime();
|
private long startNanoTime = System.nanoTime();
|
||||||
|
|
||||||
|
public String getCommandLineStart() {
|
||||||
|
return user + "@" + hostname + ":" + storage.pwd() + "$ ";
|
||||||
|
}
|
||||||
|
|
||||||
private String extractArgument(String arguments, int argumentIndex) {
|
private String extractArgument(String arguments, int argumentIndex) {
|
||||||
String[] array = arguments.split(" ");
|
String[] array = arguments.split(" ");
|
||||||
if(argumentIndex > (array.length - 1)) {
|
if (argumentIndex > (array.length - 1)) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
return array[argumentIndex];
|
return array[argumentIndex];
|
||||||
}
|
}
|
||||||
private StorageCommandLine(String userIn, String hostnameIn, Storage storageIn) {
|
|
||||||
|
public StorageCommandLine(String userIn, String hostnameIn, Storage storageIn) {
|
||||||
|
|
||||||
this.user = userIn;
|
this.user = userIn;
|
||||||
this.hostname = hostnameIn;
|
this.hostname = hostnameIn;
|
||||||
this.storage = storageIn;
|
this.storage = storageIn;
|
||||||
|
|
||||||
addCommand("whoami", arguments -> modifyResult(result -> result.setOutput(user)));
|
addCommand("whoami", arguments -> provideOutput(result -> result.setOutput(user)));
|
||||||
addCommand("uptime", arguments -> modifyResult(result
|
addCommand("uptime", arguments -> provideOutput(result
|
||||||
-> result.setOutput(
|
-> result.setOutput(
|
||||||
LocalDateTime.now().toString().replace("T", " ").substring(10, 19) + " up "
|
LocalDateTime.now().toString().replace("T", " ").substring(10, 19) + " up "
|
||||||
+ (System.nanoTime() - startNanoTime) / 1000000000l / 60l
|
+ (System.nanoTime() - startNanoTime) / 1000000000l / 60l
|
||||||
@ -60,31 +71,50 @@ public class StorageCommandLine {
|
|||||||
+ ", 1 user"
|
+ ", 1 user"
|
||||||
)));
|
)));
|
||||||
|
|
||||||
addCommand("hostname", arguments -> modifyResult(result -> result.setOutput(hostname)));
|
addCommand("hostname", arguments -> provideOutput(result -> result.setOutput(hostname)));
|
||||||
addCommand("uname", arguments -> modifyResult(result -> result.setOutput(
|
addCommand("uname", arguments -> provideOutput(result -> result.setOutput(
|
||||||
"LinuxBashCommandLinePartialEmulation"
|
"LinuxBashCommandLinePartialEmulation"
|
||||||
+
|
+ ((extractArgument(arguments, 0).equals("-a"))
|
||||||
((extractArgument(arguments, 0).equals("-a")) ?
|
? (hostname + " 0.0.0 ("
|
||||||
(hostname + " 0.0.0 ("
|
+ LocalDateTime.now().toString().replace("T", " ").substring(0, 10) + ")")
|
||||||
+
|
: "")
|
||||||
LocalDateTime.now().toString().replace("T", " ").substring(0, 10) + ")"
|
|
||||||
)
|
|
||||||
: "")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
)));
|
)));
|
||||||
|
|
||||||
|
addCommand("ls", arguments -> provideOutput(result -> result.setOutput(storage
|
||||||
|
.ls()
|
||||||
|
.stream()
|
||||||
|
.map(l -> {
|
||||||
|
String[] a = l.split("/");
|
||||||
// 12:31:18 up 2:10, 1 user
|
return a[a.length - 1];
|
||||||
|
}
|
||||||
|
).collect(Collectors.joining("\n")))));
|
||||||
|
|
||||||
|
addCommand("pwd", arguments -> provideOutput(result -> result.setOutput(storage.pwd())));
|
||||||
|
addCommand("depth", arguments -> provideOutput(result -> result.setOutput(storage.depth())));
|
||||||
|
|
||||||
|
addCommand("mkdir", arguments -> provideOutput(result
|
||||||
|
-> {
|
||||||
|
String string = storage.mkdirmore(extractArguments(arguments));
|
||||||
|
if (string.isEmpty()) {
|
||||||
|
result.setOutput("New directory was successfully created");
|
||||||
|
|
||||||
|
} else {
|
||||||
|
result.setErrorOutput("Creating new directory failed: " + string);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
));
|
||||||
|
|
||||||
commands.keySet().stream().map(k -> commands.get(k)).forEach(c -> c.setStorageCommandLine(this));
|
commands.keySet().stream().map(k -> commands.get(k)).forEach(c -> c.setStorageCommandLine(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
private StorageCommandResult modifyResult(Consumer<StorageCommandResult> consumer) {
|
private String[] extractArguments(String arguments) {
|
||||||
|
return Arrays.asList(arguments.split(" ")).stream()
|
||||||
|
.filter(a->!a.isEmpty())
|
||||||
|
.toArray(String[]::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
private StorageCommandResult provideOutput(Consumer<StorageCommandResult> consumer) {
|
||||||
|
|
||||||
StorageCommandResult result = StorageCommand.emptyNewResult();
|
StorageCommandResult result = StorageCommand.emptyNewResult();
|
||||||
consumer.accept(result);
|
consumer.accept(result);
|
||||||
@ -96,11 +126,6 @@ public class StorageCommandLine {
|
|||||||
commands.put(storageCommand.getName(), storageCommand);
|
commands.put(storageCommand.getName(), storageCommand);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String user;
|
|
||||||
private String hostname;
|
|
||||||
private Storage storage;
|
|
||||||
|
|
||||||
private boolean exited = false;
|
|
||||||
private final Map<String, StorageCommand> commands = new HashMap<>();
|
private final Map<String, StorageCommand> commands = new HashMap<>();
|
||||||
|
|
||||||
public String getUser() {
|
public String getUser() {
|
||||||
@ -136,81 +161,44 @@ public class StorageCommandLine {
|
|||||||
|
|
||||||
StorageCommandResult finalResult = new StorageCommandResult();
|
StorageCommandResult finalResult = new StorageCommandResult();
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "exit":
|
|
||||||
exited = true;
|
|
||||||
finalResult.setOutput("Exited");
|
|
||||||
break;
|
|
||||||
case "":
|
|
||||||
break;
|
|
||||||
case "ls":
|
|
||||||
String output = storage
|
|
||||||
.ls()
|
|
||||||
.stream()
|
|
||||||
.map(l -> {
|
|
||||||
String[] a = l.split("/");
|
|
||||||
return a[a.length - 1];
|
|
||||||
}
|
|
||||||
).collect(Collectors.joining("\n"));
|
|
||||||
finalResult.setOutput(output);
|
|
||||||
break;
|
|
||||||
case "pwd":
|
|
||||||
finalResult.setOutput(storage.pwd());
|
|
||||||
break;
|
|
||||||
|
|
||||||
case "depth":
|
|
||||||
finalResult.setOutput(storage.depth());
|
|
||||||
break;
|
|
||||||
case "mkdir":
|
|
||||||
if (checkArgumentCount(1, argumentCount)) {
|
|
||||||
String r = storage.mkdir(argument1.get());
|
|
||||||
if (r.isEmpty()) {
|
|
||||||
finalResult.setOutput("New directory was successfully created");
|
|
||||||
} else {
|
|
||||||
finalResult.setErrorOutput("Creating new directory failed: " + r);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "touch":
|
case "touch":
|
||||||
if (checkArgumentCount(1, argumentCount)) {
|
|
||||||
String result = storage.touch(argument1.get());
|
|
||||||
if (result.isEmpty()) {
|
|
||||||
finalResult.setOutput("New file was successfully created");
|
|
||||||
} else {
|
|
||||||
finalResult.setErrorOutput("Creating new directory failed: " + result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
String r = storage.touch(argument1.get());
|
||||||
|
if (r.isEmpty()) {
|
||||||
|
finalResult.setOutput("New file was successfully created");
|
||||||
|
} else {
|
||||||
|
finalResult.setErrorOutput("Creating new directory failed: " + r);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case "readtext":
|
case "readtext":
|
||||||
if (checkArgumentCount(1, argumentCount)) {
|
|
||||||
String result = storage.readtext(argument1.get());
|
|
||||||
if (result != null) {
|
|
||||||
finalResult.setOutput("Text file was successfully loaded" + "\n\n" + result);
|
|
||||||
} else {
|
|
||||||
finalResult.setErrorOutput("Loading text file failed:");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
String rr = storage.readtext(argument1.get());
|
||||||
|
if (rr != null) {
|
||||||
|
finalResult.setOutput("Text file was successfully loaded" + "\n\n" + rr);
|
||||||
|
} else {
|
||||||
|
finalResult.setErrorOutput("Loading text file failed:");
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "savetext":
|
case "savetext":
|
||||||
if (checkArgumentCount(2, argumentCount)) {
|
|
||||||
String result = storage.savetext(argument1.get(), argument2.get());
|
|
||||||
if (result.isEmpty()) {
|
|
||||||
finalResult.setOutput("Text file was successfully saved");
|
|
||||||
} else {
|
|
||||||
finalResult.setErrorOutput("Saving text file failed: " + result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
String result = storage.savetext(argument1.get(), argument2.get());
|
||||||
|
if (result.isEmpty()) {
|
||||||
|
finalResult.setOutput("Text file was successfully saved");
|
||||||
|
} else {
|
||||||
|
finalResult.setErrorOutput("Saving text file failed: " + result);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case "cd":
|
case "cd":
|
||||||
String r = argument1.isEmpty() ? storage.cd() : storage.cd(argument1.get());
|
String rrr = argument1.isEmpty() ? storage.cd() : storage.cd(argument1.get());
|
||||||
if (r.isEmpty()) {
|
if (rrr.isEmpty()) {
|
||||||
finalResult.setOutput("Changing working directory was successfully created");
|
finalResult.setOutput("Changing working directory was successfully created");
|
||||||
} else {
|
} else {
|
||||||
finalResult.setErrorOutput("Changing working directory failed: " + r);
|
finalResult.setErrorOutput("Changing working directory failed: " + rrr);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@ -218,6 +206,11 @@ public class StorageCommandLine {
|
|||||||
finalResult.setOutput(storage.debug());
|
finalResult.setOutput(storage.debug());
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case "exit":
|
||||||
|
exited = true;
|
||||||
|
finalResult.setOutput("Exited");
|
||||||
|
break;
|
||||||
default: {
|
default: {
|
||||||
finalResult.setErrorOutput("Unsupported command: " + command);
|
finalResult.setErrorOutput("Unsupported command: " + command);
|
||||||
}
|
}
|
||||||
@ -225,53 +218,4 @@ public class StorageCommandLine {
|
|||||||
return finalResult;
|
return finalResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
MemoryStorage memoryStorage = new MemoryStorage();
|
|
||||||
|
|
||||||
StorageCommandLine storageCommandWrapper = new StorageCommandLine("player", "openegggbert", memoryStorage);
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
System.out.print("player@openegggbert:" + memoryStorage.pwd() + "$ ");
|
|
||||||
String argument = scanner.nextLine();
|
|
||||||
|
|
||||||
StorageCommandResult result = storageCommandWrapper.execute(argument);
|
|
||||||
if (result.isError()) {
|
|
||||||
printError(result.getOutput());
|
|
||||||
} else {
|
|
||||||
print(result.getOutput());
|
|
||||||
|
|
||||||
}
|
|
||||||
if (storageCommandWrapper.isExited()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void print(int msg) {
|
|
||||||
print(String.valueOf(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void print(String msg) {
|
|
||||||
System.out.println(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void printError(String msg) {
|
|
||||||
System.err.println(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean checkArgumentCount(int wantedCount, int currentCount) {
|
|
||||||
// System.out.println("wantedCount=" + wantedCount);
|
|
||||||
// System.out.println("currentCount=" + currentCount);
|
|
||||||
boolean b = wantedCount < currentCount;
|
|
||||||
// System.out.println("b=" + b);
|
|
||||||
if (currentCount < wantedCount) {
|
|
||||||
|
|
||||||
printError("Wanted argument count is: " + wantedCount + ", but the current count of arguments is: " + currentCount);
|
|
||||||
// System.out.println("return false");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// System.out.println("return true");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,68 @@
|
|||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Open Eggbert: Free recreation of the computer game Speedy Eggbert.
|
||||||
|
// Copyright (C) 2024 the original author or authors.
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU General Public License
|
||||||
|
// as published by the Free Software Foundation, either version 3
|
||||||
|
// of the License, or (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see
|
||||||
|
// <https://www.gnu.org/licenses/> or write to the Free Software
|
||||||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
package com.openeggbert.lwjgl3.debugging.storage;
|
||||||
|
|
||||||
|
import com.openeggbert.storage.map.MemoryStorage;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author robertvokac
|
||||||
|
*/
|
||||||
|
public class StorageCommandLineScanner {
|
||||||
|
|
||||||
|
private StorageCommandLineScanner() {
|
||||||
|
//Not meant to be instantiated.
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
MemoryStorage memoryStorage = new MemoryStorage();
|
||||||
|
final String user = "player";
|
||||||
|
final String hostname = "openegggbert";
|
||||||
|
|
||||||
|
StorageCommandLine storageCommandLine = new StorageCommandLine(user, hostname, memoryStorage);
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
System.out.print(storageCommandLine.getCommandLineStart());
|
||||||
|
String argument = scanner.nextLine();
|
||||||
|
|
||||||
|
StorageCommandResult result = storageCommandLine.execute(argument);
|
||||||
|
if (result.isError()) {
|
||||||
|
printError(result.getOutput());
|
||||||
|
} else {
|
||||||
|
print(result.getOutput());
|
||||||
|
|
||||||
|
}
|
||||||
|
if (storageCommandLine.isExited()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void print(String msg) {
|
||||||
|
System.out.println(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void printError(String msg) {
|
||||||
|
System.err.println(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user