Skip to content

Commit 6d78465

Browse files
Merge pull request #29 from Kaveeshakavindi/feat/Kaveesha/touch-command
feat: implemented and tested touch command
2 parents d447025 + 0768044 commit 6d78465

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/main/java/com/mycmd/App.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ private static void registerCommands(Map<String, Command> commands) {
5151
commands.put("help", new HelpCommand(commands));
5252
commands.put("exit", new ExitCommand());
5353
commands.put("ver", new VersionCommand());
54+
commands.put("touch", new TouchCommand());
5455
commands.put("time", new TimeCommand());
5556
commands.put("date", new DateCommand());
5657
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.mycmd.commands;
2+
3+
import com.mycmd.Command;
4+
import com.mycmd.ShellContext;
5+
import java.io.File;
6+
import java.io.IOException;
7+
8+
public class TouchCommand implements Command {
9+
@Override
10+
public void execute(String[] args, ShellContext context) throws IOException {
11+
if (args.length < 1) { // ✅ Check for at least 1 argument
12+
System.out.println("Usage: touch <filename>");
13+
return;
14+
}
15+
16+
File file = new File(context.getCurrentDir(), args[0]); // ✅ Use args[0]
17+
if (file.createNewFile()) {
18+
System.out.println("File created: " + args[0]); // ✅ Use args[0]
19+
} else {
20+
// Update timestamp
21+
file.setLastModified(System.currentTimeMillis());
22+
System.out.println("File timestamp updated: " + args[0]); // ✅ Use args[0]
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)