cstamas commented on code in PR #2117: URL: https://github.com/apache/maven/pull/2117#discussion_r1967550310
########## impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/BuiltinShellCommandRegistryFactory.java: ########## @@ -130,53 +130,133 @@ public String name() { return "Builtin Maven Shell commands"; } - private List<Completers.OptDesc> commandOptions(String command) { + private void shell(CommandInput input) { + if (input.args().length > 0) { + try { + ProcessBuilder builder = new ProcessBuilder(); + List<String> processArgs = new ArrayList<>(); + if (Os.IS_WINDOWS) { + processArgs.add("cmd.exe"); + processArgs.add("/c"); + } else { + processArgs.add("sh"); + processArgs.add("-c"); + } + processArgs.add(String.join(" ", Arrays.asList(input.args()))); + builder.command(processArgs); + builder.directory(shellContext.cwd.get().toFile()); + Process process = builder.start(); + Thread out = new Thread(new StreamGobbler(process.getInputStream(), shellContext.logger::info)); + Thread err = new Thread(new StreamGobbler(process.getErrorStream(), shellContext.logger::error)); + out.start(); + err.start(); + int exitCode = process.waitFor(); + out.join(); + err.join(); + if (exitCode != 0) { + shellContext.logger.error("Shell command exited with code " + exitCode); + } + } catch (Exception e) { + saveException(e); + } + } + } + + private void cd(CommandInput input) { try { - invoke(new CommandSession(), command, "--help"); - } catch (Options.HelpException e) { - return compileCommandOptions(e.getMessage()); + if (input.args().length == 1) { + shellContext.cwd.change(input.args()[0]); + } else { + shellContext.writer.accept("Error: 'cd' accepts only one argument"); + } } catch (Exception e) { - // ignore + saveException(e); + } + } + + private List<Completer> cdCompleter(String name) { + return List.of(new ArgumentCompleter(new Completers.DirectoriesCompleter(shellContext.cwd))); + } + + private void pwd(CommandInput input) { + try { + shellContext.writer.accept(shellContext.cwd.get().toString()); Review Comment: command output does not go to logger, EXCEPT for: * shell stderr * exitCode != 0 * command wrongly invoked by user (cd with more than 1 or 0 param) These above go to context.logger.error ########## impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnsh/builtin/BuiltinShellCommandRegistryFactory.java: ########## @@ -130,53 +130,133 @@ public String name() { return "Builtin Maven Shell commands"; } - private List<Completers.OptDesc> commandOptions(String command) { + private void shell(CommandInput input) { + if (input.args().length > 0) { + try { + ProcessBuilder builder = new ProcessBuilder(); + List<String> processArgs = new ArrayList<>(); + if (Os.IS_WINDOWS) { + processArgs.add("cmd.exe"); + processArgs.add("/c"); + } else { + processArgs.add("sh"); + processArgs.add("-c"); + } + processArgs.add(String.join(" ", Arrays.asList(input.args()))); + builder.command(processArgs); + builder.directory(shellContext.cwd.get().toFile()); + Process process = builder.start(); + Thread out = new Thread(new StreamGobbler(process.getInputStream(), shellContext.logger::info)); + Thread err = new Thread(new StreamGobbler(process.getErrorStream(), shellContext.logger::error)); + out.start(); + err.start(); + int exitCode = process.waitFor(); + out.join(); + err.join(); + if (exitCode != 0) { + shellContext.logger.error("Shell command exited with code " + exitCode); + } + } catch (Exception e) { + saveException(e); + } + } + } + + private void cd(CommandInput input) { try { - invoke(new CommandSession(), command, "--help"); - } catch (Options.HelpException e) { - return compileCommandOptions(e.getMessage()); + if (input.args().length == 1) { + shellContext.cwd.change(input.args()[0]); + } else { + shellContext.writer.accept("Error: 'cd' accepts only one argument"); + } } catch (Exception e) { - // ignore + saveException(e); + } + } + + private List<Completer> cdCompleter(String name) { + return List.of(new ArgumentCompleter(new Completers.DirectoriesCompleter(shellContext.cwd))); + } + + private void pwd(CommandInput input) { + try { + shellContext.writer.accept(shellContext.cwd.get().toString()); Review Comment: command output does not go to logger, EXCEPT for: * shell stderr * exitCode != 0 * command wrongly invoked by user (cd with more than 1 or 0 param) These above go to context.logger.error -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org