Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/com/mycmd/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private static void registerCommands(Map<String, Command> commands) {
commands.put("help", new HelpCommand(commands));
commands.put("exit", new ExitCommand());
commands.put("ver", new VersionCommand());
commands.put("touch", new TouchCommand());
commands.put("time", new TimeCommand());
commands.put("date", new DateCommand());
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/mycmd/commands/TouchCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mycmd.commands;

import com.mycmd.Command;
import com.mycmd.ShellContext;
import java.io.File;
import java.io.IOException;

public class TouchCommand implements Command {
@Override
public void execute(String[] args, ShellContext context) throws IOException {
if (args.length < 1) { // ✅ Check for at least 1 argument
System.out.println("Usage: touch <filename>");
return;
}

File file = new File(context.getCurrentDir(), args[0]); // ✅ Use args[0]
if (file.createNewFile()) {
System.out.println("File created: " + args[0]); // ✅ Use args[0]
} else {
// Update timestamp
file.setLastModified(System.currentTimeMillis());
System.out.println("File timestamp updated: " + args[0]); // ✅ Use args[0]
}
}
}