diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..26d3352
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml
new file mode 100644
index 0000000..a55e7a1
--- /dev/null
+++ b/.idea/codeStyles/codeStyleConfig.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
new file mode 100644
index 0000000..fb7f4a8
--- /dev/null
+++ b/.idea/compiler.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/gradle.xml b/.idea/gradle.xml
new file mode 100644
index 0000000..69921a0
--- /dev/null
+++ b/.idea/gradle.xml
@@ -0,0 +1,17 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
new file mode 100644
index 0000000..fdc392f
--- /dev/null
+++ b/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/libraries-with-intellij-classes.xml b/.idea/libraries-with-intellij-classes.xml
new file mode 100644
index 0000000..9fa3156
--- /dev/null
+++ b/.idea/libraries-with-intellij-classes.xml
@@ -0,0 +1,65 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..6ed36dd
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/commandline/commands/Cd.java b/src/main/java/commandline/commands/Cd.java
new file mode 100644
index 0000000..5034e91
--- /dev/null
+++ b/src/main/java/commandline/commands/Cd.java
@@ -0,0 +1,30 @@
+package commandline.commands;
+
+import java.awt.geom.Path2D;
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+
+/* Function cd
+ * if it has 0 arguments, then it change current directory to
+ * home directory
+ * if it has one argument, then it change current directory to new one from arg
+ */
+public class Cd implements Command {
+ @Override
+ public String run(List args, String before) {
+ if (args.isEmpty()) {
+ System.setProperty("user.dir", System.getProperty("user.home"));
+ } else {
+ var path = Paths.get(System.getProperty("user.dir"), args.get(1));
+ if (!Files.isDirectory(path)) {
+ return "no such directory";
+ }
+ System.setProperty("user.dir", path.toAbsolutePath().toString());
+ }
+ return "";
+ }
+
+}
diff --git a/src/main/java/commandline/commands/Ls.java b/src/main/java/commandline/commands/Ls.java
new file mode 100644
index 0000000..2d078d3
--- /dev/null
+++ b/src/main/java/commandline/commands/Ls.java
@@ -0,0 +1,38 @@
+package commandline.commands;
+
+import java.io.File;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.Objects;
+
+/* Function ls
+ * if it has 0 arguments, then it print all filenames and fir name
+ * in current directory, ignoring starting with ./ or ../
+ * if it has one argument, if it file, its just print filename
+ * if it directory, just print content from this directory
+ */
+public class Ls implements Command {
+ @Override
+ public String run(List args, String before) {
+ StringBuilder resString = new StringBuilder();
+ if (args.isEmpty()) {
+ File curDir = Paths.get(System.getProperty("user.dir")).toFile();
+ for (File file : Objects.requireNonNull(curDir.listFiles())) {
+ String name = file.getName();
+ resString.append(name).append(" ");
+ }
+ } else {
+ File curDir = Paths.get(System.getProperty("user.dir"), args.get(1)).toFile();
+ if (curDir.isDirectory()) {
+ for (File file : Objects.requireNonNull(curDir.listFiles())) {
+ resString.append(file.getName()).append(" ");
+ }
+ }
+ if (curDir.isFile()) {
+ resString.append(curDir.getName()).append(" ");
+ }
+ }
+ return resString.toString().trim();
+ }
+
+}
diff --git a/src/main/java/commandline/utils/CommandBuilder.java b/src/main/java/commandline/utils/CommandBuilder.java
index 1b0f0a6..6e591b9 100644
--- a/src/main/java/commandline/utils/CommandBuilder.java
+++ b/src/main/java/commandline/utils/CommandBuilder.java
@@ -7,20 +7,24 @@
* else returns null
*/
public class CommandBuilder {
- private CommandBuilder() {
- }
+ private CommandBuilder() {
+ }
- public static Command getCommand(String commandName) {
- if (commandName.equals("pwd")) {
- return new Pwd();
- } else if (commandName.equalsIgnoreCase("cat")) {
- return new Cat();
- } else if (commandName.equalsIgnoreCase("echo")) {
- return new Echo();
- } else if (commandName.equalsIgnoreCase("wc")) {
- return new Wc();
- } else {
- return null;
- }
+ public static Command getCommand(String commandName) {
+ if (commandName.equals("pwd")) {
+ return new Pwd();
+ } else if (commandName.equalsIgnoreCase("cat")) {
+ return new Cat();
+ } else if (commandName.equalsIgnoreCase("echo")) {
+ return new Echo();
+ } else if (commandName.equalsIgnoreCase("wc")) {
+ return new Wc();
+ } else if (commandName.equalsIgnoreCase("ls")) {
+ return new Ls();
+ } else if (commandName.equalsIgnoreCase("cd")) {
+ return new Cd();
+ } else {
+ return null;
}
+ }
}
diff --git a/src/test/java/commandline/CommandLineTest.java b/src/test/java/commandline/CommandLineTest.java
index 64c5dee..69137db 100644
--- a/src/test/java/commandline/CommandLineTest.java
+++ b/src/test/java/commandline/CommandLineTest.java
@@ -1,11 +1,15 @@
package commandline;
+import commandline.commands.Cd;
+import commandline.commands.Command;
+import commandline.commands.Ls;
import commandline.utils.ShellCommand;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.util.ArrayList;
// Testing
class CommandLineTest {
@@ -60,11 +64,11 @@ void testEcho() throws IOException, InterruptedException {
}
/*Test variables
- * a=p b=wd $a$b == command pwd
- * a=ec b=ho $a$b 5 == echo 5 == 5
- * a=ec b=ho echo $a$b == echo echo == echo
- * a=ec b=ho echo $a $b == echo ec ho == ec ho
- * */
+ * a=p b=wd $a$b == command pwd
+ * a=ec b=ho $a$b 5 == echo 5 == 5
+ * a=ec b=ho echo $a$b == echo echo == echo
+ * a=ec b=ho echo $a $b == echo ec ho == ec ho
+ * */
@Test
void testVars() throws IOException, InterruptedException {
String input = "a=p \n b=wd \n $a$b \n exit";
@@ -132,4 +136,53 @@ void testOtherCommands() {
input = "git lolol \n exit";
Assertions.assertEquals("Wrong command: git lolol\n", out(input));
}
+
+ @Test
+ void testLsCommands() {
+ Command ls = new Ls();
+ ArrayList lst = new ArrayList<>();
+ String expected = ".git .github .gradle .idea build build.gradle.kts gradle gradlew gradlew.bat README.md settings.gradle src testFiles";
+ Assertions.assertEquals(expected, ls.run(lst, ""));
+ lst.add("");
+ lst.add("src");
+ expected = "main test";
+ Assertions.assertEquals(expected, ls.run(lst, ""));
+ lst.clear();
+ expected = "README.MD";
+ lst.add("");
+ lst.add(expected);
+ Assertions.assertEquals(expected, ls.run(lst, ""));
+ }
+
+ @Test
+ void testCdCommands() throws IOException, InterruptedException {
+ Command cd = new Cd();
+ ArrayList lst = new ArrayList<>();
+ String res = System.getProperty("user.dir");
+ cd.run(lst, "");
+ Assertions.assertEquals(System.getProperty("user.home"), System.getProperty("user.dir"));
+ lst.add("");
+ lst.add("main");
+ cd.run(lst, "");
+ Assertions.assertEquals("D:\\programming\\sd_fork\\CLI\\main", res + "\\" + "main");
+ String input = "ls \n exit";
+ Assertions.assertEquals(ShellCommand.executeCommand(input), out(input));
+
+ input = "cd gradle | ls \n exit";
+ Assertions.assertEquals(ShellCommand.executeCommand(input), out(input));
+
+ input = "cd build | ls classes \n exit";
+ Assertions.assertEquals(ShellCommand.executeCommand(input), out(input));
+
+ input = "cd build | cd classes | ls \n exit";
+ Assertions.assertEquals(ShellCommand.executeCommand(input), out(input));
+
+ input = "cd build | cd classes | ls java \n exit";
+ Assertions.assertEquals(ShellCommand.executeCommand(input), out(input));
+
+ input = "cd build | cd classes | cd java | ls \n exit";
+ Assertions.assertEquals(ShellCommand.executeCommand(input), out(input));
+
+ }
+
}