Skip to content

Commit cb6ed76

Browse files
committed
feat: CLI implementation
chore: split fx code from non-fx code on the logging layer
1 parent ba88235 commit cb6ed76

7 files changed

Lines changed: 147 additions & 13 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
</manifest>
146146
</archive>
147147

148-
<finalName>${artifactId}</finalName>
148+
<finalName>${project.artifactId}</finalName>
149149
<appendAssemblyId>false</appendAssemblyId>
150150
</configuration>
151151

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.github.exadmin.cyberferret;
2+
3+
import com.github.exadmin.cyberferret.async.RunnableScanner;
4+
import com.github.exadmin.cyberferret.async.RunnableSigsLoader;
5+
import com.github.exadmin.cyberferret.model.FoundItemsContainer;
6+
import com.github.exadmin.cyberferret.model.FoundPathItem;
7+
import com.github.exadmin.cyberferret.model.ItemType;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.util.List;
14+
import java.util.function.Predicate;
15+
import java.util.stream.Stream;
16+
17+
public class CyberFerretCLI {
18+
private static final Logger log = LoggerFactory.getLogger(CyberFerretCLI.class);
19+
private static final List<String> FORKED_REPOSITORIES = List.of(
20+
"k8s-conformance",
21+
"pg_hint_plan",
22+
"qubership-mistral",
23+
"qubership-integration-micro-engine",
24+
"release-drafter",
25+
"gatekeeper-library",
26+
"minio",
27+
"mistral-upstream",
28+
"robot-shop",
29+
"keycloak",
30+
"cassandra",
31+
"postgres",
32+
"kafka",
33+
"chart-releaser-action",
34+
"cassandra-exporter"
35+
);
36+
37+
public static void main(String[] args) throws Exception {
38+
log.info("Start scanning directory {} using dictionary from {}", args[0], args[1]);
39+
40+
final Path rootPathToScan = Path.of(args[0]);
41+
final Path signaturesPath = Path.of(args[1]);
42+
43+
RunnableSigsLoader sigsLoader = new RunnableSigsLoader();
44+
sigsLoader.setFileToLoad(signaturesPath);
45+
sigsLoader.run();
46+
47+
RunnableScanner runnableScanner = new RunnableScanner();
48+
runnableScanner.setAllowedSigMap(sigsLoader.getAllowedSignaturesMap());
49+
runnableScanner.setSignaturesMap(sigsLoader.getRegExpMap());
50+
runnableScanner.setExcludeExtMap(sigsLoader.getExcludeExtsMap());
51+
52+
// list directories to assume they are cloned repositories
53+
List<Path> subDirs = null;
54+
try (Stream<Path> paths = Files.list(rootPathToScan)) {
55+
subDirs = paths.filter(Files::isDirectory).toList();
56+
}
57+
58+
final int prefixPathLength = rootPathToScan.toString().length();
59+
60+
for (Path subDir : subDirs) {
61+
log.warn("\n");
62+
log.warn("***** ***** Start scanning");
63+
log.warn("***** ***** {}", subDir);
64+
log.warn("***** *****");
65+
66+
// check if next directory is a fork - to skip it
67+
String subDirName = subDir.getFileName().toString().toLowerCase();
68+
if (FORKED_REPOSITORIES.contains(subDirName)) {
69+
log.info("Skipping forked repository {}", subDirName);
70+
continue;
71+
}
72+
73+
FoundItemsContainer itemsContainer = new FoundItemsContainer();
74+
75+
runnableScanner.setFoundItemsContainer(itemsContainer);
76+
runnableScanner.setDirToScan(subDir.toString());
77+
runnableScanner.run();
78+
79+
List<FoundPathItem> items = itemsContainer.getFoundItemsCopy();
80+
81+
// filter out ignored items
82+
Predicate<FoundPathItem> keepActual = element -> !(element.isIgnored() || element.isAllowedValue());
83+
Predicate<FoundPathItem> keepSignatures = element -> element.getType().equals(ItemType.SIGNATURE);
84+
items = items.stream()
85+
.filter(keepSignatures)
86+
.filter(keepActual)
87+
.toList();
88+
89+
for (FoundPathItem item : items) {
90+
log.warn("Found {} \"{}\" in \"{}\"", item.getVisualName(), item.getFoundString(), item.getFilePath().toString().substring(prefixPathLength));
91+
}
92+
}
93+
}
94+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.github.exadmin.cyberferret.async;
2+
3+
public interface FxCallback {
4+
void showMessage(FxCallbackType type, String message);
5+
6+
enum FxCallbackType {
7+
ERROR, WARNING, INFO;
8+
}
9+
}

src/main/java/com/github/exadmin/cyberferret/async/RunnableScanner.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public class RunnableScanner extends ARunnable {
3535
private Map<String, Pattern> sigMap = null;
3636
private Map<String, String> allowedSigMap = null;
3737
private Map<String, List<String>> excludeExtMap = null;
38+
private FxCallback fxCallback = (type, message) -> {
39+
log.info(message);
40+
};
3841

3942
public RunnableScanner() {
4043
}
@@ -59,6 +62,10 @@ public void setFoundItemsContainer(FoundItemsContainer foundItemsContainer) {
5962
this.foundItemsContainer = foundItemsContainer;
6063
}
6164

65+
public void setFxCallback(FxCallback fxCallback) {
66+
this.fxCallback = fxCallback;
67+
}
68+
6269
@Override
6370
protected Logger getLog() {
6471
return log;
@@ -71,25 +78,28 @@ protected void _run() throws IOException {
7178
Path gitConfigPath = Paths.get(dirToScan, ".git", "config");
7279
File gitConfigFile = gitConfigPath.toFile();
7380
if (!gitConfigFile.exists() || !gitConfigFile.isFile()) {
74-
AlertBuilder.showWarn("""
81+
fxCallback.showMessage(FxCallback.FxCallbackType.WARNING, """
7582
You've selected not a root of a git-repository.
7683
You can continue using scanner but exclusion file may be created/written not in the canonical place.
7784
Existed exclusion configurations will not be shown""");
7885
}
7986

8087
if (sigMap == null || sigMap.isEmpty()) {
81-
AlertBuilder.showError("Load signatures first. Nothing to scan by.");
88+
fxCallback.showMessage(FxCallback.FxCallbackType.ERROR, "Load signatures first. Nothing to scan by.");
8289
return;
8390
}
8491

8592
// try loading exclusions-model from the file in the root of the repository
8693
ExcludeFileModel tmpExcludeFileModel = new ExcludeFileModel(); // create empty container
87-
Path exFile = Paths.get(dirToScan, Excluder.PERSISTENCE_FOLDER, Excluder.EXCLUDES_SHORT_FILE_NAME);
94+
Path exFilePath = Paths.get(dirToScan, Excluder.PERSISTENCE_FOLDER, Excluder.EXCLUDES_SHORT_FILE_NAME);
8895
try {
89-
OBJECT_MAPPER.enable(JsonParser.Feature.INCLUDE_SOURCE_IN_LOCATION);
90-
tmpExcludeFileModel = OBJECT_MAPPER.readValue(exFile.toFile(), ExcludeFileModel.class); // load new exclusion context
96+
File exFile = exFilePath.toFile();
97+
if (exFile.exists() && exFile.isFile()) {
98+
OBJECT_MAPPER.enable(JsonParser.Feature.INCLUDE_SOURCE_IN_LOCATION);
99+
tmpExcludeFileModel = OBJECT_MAPPER.readValue(exFile, ExcludeFileModel.class); // load new exclusion context
100+
}
91101
} catch (Exception ex) {
92-
log.error("Error while loading exclusions configuration from '{}' file", exFile, ex);
102+
log.error("Error while loading exclusions configuration from '{}' file", exFilePath, ex);
93103
}
94104

95105
final ExcludeFileModel excludeFileModel = tmpExcludeFileModel;
@@ -150,7 +160,8 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
150160
List<FoundPathItem> list = foundItemsContainer.getFoundItemsCopy();
151161
final AtomicInteger numberOfThreadsInProgress = new AtomicInteger(0);
152162

153-
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
163+
// try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
164+
try (var executor = Executors.newSingleThreadExecutor()) {
154165
list.forEach(pathItem -> {
155166
executor.submit(() -> {
156167
numberOfThreadsInProgress.incrementAndGet();
@@ -215,6 +226,9 @@ private void scan(FoundPathItem pathItem, Path rootDir, ExcludeFileModel exclude
215226
Path filePath = pathItem.getFilePath();
216227
String fileBody;
217228
try {
229+
System.out.println("Reading file " + filePath);
230+
231+
log.info("Reading file {}", filePath);
218232
fileBody = FileUtils.readFile(filePath);
219233
} catch (IOException ex) {
220234
log.error("Error while reading file '{}'. Skipping it.", filePath, ex);

src/main/java/com/github/exadmin/cyberferret/fxui/SceneBuilder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ protected TitledPane createOfflineDictionaryPane() {
162162

163163
runnableSigsLoader.setBeforeStart(() -> btnLoadSigs.setDisable(true));
164164
runnableSigsLoader.setAfterFinished(() -> {
165+
runnableScanner.setFxCallback((type, message) -> {
166+
switch (type) {
167+
case ERROR -> AlertBuilder.showError(message);
168+
case WARNING -> AlertBuilder.showError(message);
169+
case INFO -> AlertBuilder.showInfo(message);
170+
default -> throw new IllegalStateException("Unsupported message type " + type);
171+
}
172+
});
173+
165174
runnableScanner.setSignaturesMap(runnableSigsLoader.getRegExpMap());
166175
runnableScanner.setAllowedSigMap(runnableSigsLoader.getAllowedSignaturesMap());
167176
runnableScanner.setExcludeExtMap(runnableSigsLoader.getExcludeExtsMap());

src/main/java/com/github/exadmin/cyberferret/model/FoundItemsContainer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class FoundItemsContainer {
1010

1111
public void addItem(FoundPathItem newItem) {
1212
foundPathItems.add(newItem);
13-
onAddNewItemListener.newItemAdded(newItem);
13+
if (onAddNewItemListener != null) onAddNewItemListener.newItemAdded(newItem);
1414
}
1515

1616
// do not public this api
@@ -33,6 +33,6 @@ public void setOnAddNewItemListener(FoundFileItemListener onAddNewItemListener)
3333

3434
public void clearAll() {
3535
getFoundItems().clear();
36-
onAddNewItemListener.onClearAll();
36+
if (onAddNewItemListener != null) onAddNewItemListener.onClearAll();
3737
}
3838
}

src/main/resources/log4j2.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Configuration status="WARN" packages="com.github.exadmin.cyberferret.fxui.logger">
33
<Appenders>
4-
<Console name="Console" target="SYSTEM_OUT">
4+
<!--<Console name="Console" target="SYSTEM_OUT">
55
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] [%tid] %-5level %logger{36} - %msg%n"/>
66
</Console>
77
8-
<File name="File" fileName="./logs.log" append="true">
8+
<File name="File" fileName="./logs2.log" append="true">
99
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] [%tid] %-5level %logger{36} - %msg%n"/>
10+
</File>-->
11+
12+
<Console name="Console" target="SYSTEM_OUT">
13+
<PatternLayout pattern="%d{HH:mm:ss} %-5level: %msg%n"/>
14+
</Console>
15+
16+
<File name="File" fileName="./logs.log" append="true">
17+
<PatternLayout pattern="%d{HH:mm:ss} %-5level: %msg%n"/>
1018
</File>
1119

1220
<FXConsoleAppender name="FXConsoleAppender"/>
1321
</Appenders>
1422
<Loggers>
1523
<Logger name="org.apache" level="INFO"/>
1624

17-
<Root level="DEBUG">
25+
<Root level="WARN">
1826
<AppenderRef ref="Console"/>
1927
<AppenderRef ref="File"/>
2028
<AppenderRef ref="FXConsoleAppender" />

0 commit comments

Comments
 (0)