Skip to content

Commit 9b44ace

Browse files
committed
mkdir command with -p and -v flags
- Created MkdirCommand class that implements the Command interface. - Added flag parsing for -p and -v within the execute method. - Integrated path normalization to handle different path formats uniformly across platforms. - Enhanced the createDirectory method to mimic UNIX-like behavior.
1 parent 6395e72 commit 9b44ace

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed
Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,85 @@
11
package com.unixtools.command.filemanagement;
22

3-
public class MkdirCommand {
4-
3+
import com.unixtools.core.Command;
4+
5+
import java.io.File;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
public class MkdirCommand implements Command {
12+
private static final String VALID_FLAGS = "pv";
13+
14+
@Override
15+
public void execute(String[] args) {
16+
List<String> flags = new ArrayList<>();
17+
List<String> directories = new ArrayList<>();
18+
19+
parseArguments(args, flags, directories);
20+
if (!areFlagsValid(flags)) {
21+
System.out.println("Invalid flag(s). Valid flags are: " + VALID_FLAGS);
22+
return;
23+
}
24+
25+
if (directories.isEmpty()) {
26+
System.out.println("mkdir: No Directory Name provided");
27+
return;
28+
}
29+
30+
for (String dirName : directories) {
31+
createDirectory(dirName, flags.contains("p"), flags.contains("v"));
32+
}
33+
}
34+
35+
private void parseArguments(String[] args, List<String> flags, List<String> directories) {
36+
for (String arg : args) {
37+
if (arg.startsWith("-")) {
38+
for (char flag : arg.substring(1).toCharArray()) {
39+
flags.add(String.valueOf(flag));
40+
}
41+
} else {
42+
directories.add(arg);
43+
}
44+
}
45+
}
46+
47+
private boolean areFlagsValid(List<String> flags) {
48+
return flags.stream().allMatch(flag -> VALID_FLAGS.contains(flag));
49+
}
50+
51+
private void createDirectory(String dirName, boolean parentDirectories, boolean verbose) {
52+
String normalizedDirName = normalizePath(dirName);
53+
File directory = new File(normalizedDirName);
54+
55+
if (!parentDirectories && directory.getParentFile() != null && !directory.getParentFile().exists()) {
56+
System.out.println("mkdir: cannot create directory \'" + dirName + "\': No such file or directory");
57+
return;
58+
}
59+
60+
boolean exists = directory.exists();
61+
boolean created = exists ? false : (parentDirectories ? directory.mkdirs() : directory.mkdir());
62+
63+
if (verbose) {
64+
printVerboseMessage(dirName, exists, created);
65+
} else if (!created && !parentDirectories) {
66+
System.out.println("mkdir: cannot create directory \'" + dirName + "\': Already exists");
67+
}
68+
}
69+
70+
private String normalizePath(String path) {
71+
String normalizedPath = path.replace("\\", "/");
72+
Path pathObj = Paths.get(normalizedPath).normalize();
73+
return pathObj.toString();
74+
}
75+
76+
private void printVerboseMessage(String dirName, boolean exists, boolean created) {
77+
if (created) {
78+
System.out.println("mkdir: created directory \'" + dirName + "\'");
79+
} else if (exists) {
80+
System.out.println("mkdir: directory \'" + dirName + "\' already exists");
81+
} else {
82+
System.out.println("mkdir: error creating directory \'" + dirName + "\'");
83+
}
84+
}
585
}

src/com/unixtools/core/CommandFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.unixtools.core;
22

33
import com.unixtools.command.filemanagement.LsCommand;
4+
import com.unixtools.command.filemanagement.MkdirCommand;
45

56
public class CommandFactory {
67
public static Command getCommand(String commandName) {
78
switch (commandName) {
89
case "ls":
910
return new LsCommand();
11+
case "mkdir":
12+
return new MkdirCommand();
1013
default:
1114
return null;
1215
}

0 commit comments

Comments
 (0)