-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.java
More file actions
327 lines (262 loc) · 10.3 KB
/
Commands.java
File metadata and controls
327 lines (262 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public abstract class Commands {
public String cdCommand(String[] args) {
if (args.length == 0) {
// Case 1: 'cd' with no arguments - change to the home directory.
String userHome = System.getProperty("user.home");
System.setProperty("user.dir", userHome);
return userHome;
} else if (args.length == 1 || args.length == 2 || args.length == 3) {
if (args[0].equals("..")) {
// Case 2: 'cd ..' - change to the previous directory.
String currentDir = System.getProperty("user.dir");
File currentDirectory = new File(currentDir);
String parentDir = currentDirectory.getParent();
if (parentDir != null) {
System.setProperty("user.dir", parentDir);
return (parentDir);
} else {
return ("Already at the root directory.");
}
} else {
// Case 3: 'cd <path>' - change to the specified directory.
File newDir = new File(args[0]);
if (newDir.isAbsolute()) {
if (newDir.isDirectory()) {
System.setProperty("user.dir", newDir.getAbsolutePath());
return (newDir.toString());
} else {
return ("Not a directory: " + args[0]);
}
} else {
String currentDir = System.getProperty("user.dir");
File targetDir = new File(currentDir, args[0]);
if (targetDir.isDirectory()) {
System.setProperty("user.dir", targetDir.getAbsolutePath());
return (targetDir.toString());
} else {
return ("Directory not found: " + args[0]);
}
}
}
} else {
return ("Invalid usage. Usage: cd [<path>|..]");
}
}
public String lsCommand(String[] args) {
File currentDir = new File(System.getProperty("user.dir"));
File[] files = currentDir.listFiles();
String ans = "";
if (args.length == 0) {
// Case 1: 'ls' - list contents of the current directory alphabetically.
if (files != null) {
Arrays.sort(files);
for (File file : files) {
// System.out.println(file.getName());
ans += (file.getName() + " \n");
}
}
} else if (args.length == 1 && args[0].equals("-r")) {
// Case 2: 'ls -r' - list contents of the current directory in reverse order.
if (files != null) {
Arrays.sort(files, (f1, f2) -> f2.getName().compareTo(f1.getName()));
for (File file : files) {
// System.out.println(file.getName());
ans += (file.getName() + " \n");
}
}
} else {
// System.out.println("Invalid usage. Usage: ls [-r]");
ans = "Invalid usage. Usage: ls [-r]";
}
return ans;
}
public String touchCommand(String fileName) {
File currentDir = new File(fileName);
try {
if (!currentDir.exists()) {
boolean fileCreated = currentDir.createNewFile();
if (fileCreated) {
return ("Creating file: " + fileName);
} else {
return ("The file is not created.");
}
} else {
return ("The file is already created.");
}
} catch (IOException e) {
e.printStackTrace();
return "Error!";
}
}
public String cpCommand(String source, String destination) {
// if the destination file doesn't exists, create a new one, but if soucre doesn't exists, return void
File start = new File(source), end = new File(destination);
Path src = Paths.get(source), des = Paths.get(destination);
if (!start.exists()){
return "The Source File doesn't exists.";
} else if (!end.exists()) {
try {
end.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return "Error creating the destination file: " + e.getMessage();
}
}
try {
Files.copy(src, des, StandardCopyOption.REPLACE_EXISTING);
return "Created Successfully";
} catch (IOException e) {
e.printStackTrace();
return "Error !";
}
}
public String cprCommand(File source, File destination) {
// if the destination file doesn't exists, create a new one, but if soucre doesn't exists, return void
try {
if (source.isDirectory()) {
if (!destination.exists()) {
destination.mkdirs();
}
String[] files = source.list();
for (String file : files) {
File srcFile = new File(source, file);
File destFile = new File(destination, file);
cprCommand(srcFile, destFile);
}
return "";
} else {
Files.copy(source.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING);
return "Successfully";
}
} catch (IOException e) {
e.printStackTrace();
return "Error";
}
}
public String rmCommand(String[] args) {
switch (args.length) {
case 1:
File file = new File(args[0]);
if (file.exists()) {
if (file.delete()) {
return ("File Deleted .");
}
else {
return ("Failed to Delete !");
}
}
else {
return ("No Such File Exists !");
}
default:
return ("Invalid Usage ! Usage : rm <filename>");
}
}
public String catCommand(String[] args) {
String ansStr = "";
switch (args.length) {
case 1:
File file = new File(args[0]);
try {
List<String> ans = Files.readAllLines(Paths.get(args[0]));
for (String line : ans)
ansStr += (line);
return ansStr;
}
catch (IOException e) {
if (e instanceof NoSuchFileException)
ansStr = ("No Such File exists !");
return ansStr;
}
case 2:
String[] files = { args[0], args[1] };
List<String> ans = new ArrayList<String>();
try {
for (String obj : files) {
ans.addAll(Files.readAllLines(Paths.get(obj)));
}
for (String line : ans)
ansStr += (line);
return ansStr;
}
catch (IOException e) {
if (e instanceof NoSuchFileException)
ansStr = ("No Such Files Exist !");
return ansStr;
}
default:
return ("Invalid Usage ! Usage : cat <file_name> <file_name>(optional)");
}
}
public String wcCommand(String[] args) {
String ansStr = "";
switch (args.length) {
case 1:
File file = new File(args[0]);
int lineCount = 0, wordCount = 0, characterCount = 0;
try {
List<String> ans = Files.readAllLines(Paths.get(args[0]));
lineCount = ans.size();
for (String line : ans) {
String[] words = line.trim().split(" ");
wordCount += words.length;
for (String word : words)
characterCount += word.length();
}
ansStr = (lineCount + " " + wordCount + " " + characterCount + " " + args[0]);
return ansStr;
}
catch (IOException e) {
e.printStackTrace();
if (e instanceof NoSuchFileException)
ansStr = "File Not Found !";
return ansStr;
}
default:
return ("Invalid Usage ! Usage : wc <filename>");
}
}
public String mkdir(String[] newDirs) {
String targetPath = newDirs[newDirs.length - 1]; // Get the last element as the target path
File targetDir = new File(targetPath);
String result = "";
// full path not given
if (!targetDir.exists()) {
String currentDir = System.getProperty("user.dir");
for (String dir : newDirs) {
result += createDirectory(currentDir + File.separator + dir);
}
} else { // if full path is given
for (int i = 0; i < newDirs.length - 1; i++) {
result += createDirectory(targetPath + File.separator + newDirs[i]);
}
}
return result;
}
private String createDirectory(String directoryPath) {
File directory = new File(directoryPath);
String message = "";
if (directory.exists()) {
message = "Directory already exists: " + directory.getAbsolutePath() + "\n";
} else {
boolean created = directory.mkdir();
if (created) {
message = "";
} else {
message = "Failed to create directory: " + directory.getAbsolutePath() + "\n";
}
}
return message;
}
}