/** * Returns the command line as an array of strings. * * @return The command line as an string array */ public String[] toStrings() { final String[] result = new String[arguments.size() + 1]; result[0] = this.getExecutable(); System.arraycopy(getArguments(), 0, result, 1, result.length-1); return result; }
protected int doExecution(DefaultExecutor executor, CommandLine commandLine, Map<Object, Object> env) { try { final StringJoiner paramsJoiner = new StringJoiner(" "); for (String cmdArg : commandLine.toStrings()) { paramsJoiner.add(cmdArg.replaceAll("(?<=\\.password=)(.*)", "****")); } logger.info("Executing: " + paramsJoiner.toString()); return executor.execute(commandLine, env); } catch (ExecuteException e) { return e.getExitValue(); } catch (Exception e) { throw new MuleControllerException("Error executing [" + commandLine.getExecutable() + " " + commandLine.getArguments() + "]", e); } }
out.print(command); final String[] args = cmd.getArguments(); for (final String arg : args) { out.println(" -");
/** * Parse parameters to be passed as arguments to the groovy binary * */ private String[] parseParams(String line) { //JENKINS-24870 CommandLine.getExecutable tries to fix file separators, so if the first param contains slashes, it can cause problems //Adding some placeholder instead of executable CommandLine cmdLine = CommandLine.parse("executable_placeholder " + line); String[] parsedArgs = cmdLine.getArguments(); String[] args = new String[parsedArgs.length]; if(parsedArgs.length > 0) { System.arraycopy(parsedArgs, 0, args, 0, parsedArgs.length); } return args; }
private static Function<CommandLine, CommandLine> getLinuxShellWrapperFunc() { return (cmd) -> { final String originalCmd = Stream.concat( Arrays.stream(new String[]{cmd.getExecutable()}), Arrays.stream(cmd.getArguments()) ).collect(Collectors.joining(" ")); return new CommandLine("/bin/sh") .addArgument("-c") .addArgument(originalCmd, false); }; }
CommandLine commandLineAdditionalParams = CommandLine.parse(additionalMLCPParameters); mlcpCommand.add(commandLineAdditionalParams.getExecutable()); mlcpCommand.addAll(Arrays.asList(commandLineAdditionalParams.getArguments()));
CommandLine commandLineAdditionalParams = CommandLine.parse(additionalMLCPParameters); mlcpCommand.add(commandLineAdditionalParams.getExecutable()); mlcpCommand.addAll(Arrays.asList(commandLineAdditionalParams.getArguments()));
private Protos.CommandInfo buildCommand(final Protos.CommandInfo.URI uri, final String script, final ShardingContexts shardingContexts, final boolean isCommandExecutor) { Protos.CommandInfo.Builder result = Protos.CommandInfo.newBuilder().addUris(uri).setShell(true); if (isCommandExecutor) { CommandLine commandLine = CommandLine.parse(script); commandLine.addArgument(GsonFactory.getGson().toJson(shardingContexts), false); result.setValue(Joiner.on(" ").join(commandLine.getExecutable(), Joiner.on(" ").join(commandLine.getArguments()))); } else { result.setValue(script); } return result.build(); }
@Test public void testCommandArgs() { CommandLine c = new CommandLine("/usr/local/Cellar/ansible/2.4.3.0/libexec/bin/ansible"); c.addArgument("--version"); // c.addArgument("-ef"); // c.addArgument("|"); // c.addArgument("grep a"); for(String s:c.getArguments()) System.err.println(s); String rt = CmdUtils.run(c); System.err.println(rt); }