Skip to content
Open
Changes from 2 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
65 changes: 61 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,8 @@

package dev.starfix;
import io.quarkus.runtime.annotations.RegisterForReflection;
import jdk.jfr.StackTrace;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why is this needed?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Removed


import org.zeroturnaround.exec.ProcessExecutor;
import org.zeroturnaround.exec.ProcessResult;
import picocli.CommandLine;
Expand All @@ -32,10 +34,14 @@
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")
public class Starfix implements Runnable{

private static String OS = System.getProperty("os.name").toLowerCase();

@Parameters(arity = "0..1")
String uri;
Expand All @@ -54,12 +60,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);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

think better to just pass in the exception then you can also pass message and other context in.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yes 👍
have changed it now.

throw illegalArgumentException;
}


Expand Down Expand Up @@ -100,7 +109,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
Copy Markdown
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 +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
Expand Down Expand Up @@ -341,6 +360,44 @@ 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");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this would cause conflicts if concurrent runs.
shuold also put it in temporary directory.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Have made it a temp file now and delete it on exit .

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>"+message+"</h2>");
bw.write("<p>");

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

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