Repository navigation
Expand file tree
/
Copy pathCat.java
More file actions
53 lines (46 loc) · 1.71 KB
/
Copy pathCat.java
File metadata and controls
53 lines (46 loc) · 1.71 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
package commands;
import java.util.List;
import driver.IShellState;
import file_system.File;
import helper_classes.PathHandler;
/**
* Cat class Returns content of the file(s) at path(s) that are provided
*/
public class Cat extends Command {
public Cat() {
super(100, 1);
}
/**
* Returns content of the file(s) at path(s) that are provided
* @param shellState is the state of the program
* @param arguments is list of arguments that were passed along with command
* @throws Exception if path is invalid
*/
public String executeCommand(IShellState shellState, List<String> arguments)
throws Exception {
/*
* loop through each path in the argument list
* and get the directory of the requirement file and its name
*/
checkArgumentsNum(arguments);
StringBuilder output = new StringBuilder();
for (String path: arguments) {
try {
File filetoCat = PathHandler.getFileByPath(shellState, path);
/*
* if this is the first file get cat, do not include line break
* else concatenate the current file after the previous
* file with 3 lines break
*/
output.append((output.toString().equals("")) ?
filetoCat.getContent() : ("\n\n" + filetoCat.getContent()));
}
catch (Exception e) {
String errorString = "cannot view file " + path + ": " + e.getMessage();
output.append((output.toString().equals("")) ?
errorString : "\n\n" + errorString);
}
}
return output.toString();
}
}