Skip to content

Generate webpage in case of exceptions #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions cli/src/main/java/dev/starfix/Starfix.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package dev.starfix;
import io.quarkus.runtime.annotations.RegisterForReflection;

import org.zeroturnaround.exec.ProcessExecutor;
import org.zeroturnaround.exec.ProcessResult;
import picocli.CommandLine;
Expand All @@ -32,10 +33,14 @@
import java.util.regex.Pattern;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.*;
import java.sql.Timestamp;

@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;
Expand All @@ -54,12 +59,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);
throw illegalArgumentException;
}


Expand Down Expand Up @@ -100,7 +108,11 @@ public void run() {
new CommandLine(new Starfix()).usage(System.out); // Will invoke Picocli Help
return;
}
try{
cloneCmd(uri);
}catch(Exception e){
e.printStackTrace();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never ok to just print out stacktrace in case of error. need to handle it or guide the user.

}
}

// Function to validate URL using with Regex
Expand All @@ -116,11 +128,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
Expand Down Expand Up @@ -341,6 +359,48 @@ public static void processCloneURL(CloneUrl cloneUrl) throws URISyntaxException,

}

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("<html><body><h1>Starfix</h1>");
bw.write("<a href='ide://config'>Launch Starfix Config Editor</a>");
bw.write("<h2>"+e.getMessage()+"</h2>");
bw.write("<p>");

for(StackTraceElement line:e.getStackTrace()){
bw.write(line.toString()+"<br>");
}
bw.write("<br>");

bw.write("<b>Timestamp: "+ new Timestamp(System.currentTimeMillis())+"</b>");

bw.write("</p>");
bw.write("</body></html>");
bw.close();

openWebPageInBrowser(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
}
}


public static abstract class IDE{
public abstract void launch_editor(Path directory, String ide, String path, String filePath)throws IOException, InterruptedException;
Expand Down