Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#737: Added cd command to shell commandlet #748

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Changes from 6 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.devonfw.tools.ide.commandlet;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;

import org.fusesource.jansi.AnsiConsole;
Expand Down Expand Up @@ -129,9 +132,40 @@ private int runCommand(String args) {
String[] arguments = args.split(" ", 0);
CliArguments cliArgs = new CliArguments(arguments);
cliArgs.next();

if ("cd".equals(arguments[0])) {
return changeDirectory(cliArgs);
}

return ((AbstractIdeContext) this.context).run(cliArgs);
}

private int changeDirectory(CliArguments cliArgs) {
if (!cliArgs.hasNext()) {
this.context.error("Error: 'cd' requires a directory argument.");
return -1;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure about the error code.

}

String targetDir = String.valueOf(cliArgs.next());
Path path = Paths.get(targetDir);

// If the given path is relative, resolve it relative to the current directory
if (!path.isAbsolute()) {
path = Paths.get(System.getProperty("user.dir")).resolve(path).normalize();
leonrohne27 marked this conversation as resolved.
Show resolved Hide resolved
}

// Check if the path exists and is a directory
if (Files.exists(path) && Files.isDirectory(path)) {
// Set the current working directory to the new path
System.setProperty("user.dir", path.toAbsolutePath().toString());
leonrohne27 marked this conversation as resolved.
Show resolved Hide resolved
this.context.info("Changed directory to: " + path.toAbsolutePath());
return 0;
} else {
this.context.error("Error: Directory not found: " + targetDir);
return -1;
}
}

/**
* @param argument the current {@link CliArgument} (position) to match.
* @param commandlet the potential {@link Commandlet} to match.
Expand Down
Loading