Skip to content

Commit

Permalink
Add possibility to put script in background : update save post proces…
Browse files Browse the repository at this point in the history
…sor to serialize every objects
  • Loading branch information
fonimus committed Oct 24, 2020
1 parent 7132a5d commit 03c39d9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@

@Slf4j
public class SavePostProcessor
implements PostProcessor<String> {
implements PostProcessor<Object> {

public static final String SAVE = "save";

private static final String REPLACE_REGEX = "(\\x1b\\x5b|\\x9b)[\\x30-\\x3f]*[\\x20-\\x2f]*[\\x40-\\x7e]";

@Override
public String getName() {
return SAVE;
}

@Override
public String process(String result, List<String> parameters) throws PostProcessorException {
public String process(Object result, List<String> parameters) throws PostProcessorException {
if (parameters == null || parameters.isEmpty()) {
throw new PostProcessorException("Cannot save without file path !");
} else {
Expand All @@ -54,8 +56,7 @@ public String process(String result, List<String> parameters) throws PostProcess
}
File file = new File(path);
try {
String toWrite =
result.replaceAll("(\\x1b\\x5b|\\x9b)[\\x30-\\x3f]*[\\x20-\\x2f]*[\\x40-\\x7e]", "") + "\n";
String toWrite = string(result).replaceAll(REPLACE_REGEX, "") + "\n";
Files.write(file.toPath(), toWrite.getBytes(StandardCharsets.UTF_8), CREATE, APPEND);
return "Result saved to file: " + file.getAbsolutePath();
} catch (IOException e) {
Expand All @@ -64,4 +65,16 @@ public String process(String result, List<String> parameters) throws PostProcess
}
}
}

private String string(Object result) {
if (result instanceof String) {
return (String) result;
} else if (result instanceof Throwable) {
return ((Throwable) result).getClass().getName() + ": " + ((Throwable) result).getMessage();
} else if (result != null) {
return result.toString();
} else {
return "";
}
}
}
2 changes: 2 additions & 0 deletions starter/src/test/resources/script.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ wait 500
wait 500
wait 500
wait 500
xxx
yyy

0 comments on commit 03c39d9

Please sign in to comment.