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

Merged
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
3be67d0
#737: Added cd command to shell commandlet
leonrohne27 Nov 6, 2024
60f2e8a
Merge branch 'main' into implement/737-add-cd-command-to-shell-comman…
leonrohne27 Nov 6, 2024
02cdeb0
Merge branch 'main' into implement/737-add-cd-command-to-shell-comman…
jan-vcapgemini Nov 7, 2024
2d41161
Merge branch 'main' into implement/737-add-cd-command-to-shell-comman…
jan-vcapgemini Nov 11, 2024
bad672f
Merge branch 'main' into implement/737-add-cd-command-to-shell-comman…
jan-vcapgemini Nov 14, 2024
b85ca18
Merge branch 'main' into implement/737-add-cd-command-to-shell-comman…
jan-vcapgemini Nov 18, 2024
51dd0c2
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/ShellComman…
leonrohne27 Nov 19, 2024
0bd754c
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/ShellComman…
leonrohne27 Nov 19, 2024
1af2948
#737: optimized cd command
leonrohne27 Nov 19, 2024
ec4d5f6
#737: optimized cd command
leonrohne27 Nov 19, 2024
06e9b8a
#737: Added Changelog entry
leonrohne27 Nov 19, 2024
17b5888
Merge branch 'main' into implement/737-add-cd-command-to-shell-comman…
jan-vcapgemini Nov 19, 2024
bcdd9bf
Merge branch 'main' into implement/737-add-cd-command-to-shell-comman…
jan-vcapgemini Nov 20, 2024
2e86be8
Merge branch 'main' into implement/737-add-cd-command-to-shell-comman…
hohwille Nov 28, 2024
d9aba17
#737: Changed cd without args and prompt String
leonrohne27 Nov 29, 2024
6f9865c
Merge branch 'main' into implement/737-add-cd-command-to-shell-comman…
hohwille Nov 29, 2024
7a7e137
Update cli/src/main/java/com/devonfw/tools/ide/commandlet/ShellComman…
leonrohne27 Nov 29, 2024
2ca5400
#737: Added changelog entry
leonrohne27 Nov 29, 2024
613b223
#737
leonrohne27 Nov 29, 2024
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.");
hohwille marked this conversation as resolved.
Show resolved Hide resolved
return -1;
hohwille marked this conversation as resolved.
Show resolved Hide resolved
}

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