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
28 changes: 26 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,33 @@
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Creates executable jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
Expand All @@ -34,4 +58,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
73 changes: 34 additions & 39 deletions src/main/java/com/mycmd/ShellContext.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
package com.mycmd;

import lombok.Getter;
import lombok.Setter;
import lombok.NonNull;
import lombok.AccessLevel;
import java.io.*;
import java.util.*;
import java.time.Instant;

@Getter(AccessLevel.PUBLIC)
public class ShellContext {

@Setter
@NonNull
private File currentDir;
private List<String> history;
private Map<String, String> aliases;

private static final String ALIAS_FILE = ".mycmd_aliases";
private static final int MAX_HISTORY = 100;

private final List<String> commandHistory;
private final Instant startTime;
private final Instant startTime;

private final Map<String, String> envVars = new HashMap<>();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add @Getter(AccessLevel.NONE) to the envVars field.

The envVars field has a custom getter at lines 54-56 that returns a defensive copy. Consistent with the recommendation for history and aliases, this field should also be annotated with @Getter(AccessLevel.NONE).

Apply this diff:

+    @Getter(AccessLevel.NONE)
     private final Map<String, String> envVars = new HashMap<>();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private final Map<String, String> envVars = new HashMap<>();
@Getter(AccessLevel.NONE)
private final Map<String, String> envVars = new HashMap<>();
🤖 Prompt for AI Agents
In src/main/java/com/mycmd/ShellContext.java around line 24, the envVars field
needs to prevent Lombok from generating a getter because a custom defensive-copy
getter already exists; add the annotation @Getter(AccessLevel.NONE) to the
private final Map<String, String> envVars = new HashMap<>(); and ensure
lombok.Getter and lombok.AccessLevel are imported (or existing imports include
them), so the generated getter is suppressed and the class uses the custom
getter at lines 54-56.


public ShellContext() {
this.currentDir = new File(System.getProperty("user.dir"));
Expand All @@ -22,39 +34,34 @@ public ShellContext() {
loadAliases();
}

public File getCurrentDir() {
return currentDir;
}

public void setCurrentDir(File dir) {
this.currentDir = dir;
}

public void addToHistory(String command) {
history.add(command);
commandHistory.add(command); // Add to command history
commandHistory.add(command);
if (history.size() > MAX_HISTORY) {
history.remove(0);
}
}


/** RETAINED FOR SAFETY: Returns a DEFENSIVE COPY instead of the raw Map. */
public List<String> getHistory() {
return new ArrayList<>(history);
}

public List<String> getCommandHistory() {
return commandHistory;
public Map<String, String> getAliases() {
return new HashMap<>(aliases);
}

public Instant getStartTime() {
return startTime;
public Map<String, String> getEnvVars() {
return new HashMap<>(envVars);
}


public void clearHistory() {
history.clear();
}

// Alias management methods
public void addAlias(String name, String command) {
aliases.put(name, command);
saveAliases();
Expand All @@ -64,21 +71,26 @@ public void removeAlias(String name) {
aliases.remove(name);
saveAliases();
}

public String getAlias(String name) {
return aliases.get(name);
}

public Map<String, String> getAliases() {
return new HashMap<>(aliases);
}

public boolean hasAlias(String name) {
return aliases.containsKey(name);
}

public void setEnvVar(String key, String value) {
envVars.put(key, value);
}

public String getEnvVar(String key) {
return envVars.get(key);
}

private void loadAliases() {
File aliasFile = new File(System.getProperty("user.home"), ALIAS_FILE);
// ... (method body remains the same)
if (!aliasFile.exists()) {
return;
}
Expand All @@ -104,6 +116,7 @@ private void loadAliases() {

private void saveAliases() {
File aliasFile = new File(System.getProperty("user.home"), ALIAS_FILE);
// ... (method body remains the same)
try (BufferedWriter writer = new BufferedWriter(new FileWriter(aliasFile))) {
writer.write("# MyCMD Aliases Configuration\n");
writer.write("# Format: aliasName=command\n\n");
Expand All @@ -130,22 +143,4 @@ public File resolvePath(String path) {
return new File(currentDir, path);
}
}

// environmental variable map
// author: Kaveesha Fernando
// date: 2024-06-10
private final Map<String, String> envVars = new HashMap<>();

public void setEnvVar(String key, String value) {
envVars.put(key, value);
}

public String getEnvVar(String key) {
return envVars.get(key);
}

public Map<String, String> getEnvVars() {
return new HashMap<>(envVars);
}

}
}