01: package org.mvel.sh.command.file;
02:
03: import org.mvel.sh.Command;
04: import org.mvel.sh.CommandException;
05: import org.mvel.sh.ShellSession;
06:
07: import java.util.Map;
08: import java.io.File;
09:
10: public class ChangeWorkingDir implements Command {
11:
12: public Object execute(ShellSession session, String[] args) {
13: File cwd = new File(session.getEnv().get("$CWD"));
14:
15: if (args.length == 0 || ".".equals(args[0]))
16: return null;
17: else if ("..".equals(args[0])) {
18: if (cwd.getParentFile() != null) {
19: cwd = cwd.getParentFile();
20: } else {
21: throw new CommandException(
22: "already at top-level directory");
23: }
24: } else if (args[0].charAt(0) == '/') {
25: cwd = new File(args[0]);
26: if (!cwd.exists()) {
27: throw new CommandException("no such directory: "
28: + args[0]);
29: }
30: } else {
31: cwd = new File(cwd.getAbsolutePath() + "/" + args[0]);
32: if (!cwd.exists()) {
33: throw new CommandException("no such directory: "
34: + args[0]);
35: }
36: }
37:
38: session.getEnv().put("$CWD", cwd.getAbsolutePath());
39:
40: return null;
41:
42: }
43:
44: public String getDescription() {
45: return "changes the working directory";
46: }
47:
48: public String getHelp() {
49: return "no help yet";
50: }
51: }
|