From 5df0c75caecc06a7aeac0ab988e7398c8919c617 Mon Sep 17 00:00:00 2001
From: Fahad Israr
Date: Sun, 15 Aug 2021 16:05:48 +0530
Subject: [PATCH 1/3] generate webpage in case of exceptions
---
cli/src/main/java/dev/starfix/Starfix.java | 35 ++++++++++++++++++++--
1 file changed, 33 insertions(+), 2 deletions(-)
diff --git a/cli/src/main/java/dev/starfix/Starfix.java b/cli/src/main/java/dev/starfix/Starfix.java
index a72a0ea..8e33be1 100644
--- a/cli/src/main/java/dev/starfix/Starfix.java
+++ b/cli/src/main/java/dev/starfix/Starfix.java
@@ -8,6 +8,8 @@
package dev.starfix;
import io.quarkus.runtime.annotations.RegisterForReflection;
+import jdk.jfr.StackTrace;
+
import org.zeroturnaround.exec.ProcessExecutor;
import org.zeroturnaround.exec.ProcessResult;
import picocli.CommandLine;
@@ -32,6 +34,8 @@
import java.util.regex.Pattern;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
+import java.awt.Desktop;
+import java.io.*;
@CommandLine.Command(name = "starfix", mixinStandardHelpOptions = true, defaultValueProvider = YAMLDefaultProvider.class)
@RegisterForReflection(classNames = "java.util.Properties")
@@ -54,12 +58,15 @@ public int config() throws Exception {
}
@Command(name = "clone")
- public int cloneCmd(@Parameters(index = "0") String url) {
+ public int cloneCmd(@Parameters(index = "0") String url)throws Exception {
CloneUrl cloneUrl = new CloneUrl(url);
// URL Validation to check a valid git repository
if (!validate_url(cloneUrl.url)) { // Incase URI doesn't macth our scheme we'll terminate
System.out.println(url);
- throw new IllegalArgumentException("Not a valid URI for git repository");
+ String message = "Not a valid URI for git repository: "+cloneUrl.url;
+ Exception illegalArgumentException = new IllegalArgumentException(message);
+ generateHTML(illegalArgumentException.getStackTrace(),message);
+ throw illegalArgumentException;
}
@@ -100,7 +107,11 @@ public void run() {
new CommandLine(new Starfix()).usage(System.out); // Will invoke Picocli Help
return;
}
+ try{
cloneCmd(uri);
+ }catch(Exception e){
+ e.printStackTrace();
+ }
}
// Function to validate URL using with Regex
@@ -341,6 +352,26 @@ public static void processCloneURL(CloneUrl cloneUrl) throws URISyntaxException,
}
+ public static void generateHTML(StackTraceElement[] stacktrace, String message)throws IOException{
+ String userHome = System.getProperty("user.home");
+ File f = new File(userHome+"/starfix-exception.html");
+ BufferedWriter bw = new BufferedWriter(new FileWriter(f));
+ bw.write("Starfix
");
+ bw.write("Launch Starfix Config Editor");
+ bw.write(""+message+"
");
+ bw.write("");
+
+ for(StackTraceElement line:stacktrace){
+ bw.write(line.toString()+"
");
+ }
+
+ bw.write("
");
+ bw.write("");
+ bw.close();
+
+ Desktop.getDesktop().browse(f.toURI());
+ }
+
public static abstract class IDE{
public abstract void launch_editor(Path directory, String ide, String path, String filePath)throws IOException, InterruptedException;
From 4269882033228cce862a0c1da12583070a4ad2f6 Mon Sep 17 00:00:00 2001
From: Fahad Israr
Date: Thu, 26 Aug 2021 20:40:59 +0530
Subject: [PATCH 2/3] added OS specific commands to launch web page in case of
exception
---
cli/src/main/java/dev/starfix/Starfix.java | 36 +++++++++++++++++++---
1 file changed, 31 insertions(+), 5 deletions(-)
diff --git a/cli/src/main/java/dev/starfix/Starfix.java b/cli/src/main/java/dev/starfix/Starfix.java
index 8e33be1..22568bc 100644
--- a/cli/src/main/java/dev/starfix/Starfix.java
+++ b/cli/src/main/java/dev/starfix/Starfix.java
@@ -40,6 +40,8 @@
@CommandLine.Command(name = "starfix", mixinStandardHelpOptions = true, defaultValueProvider = YAMLDefaultProvider.class)
@RegisterForReflection(classNames = "java.util.Properties")
public class Starfix implements Runnable{
+
+ private static String OS = System.getProperty("os.name").toLowerCase();
@Parameters(arity = "0..1")
String uri;
@@ -127,11 +129,17 @@ public static boolean isBlob(String url){
return Pattern.matches(pattern,url);
}
- // Function yo determine if the current OS is Windows
+ // Function to determine if the current OS is Windows
public static boolean isWindows() {
- return System.getProperty("os.name").toLowerCase().contains("windows");
+ return OS.contains("windows");
+ }
+
+ public static boolean isMac() {
+ return OS.contains("mac");
}
+
+
// Function to fetch config file
public static File getConfigFile() {
String userHome = System.getProperty("user.home"); // Get User Home Directory: /home/user_name
@@ -352,9 +360,9 @@ public static void processCloneURL(CloneUrl cloneUrl) throws URISyntaxException,
}
- public static void generateHTML(StackTraceElement[] stacktrace, String message)throws IOException{
+ public static void generateHTML(StackTraceElement[] stacktrace, String message)throws IOException, InterruptedException{
String userHome = System.getProperty("user.home");
- File f = new File(userHome+"/starfix-exception.html");
+ File f = new File(userHome+"/starfixException.html");
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write("Starfix
");
bw.write("Launch Starfix Config Editor");
@@ -368,8 +376,26 @@ public static void generateHTML(StackTraceElement[] stacktrace, String message)t
bw.write("
");
bw.write("");
bw.close();
+
+ openWebPageInBrowser(f.toURI());
+
+
+ }
- Desktop.getDesktop().browse(f.toURI());
+ public static void openWebPageInBrowser(URI uri) throws IOException, InterruptedException{
+
+ runCommand(Paths.get(""),getBrowserLaunchCommandBasedOnOS(),uri.toString());
+
+ }
+
+ public static String getBrowserLaunchCommandBasedOnOS(){
+ if(isWindows()){
+ return "start"; // If Windows
+ } else if(isMac()){
+ return "open"; //If macosx
+ } else {
+ return "xdg-open"; // If Linux
+ }
}
From 82691d509b4ceae2c1980b1440af1a44286cc534 Mon Sep 17 00:00:00 2001
From: Fahad Israr
Date: Tue, 21 Sep 2021 11:03:47 +0530
Subject: [PATCH 3/3] Create Temp file for Error HTML and update function call
params
---
cli/src/main/java/dev/starfix/Starfix.java | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)
diff --git a/cli/src/main/java/dev/starfix/Starfix.java b/cli/src/main/java/dev/starfix/Starfix.java
index 22568bc..1056ffd 100644
--- a/cli/src/main/java/dev/starfix/Starfix.java
+++ b/cli/src/main/java/dev/starfix/Starfix.java
@@ -8,7 +8,6 @@
package dev.starfix;
import io.quarkus.runtime.annotations.RegisterForReflection;
-import jdk.jfr.StackTrace;
import org.zeroturnaround.exec.ProcessExecutor;
import org.zeroturnaround.exec.ProcessResult;
@@ -34,8 +33,8 @@
import java.util.regex.Pattern;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
-import java.awt.Desktop;
import java.io.*;
+import java.sql.Timestamp;
@CommandLine.Command(name = "starfix", mixinStandardHelpOptions = true, defaultValueProvider = YAMLDefaultProvider.class)
@RegisterForReflection(classNames = "java.util.Properties")
@@ -67,7 +66,7 @@ public int cloneCmd(@Parameters(index = "0") String url)throws Exception {
System.out.println(url);
String message = "Not a valid URI for git repository: "+cloneUrl.url;
Exception illegalArgumentException = new IllegalArgumentException(message);
- generateHTML(illegalArgumentException.getStackTrace(),message);
+ generateHTML(illegalArgumentException);
throw illegalArgumentException;
}
@@ -360,19 +359,23 @@ public static void processCloneURL(CloneUrl cloneUrl) throws URISyntaxException,
}
- public static void generateHTML(StackTraceElement[] stacktrace, String message)throws IOException, InterruptedException{
- String userHome = System.getProperty("user.home");
- File f = new File(userHome+"/starfixException.html");
+ public static void generateHTML(Exception e)throws IOException, InterruptedException{
+ // String userHome = System.getProperty("user.home");
+ File f = File.createTempFile("starfixException",".html"); //Create Temp File with Prefix:"starfixException" and Suffix: ".html"
+ f.deleteOnExit(); // Delete Temp File on Exiting Normally
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write("Starfix
");
bw.write("Launch Starfix Config Editor");
- bw.write(""+message+"
");
+ bw.write(""+e.getMessage()+"
");
bw.write("");
- for(StackTraceElement line:stacktrace){
+ for(StackTraceElement line:e.getStackTrace()){
bw.write(line.toString()+"
");
}
+ bw.write("
");
+ bw.write("Timestamp: "+ new Timestamp(System.currentTimeMillis())+"");
+
bw.write("
");
bw.write("");
bw.close();