Skip to content

Commit fde3e6a

Browse files
committed
fix(ENGKNOW-2766): Fix threading issues in cli
- Threading issues. - Refactoring
1 parent 10cc88b commit fde3e6a

44 files changed

Lines changed: 209 additions & 191 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

gorscripts/src/main/java/org/gorpipe/gor/cli/GorCLI.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,8 @@
2222

2323
package org.gorpipe.gor.cli;
2424

25-
import org.gorpipe.gor.cli.cache.CacheCommand;
26-
import org.gorpipe.gor.cli.help.HelpCommand;
27-
import org.gorpipe.gor.cli.index.IndexCommand;
28-
import org.gorpipe.gor.cli.info.InfoCommand;
29-
import org.gorpipe.gor.cli.link.LinkCommand;
30-
import org.gorpipe.gor.cli.manager.ManagerCommand;
3125
import org.gorpipe.gor.cli.migrator.FolderMigratorCommand;
3226
import org.gorpipe.gor.cli.query.QueryCommand;
33-
import org.gorpipe.gor.cli.render.RenderCommand;
3427
import org.gorpipe.logging.GorLogbackUtil;
3528
import picocli.CommandLine;
3629

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.gorpipe.gor.cli;
2+
3+
import picocli.CommandLine;
4+
import java.io.PrintStream;
5+
6+
public abstract class BaseSubCommand extends HelpOptions implements CommandSupport, Runnable {
7+
8+
@CommandLine.ParentCommand
9+
protected CommandSupport parentSupport;
10+
11+
@Override
12+
public void run() { CommandLine.usage(this, getStdErr()); }
13+
14+
public String getSecurityContext() {
15+
return parentSupport != null ? parentSupport.getSecurityContext() : "";
16+
}
17+
18+
public String getProjectRoot() {
19+
return parentSupport != null ? parentSupport.getProjectRoot() : "";
20+
}
21+
22+
public PrintStream getStdOut() {
23+
return parentSupport != null ? parentSupport.getStdOut() : System.out;
24+
}
25+
26+
public PrintStream getStdErr() {
27+
return parentSupport != null ? parentSupport.getStdErr() : System.err;
28+
}
29+
30+
public void exit(int status) {
31+
if (parentSupport != null) parentSupport.exit(status);
32+
else System.exit(status);
33+
}
34+
}
35+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.gorpipe.gor.cli;
2+
3+
import java.io.PrintStream;
4+
5+
public interface CommandSupport {
6+
String getSecurityContext();
7+
String getProjectRoot();
8+
PrintStream getStdOut();
9+
PrintStream getStdErr();
10+
void exit(int status);
11+
}

gortools/src/main/java/org/gorpipe/gor/cli/GorExecCLI.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
package org.gorpipe.gor.cli;
2424

25+
import java.io.PrintStream;
2526
import java.nio.file.Path;
2627

2728
import org.gorpipe.gor.cli.cache.CacheCommand;
@@ -44,17 +45,20 @@
4445
subcommands = {HelpCommand.class, ManagerCommand.class, IndexCommand.class,
4546
CacheCommand.class, RenderCommand.class, InfoCommand.class,
4647
LinkCommand.class, GitCommand.class, FilesCommand.class})
47-
public class GorExecCLI extends HelpOptions implements Runnable {
48+
public class GorExecCLI extends HelpOptions implements CommandSupport, Runnable {
49+
4850
public static void main(String[] args) {
4951
GorLogbackUtil.initLog("gor");
5052
CommandLine cmd = new CommandLine(new GorExecCLI());
5153
cmd.parseWithHandlers(
5254
new CommandLine.RunLast(),
5355
CommandLine.defaultExceptionHandler().andExit(-1),
5456
args);
55-
5657
}
5758

59+
private PrintStream stdOut;
60+
private PrintStream stdErr;
61+
5862
@CommandLine.Option(names = {"-v", "--version"},
5963
versionHelp = true,
6064
description = "Print version information and exits.")
@@ -66,6 +70,16 @@ public static void main(String[] args) {
6670
@CommandLine.Option(defaultValue = "", names={"--securityContext"}, description = "Sets the security context for the current gor session.")
6771
private String securityContext;
6872

73+
public GorExecCLI() {
74+
stdOut = System.out;
75+
stdErr = System.err;
76+
}
77+
78+
public GorExecCLI(PrintStream out, PrintStream err) {
79+
this.stdOut = out;
80+
this.stdErr = err;
81+
}
82+
6983
@Override
7084
public void run() {
7185
CommandLine.usage(this, System.err);
@@ -78,4 +92,22 @@ public String getProjectRoot() {
7892
public String getSecurityContext() {
7993
return securityContext;
8094
}
95+
96+
public PrintStream getStdOut() {
97+
return stdOut;
98+
}
99+
100+
public PrintStream getStdErr() {
101+
return stdErr;
102+
}
103+
104+
@Override
105+
public void exit(int status) {
106+
if (System.err.equals(getStdErr())) {
107+
System.exit(status);
108+
} else {
109+
throw new CommandLine.ExecutionException(new CommandLine(this),
110+
"Exit with status: " + status);
111+
}
112+
}
81113
}

gortools/src/main/java/org/gorpipe/gor/cli/HelpOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
public class HelpOptions extends FormattingOptions {
2929

30-
@CommandLine.Option(names = {"-h", "--help"},
30+
@CommandLine.Option(names = {"--help"},
3131
hidden = true,
3232
usageHelp = true,
3333
description = "Print usage help and exit.")

gortools/src/main/java/org/gorpipe/gor/cli/cache/CacheCommand.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
package org.gorpipe.gor.cli.cache;
2424

25+
import org.gorpipe.gor.cli.BaseSubCommand;
2526
import org.gorpipe.gor.cli.FormattingOptions;
2627
import picocli.CommandLine;
2728

@@ -31,10 +32,6 @@
3132
header = "Cache management",
3233
subcommands = {PurgeCommand.class, TouchCommand.class},
3334
description="Purge or touch a cache directory.")
34-
public class CacheCommand extends FormattingOptions implements Runnable{
35+
public class CacheCommand extends BaseSubCommand {
3536

36-
@Override
37-
public void run() {
38-
CommandLine.usage(this, System.err);
39-
}
4037
}

gortools/src/main/java/org/gorpipe/gor/cli/cache/FilterOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222

2323
package org.gorpipe.gor.cli.cache;
2424

25-
import org.gorpipe.gor.cli.HelpOptions;
25+
import org.gorpipe.gor.cli.BaseSubCommand;
2626
import picocli.CommandLine;
2727

2828
import java.io.File;
2929

30-
abstract class FilterOptions extends HelpOptions {
30+
abstract class FilterOptions extends BaseSubCommand {
3131

3232
@CommandLine.Option(names = {"-v", "--verbose"},
3333
description = "Display path of all files touched or purged.")

gortools/src/main/java/org/gorpipe/gor/cli/cache/PurgeCommand.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ public class PurgeCommand extends FilterOptions implements Runnable{
5555
public void run() {
5656

5757
if (!cachePath.exists() || !cachePath.isDirectory()){
58-
System.err.printf("Cache path %1$s does not exist!", cachePath.toString());
59-
System.exit(-1);
58+
getStdErr().printf("Cache path %1$s does not exist!", cachePath.toString());
59+
if (System.err.equals(getStdErr())) {
60+
System.exit(-1);
61+
}
6062
}
6163

6264
analyseCache(cachePath, ageInDays);
@@ -92,7 +94,7 @@ private void listFiles(File parentFile) {
9294
FileTime time = attrs.lastAccessTime();
9395
lastAccessTime = time.toMillis();
9496
} catch (Exception e) {
95-
System.err.println("!!Failed to get last access time for: " + file.toString());
97+
getStdErr().println("!!Failed to get last access time for: " + file.toString());
9698
}
9799

98100
this.fileCounter++;
@@ -104,7 +106,7 @@ private void listFiles(File parentFile) {
104106
result.process(file.toPath());
105107
}
106108
}
107-
System.err.print("Processing files: " + this.fileCounter + "\r");
109+
getStdErr().print("Processing files: " + this.fileCounter + "\r");
108110
} else if (file.isDirectory()) {
109111
listFiles(file);
110112
}
@@ -114,13 +116,15 @@ private void listFiles(File parentFile) {
114116
private void verifyAndDeleteFromCache() {
115117

116118
if (this.result.getFileList().size() == 0) {
117-
System.err.println("No files to remove. Exiting.");
118-
System.exit(0);
119+
getStdErr().println("No files to remove. Exiting.");
120+
if (System.err.equals(getStdErr())) {
121+
System.exit(0);
122+
}
119123
}
120124

121125
if (!noVerification) {
122126
Scanner user_input = new Scanner(System.in);
123-
System.err.print("Remove files from cache? [y/n]:");
127+
getStdErr().print("Remove files from cache? [y/n]:");
124128
while (true) {
125129
String answer = user_input.next().trim().toLowerCase();
126130

@@ -130,10 +134,10 @@ private void verifyAndDeleteFromCache() {
130134
System.exit(0);
131135
}
132136
}
133-
System.err.println();
137+
getStdErr().println();
134138
}
135139

136-
System.err.println("Removing files from cache at " + this.cachePath);
140+
getStdErr().println("Removing files from cache at " + this.cachePath);
137141

138142
List<String> fileNames = this.result.getFileList();
139143
int fileRemovalCounter = 0;
@@ -143,39 +147,38 @@ private void verifyAndDeleteFromCache() {
143147
File fileToDelete = new File(cachePath, filename);
144148

145149
if (verbose) {
146-
System.err.println("Removing: " + filename + extra );
150+
getStdErr().println("Removing: " + filename + extra );
147151
}
148152

149153
if (!dryRun) {
150154
try {
151155
if (!fileToDelete.delete()) {
152-
System.err.println("Failed to delete file: " + filename);
156+
getStdErr().println("Failed to delete file: " + filename);
153157
}
154158
fileRemovalCounter++;
155159
} catch (Exception e) {
156-
System.err.println("An error occurred when deleting file: " + filename);
157-
System.err.println(e.getMessage());
160+
getStdErr().println("An error occurred when deleting file: " + filename);
161+
getStdErr().println(e.getMessage());
158162
}
159163
} else {
160164
fileRemovalCounter++;
161165
}
162166
}
163167

164-
System.err.printf("Removed %1$d from cache%2$s%n", fileRemovalCounter, extra);
165-
168+
getStdErr().printf("Removed %1$d from cache%2$s%n", fileRemovalCounter, extra);
166169
}
167170

168171
private void displayResult() {
169-
System.err.println("Processed files: " + this.fileCounter + " ");
170-
System.err.println("Files to remove: " + result.getFileList().size());
172+
getStdErr().println("Processed files: " + this.fileCounter + " ");
173+
getStdErr().println("Files to remove: " + result.getFileList().size());
171174

172175
if (result.getFileList().size() > 0) {
173-
System.err.println("Summary by extension:");
176+
getStdErr().println("Summary by extension:");
174177
Map<String, Integer> extensionMap = result.getExtensionCountMap();
175178
for (Map.Entry<String, Integer> entry : extensionMap.entrySet()) {
176-
System.err.printf("\t%1$s\t%2$d%n", entry.getKey(), entry.getValue());
179+
getStdErr().printf("\t%1$s\t%2$d%n", entry.getKey(), entry.getValue());
177180
}
178-
System.err.println();
181+
getStdErr().println();
179182
}
180183
}
181184
}

gortools/src/main/java/org/gorpipe/gor/cli/cache/TouchCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void run() {
4545
private void touchCache(File cachePath) {
4646
touchFiles(cachePath);
4747

48-
System.err.printf("Touched %1$d files%2$s%n", fileCounter, extra);
48+
getStdErr().printf("Touched %1$d files%2$s%n", fileCounter, extra);
4949
}
5050

5151
private void touchFiles(File parentFile) {
@@ -63,7 +63,7 @@ private void touchFiles(File parentFile) {
6363
if (type != null && file.setLastModified(timestamp)) {
6464
fileCounter++;
6565
if (verbose) {
66-
System.err.println("Touching file: " + file.toString() + this.extra);
66+
getStdErr().println("Touching file: " + file.toString() + this.extra);
6767
}
6868
}
6969
} else if (file.isDirectory()) {

gortools/src/main/java/org/gorpipe/gor/cli/files/CatCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class CatCommand implements Runnable {
2020
public void run() {
2121
var reader = parent.getFileReader();
2222
try (var stream = reader.readFile(path)) {
23-
stream.forEach(System.out::println);
23+
stream.forEach(parent.getStdOut()::println);
2424
} catch (IOException e) {
2525
throw new CommandLine.ExecutionException(spec.commandLine(), e.getMessage(), e);
2626
}

0 commit comments

Comments
 (0)