Skip to content

Commit

Permalink
Exports the scan report as json (#41)
Browse files Browse the repository at this point in the history
* Saves report as json

* Version bumps

* Readme update

* Pom corrections
  • Loading branch information
prathamgahlout authored Jul 12, 2024
1 parent a32c7b7 commit 2c9742e
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 23 deletions.
2 changes: 1 addition & 1 deletion LicenseDetector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>common-utils</artifactId>
<version>${project.version}</version>
<version>1.1</version>
</dependency>
</dependencies>
</project>
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The tool scans the metadata file (e.g. pom.xml for maven based projects and pack
- [ ] License Detection through README files (If no License file is found in the package)
- [X] Supports scanning remote public repo
- [X] Supports scanning packages (zip/jar/tgz)
- [ ] Export report as XML/JSON
- [X] Export report as JSON

### Supported package/project managers

Expand Down
2 changes: 1 addition & 1 deletion common-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.phsyberdome</groupId>
<artifactId>common-utils</artifactId>
<version>1.0</version>
<version>1.1</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ public static void printLine(String data, Color color) {
System.out.println(ansi().fg(color).a(data).reset());
}

public static void printLine(String data) {
System.out.println(data);
}

public static void print(String data, Color color) {
System.out.print(ansi().fg(color).a(data).reset());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,5 +336,15 @@ public static void cleanup(){
File file = FileSystems.getDefault().getPath(loc).toFile();
deleteDirectory(file);
}

public static boolean writeToFile(String fileName, String contents, String path) {
try {
File file = FileSystems.getDefault().getPath(path).resolve(fileName).toFile();
FileUtils.writeStringToFile(file, contents, "UTF-8", true);
return true;
} catch(IOException | InvalidPathException e) {
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
*/
public enum CLI_FLAGS {
src,
dest
dest,
v,
verbose
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,57 @@ private TreeNode createFromModuleObj(Module module) {

public void prettyPrintTree() {
CLIHelper.updateCurrentLine("", Ansi.Color.YELLOW);
printNode(rootNode, "");
CLIHelper.printLine(createTreeWithAnsiChars(rootNode, ""), Ansi.Color.YELLOW);
}

private void printNode(TreeNode node, String prefix) {
public String createTreeWithAnsiChars(TreeNode node, String prefix) {
if(node == null) {
return;
return "";
}
String nodeName = node.getModule().getName() + "@" + node.getModule().getVersion();
String result = "";
String nodeName = ansi().fg(Ansi.Color.YELLOW).a(node.getModule().getName() + "@" + node.getModule().getVersion()).reset().toString();
if(node.getModule().getLicense() != null && node.getModule().getLicense().equals("null")==false) {
nodeName += ansi().fg(Ansi.Color.GREEN).a("[" + node.getModule().getLicense() + "]");
}

CLIHelper.printLine(nodeName, Ansi.Color.YELLOW);
result += nodeName + "\n";
int NO_OF_CHILDREN = node.getChildren().size();
for(int i=0;i<NO_OF_CHILDREN;i++) {
TreeNode m = node.getChildren().get(i);
CLIHelper.print(prefix, Ansi.Color.RED);
result += ansi().fg(Ansi.Color.RED).a(prefix).reset();
if(i==NO_OF_CHILDREN-1){
CLIHelper.print(CLIHelper.CORNER, Ansi.Color.RED);
printNode(m, prefix + CLIHelper.SPACE_2);
result += ansi().fg(Ansi.Color.RED).a(CLIHelper.CORNER).reset();
result += createTreeWithAnsiChars(m,prefix + CLIHelper.SPACE_2);
}else{
CLIHelper.print(CLIHelper.CROSS, Ansi.Color.RED);
printNode(m, prefix + CLIHelper.VERTICAL);
result += ansi().fg(Ansi.Color.RED).a(CLIHelper.CROSS).reset();
result += createTreeWithAnsiChars(m,prefix + CLIHelper.VERTICAL);
}
}
return result;
}


public String createTree(TreeNode node, String prefix) {
if(node == null) {
return "";
}
String result = "";
String nodeName = node.getModule().getName() + "@" + node.getModule().getVersion();
if(node.getModule().getLicense() != null && node.getModule().getLicense().equals("null")==false) {
nodeName += "[" + node.getModule().getLicense() + "]";
}
result += nodeName + "\n";
int NO_OF_CHILDREN = node.getChildren().size();
for(int i=0;i<NO_OF_CHILDREN;i++) {
TreeNode m = node.getChildren().get(i);
result += prefix;
if(i==NO_OF_CHILDREN-1){
result += CLIHelper.CORNER;
result += createTree(m,prefix + CLIHelper.SPACE_2);
}else{
result += CLIHelper.CROSS;
result += createTree(m,prefix + CLIHelper.VERTICAL);
}
}
return result;
}

}
2 changes: 1 addition & 1 deletion dependency-scanner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.phsyberdome</groupId>
<artifactId>dependency-scanner</artifactId>
<version>1.0.6-beta</version>
<version>1.0.7-beta</version>
<packaging>pom</packaging>
<modules>
<module>../common-utils</module>
Expand Down
4 changes: 2 additions & 2 deletions phsyberdome-sca-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.phsyberdome</groupId>
<artifactId>phsyberdome-sca-cli</artifactId>
<version>1.0.6-beta</version>
<version>1.0.7-beta</version>
<packaging>jar</packaging>
<build>
<plugins>
Expand Down Expand Up @@ -69,7 +69,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>common-utils</artifactId>
<version>1.0</version>
<version>1.1</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import com.phsyberdome.common.utils.NetworkHelper;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import org.fusesource.jansi.Ansi.Color;

/**
Expand Down Expand Up @@ -54,7 +57,7 @@ private static void scan(String targetPath, String destPath){
DependencyExcavator excavator = new DependencyExcavator();

DependencyScanResult result = excavator.excavate();

saveResultAsJson(result);
printResult(result);

/**
Expand Down Expand Up @@ -91,6 +94,21 @@ private static void printResult(DependencyScanResult result) {
}
}

private static void saveResultAsJson(DependencyScanResult result) {
final Calendar calendar = Calendar.getInstance();
String timestamp = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH))+String.valueOf(calendar.get(Calendar.MONTH))+String.valueOf(calendar.get(Calendar.YEAR));

String fileName = "scan_report_"+UUID.randomUUID()+"_"+timestamp+".json";
for(DependencyManager manager: result.getResult()){
String json = JSONHelper.convertToJson(manager);
if(FileUtil.writeToFile(fileName, json, Configuration.getConfiguration().getBasePath().toString()) == false) {
CLIHelper.printLine("Failed to export file!");
return;
}
}
CLIHelper.printLine("Successfully exported data to "+Configuration.getConfiguration().getBasePath().resolve(fileName).toAbsolutePath());
}


private static void printOrganizationalData() {
if(
Expand Down
2 changes: 1 addition & 1 deletion plugin.cargo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>common-utils</artifactId>
<version>${project.version}</version>
<version>1.1</version>
</dependency>
<dependency>
<groupId>com.moandjiezana.toml</groupId>
Expand Down
2 changes: 1 addition & 1 deletion plugin.maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>common-utils</artifactId>
<version>${project.version}</version>
<version>1.1</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion plugin.npm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>common-utils</artifactId>
<version>${project.version}</version>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down

0 comments on commit 2c9742e

Please sign in to comment.