Skip to content

Commit 95dec24

Browse files
Merge pull request #74 from Kaveeshakavindi/feat/Kaveesha/setCommand
Feat/kaveesha/set command
2 parents 343f77e + a0d4e3b commit 95dec24

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ private static void registerCommands(Map<String, Command> commands) {
9292
commands.put("whoami", new WhoamiCommand());
9393
commands.put("touch", new TouchCommand());
9494
commands.put("time", new TimeCommand());
95+
commands.put("tasklist", new TasklistCommand());
9596
commands.put("tree", new TreeCommand());
9697
commands.put("date", new DateCommand());
9798
commands.put("history", new HistoryCommand());
@@ -104,6 +105,8 @@ private static void registerCommands(Map<String, Command> commands) {
104105
commands.put("alias", new AliasCommand());
105106
commands.put("unalias", new UnaliasCommand());
106107
commands.put("rename", new RenameCommand());
107-
commands.put("tasklist", new TasklistCommand());
108-
}
108+
commands.put("set", new SetCommand());
109+
}
109110
}
111+
112+

src/main/java/com/mycmd/ShellContext.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,22 @@ public File resolvePath(String path) {
130130
return new File(currentDir, path);
131131
}
132132
}
133+
134+
// environmental variable map
135+
// author: Kaveesha Fernando
136+
// date: 2024-06-10
137+
private final Map<String, String> envVars = new HashMap<>();
138+
139+
public void setEnvVar(String key, String value) {
140+
envVars.put(key, value);
141+
}
142+
143+
public String getEnvVar(String key) {
144+
return envVars.get(key);
145+
}
146+
147+
public Map<String, String> getEnvVars() {
148+
return new HashMap<>(envVars);
149+
}
150+
133151
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.mycmd.commands;
2+
3+
import com.mycmd.*;
4+
import java.io.IOException;
5+
import java.util.Map;
6+
7+
public class SetCommand implements Command {
8+
9+
@Override
10+
public void execute(String[] args, ShellContext context) throws IOException {
11+
if (args.length == 0) {
12+
// Print all environment variables
13+
for (Map.Entry<String, String> entry : context.getEnvVars().entrySet()) {
14+
System.out.println(entry.getKey() + "=" + entry.getValue());
15+
}
16+
return;
17+
}
18+
19+
String arg = args[0];
20+
if (arg.contains("=")) {
21+
String[] parts = arg.split("=", 2);
22+
String key = parts[0].trim();
23+
String value = parts[1].trim();
24+
context.setEnvVar(key, value);
25+
System.out.println("Variable set: " + key + "=" + value);
26+
} else {
27+
String value = context.getEnvVar(arg);
28+
if (value != null) {
29+
System.out.println(arg + "=" + value);
30+
} else {
31+
System.out.println("Variable not found: " + arg);
32+
}
33+
}
34+
}
35+
36+
@Override
37+
public String description() {
38+
return "Displays, sets, or retrieves shell environment variables.";
39+
}
40+
41+
@Override
42+
public String usage() {
43+
return "Usage: set [variable[=value]]";
44+
}
45+
}

0 commit comments

Comments
 (0)