Skip to content

Commit

Permalink
chore: Cosmetic changes in Citrus JBang
Browse files Browse the repository at this point in the history
  • Loading branch information
christophd committed Nov 17, 2023
1 parent d8f223e commit d9adfff
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public abstract class CitrusCommand implements Callable<Integer> {
CommandSpec spec;

private final CitrusJBangMain main;
private File citrusDir;
private File userDir;

//CHECKSTYLE:OFF
@CommandLine.Option(names = { "-h", "--help" }, usageHelp = true, description = "Display the help and sub-commands")
Expand All @@ -49,17 +49,19 @@ public CitrusJBangMain getMain() {
}

public File getStatusFile(String pid) {
if (citrusDir == null) {
citrusDir = new File(System.getProperty("user.home"), ".citrus");
}
return new File(citrusDir, pid + "-status.json");
return new File(getUserDir(), pid + "-status.json");
}

public File getOutputFile(String pid) {
if (citrusDir == null) {
citrusDir = new File(System.getProperty("user.home"), ".citrus");
return new File(getUserDir(), pid + "-output.json");
}

protected File getUserDir() {
if (userDir == null) {
userDir = new File(System.getProperty("user.home"), ".citrus");
}
return new File(citrusDir, pid + "-output.json");

return userDir;
}

protected abstract static class ParameterConsumer<T> implements IParameterConsumer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,16 @@
*/
package org.citrusframework.jbang.commands;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.citrusframework.util.FileUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.freva.asciitable.AsciiTable;
import com.github.freva.asciitable.Column;
import com.github.freva.asciitable.HorizontalAlign;
import com.github.freva.asciitable.OverflowBehaviour;
import main.CitrusJBang;
import org.citrusframework.jbang.JsonSupport;
import org.citrusframework.jbang.CitrusJBangMain;
import picocli.CommandLine;
import picocli.CommandLine.Command;
Expand All @@ -58,16 +52,15 @@ public Integer call() throws Exception {
final long cur = ProcessHandle.current().pid();
ProcessHandle.allProcesses()
.filter(ph -> ph.pid() != cur)
.filter(ph -> isTestProcess(ph.info()))
.forEach(ph -> {
if (ph.info().commandLine().orElse("").contains(CitrusJBang.class.getSimpleName())) {
Row row = new Row();
row.pid = "" + ph.pid();
row.uptime = extractSince(ph);
row.ago = printSince(row.uptime);
row.name = extractName(ph);
row.status = ph.isAlive() ? "Running" : "Finished";
rows.add(row);
}
Row row = new Row();
row.pid = "" + ph.pid();
row.uptime = extractSince(ph);
row.ago = printSince(row.uptime);
row.name = extractName(ph);
row.status = ph.isAlive() ? "Running" : "Finished";
rows.add(row);
});

// sort rows
Expand All @@ -88,16 +81,24 @@ public Integer call() throws Exception {
return 0;
}

protected boolean isTestProcess(ProcessHandle.Info info) {
return info.commandLine().orElse("").contains(CitrusJBang.class.getSimpleName());
}

private String extractName(ProcessHandle ph) {
String cl = ph.info().commandLine().orElse("");
String citrusJBangRun = String.format("main.%s run ", CitrusJBang.class.getSimpleName());
String citrusJBangRun = getJBangRunCommand();
if (cl.contains(citrusJBangRun)) {
return cl.substring(cl.indexOf(citrusJBangRun) + citrusJBangRun.length());
}

return "";
}

protected String getJBangRunCommand() {
return String.format("main.%s run ", CitrusJBang.class.getSimpleName());
}

private String printSince(long timestamp) {
long age = System.currentTimeMillis() - timestamp;
Duration duration = Duration.ofMillis(age);
Expand Down Expand Up @@ -138,30 +139,15 @@ protected int sortRow(Row o1, Row o2) {
}
}

private JsonNode loadStatus(long pid) {
try {
File f = getStatusFile("" + pid);
if (f != null && f.exists()) {
try (FileInputStream fis = new FileInputStream(f)) {
String text = FileUtils.readToString(fis);
return JsonSupport.json().reader().readTree(text);
}
}
} catch (IOException e) {
// ignore
}
return null;
}

static long extractSince(ProcessHandle ph) {
protected static long extractSince(ProcessHandle ph) {
long since = 0;
if (ph.info().startInstant().isPresent()) {
since = ph.info().startInstant().get().toEpochMilli();
}
return since;
}

private static class Row {
protected static class Row {
String pid;
String name;
String status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private int run() throws Exception {
return exitStatus.exitStatus();
}

private TestRunConfiguration getRunConfiguration(List<String> files) {
protected TestRunConfiguration getRunConfiguration(List<String> files) {
TestRunConfiguration configuration = new TestRunConfiguration();

String ext = FileUtils.getFileExtension(files.get(0));
Expand Down

0 comments on commit d9adfff

Please sign in to comment.