Add primary command feature

- NonInteractiveShellRunner can now shortcircuit into primary command
  just running it and passing args.
- Add hooks into autoconfig so that this is easy to configure.
- Relates #755
This commit is contained in:
Janne Valkealahti
2023-07-05 10:33:55 +01:00
parent 4ee1907f00
commit 32675c5e73
7 changed files with 169 additions and 35 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2021-2022 the original author or authors.
* Copyright 2021-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -60,12 +60,19 @@ public class NonInteractiveShellRunner implements ShellRunner {
private Parser lineParser;
private String primaryCommand;
private static final String SINGLE_QUOTE = "\'";
private static final String DOUBLE_QUOTE = "\"";
private Function<ApplicationArguments, List<String>> commandsFromInputArgs = args -> {
if (args.getSourceArgs().length == 0) {
return Collections.emptyList();
if (StringUtils.hasText(primaryCommand)) {
Collections.singletonList(primaryCommand);
}
else {
return Collections.emptyList();
}
}
// re-quote if needed having whitespace
String raw = Arrays.stream(args.getSourceArgs())
@@ -76,7 +83,12 @@ public class NonInteractiveShellRunner implements ShellRunner {
return a;
})
.collect(Collectors.joining(" "));
return Collections.singletonList(raw);
if (StringUtils.hasText(primaryCommand)) {
return Collections.singletonList(primaryCommand + " " + raw);
}
else {
return Collections.singletonList(raw);
}
};
private static boolean isQuoted(String str) {
@@ -88,8 +100,13 @@ public class NonInteractiveShellRunner implements ShellRunner {
}
public NonInteractiveShellRunner(Shell shell, ShellContext shellContext) {
this(shell, shellContext, null);
}
public NonInteractiveShellRunner(Shell shell, ShellContext shellContext, String primaryCommand) {
this.shell = shell;
this.shellContext = shellContext;
this.primaryCommand = primaryCommand;
this.lineParser = new DefaultParser();
}