Skip to content
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

[cli]] updates for multi-os browser for langstream UI #682

Merged
merged 25 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import io.undertow.server.handlers.resource.ResourceHandler;
import io.undertow.util.Headers;
import io.undertow.util.HttpString;
import java.awt.Desktop;
mark878 marked this conversation as resolved.
Show resolved Hide resolved
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -46,6 +48,7 @@
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SystemUtils;
import org.xnio.OptionMap;
import org.xnio.Xnio;
import picocli.CommandLine;
Expand Down Expand Up @@ -176,19 +179,54 @@ public static Undertow startServer(
actualPort.set(
((InetSocketAddress) server.getListenerInfo().get(0).getAddress()).getPort());

logger.log("Started UI at http://localhost:" + actualPort.get());
openBrowserAtPort("open", actualPort.get());
logger.log("Starting UI at http://localhost:" + actualPort.get());
String os = System.getProperty("os.name").toLowerCase();
logger.log("Operating system identified as: " + os);
if (openBrowserAtPort("http://localhost:", actualPort.get())) {
logger.log("Started UI at http://localhost:" + actualPort.get());
} else {
logger.log(
"Could not Start browser. Either add the proper command to your OS (open for mac or xdg-open for linux) or start a browser manually at http://localhost:"
+ actualPort.get());
}

return server;
}

static void openBrowserAtPort(String openCommand, int port) {
try {
new ProcessBuilder(openCommand, "http://localhost:" + port).start().waitFor();
} catch (InterruptedException interruptedException) {
Thread.currentThread().interrupt();
} catch (IOException ioException) {
static boolean checkAndLaunch(String openCommand, int port) {
File f = new File(openCommand);
boolean existsInFilesystem = f.exists();
if (existsInFilesystem) {
try {
new ProcessBuilder(openCommand, "http://localhost:" + port).start();
Thread.sleep(1000);
return true;
} catch (InterruptedException interruptedException) {
Thread.currentThread().interrupt();
return false;
} catch (IOException ioException) {
return false;
}
} else {
return false;
}
}

static boolean openBrowserAtPort(String URL, int port) {
String openCommand = "";
if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_WINDOWS) {
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(new URI(URL + port));
} catch (Exception e) {
return false;
}
return true;
} else if (SystemUtils.IS_OS_LINUX) {
mark878 marked this conversation as resolved.
Show resolved Hide resolved
openCommand = "/usr/bin/xdg-open";
return checkAndLaunch(openCommand, port);
}
return false;
}

@Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,41 @@
*/
package ai.langstream.cli.commands.applications;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;
import java.util.UUID;
import org.junit.jupiter.api.Test;

class UIAppCmdTest {

static void writeExecutableFile(String fname, String contents) throws IOException {

var path = Files.writeString(Path.of(fname), contents);

Files.setPosixFilePermissions(
path, Set.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_EXECUTE));
}

@Test
void openBrowser() {
void openBrowser() throws IOException {
// write test file
String fname = "/tmp/" + UUID.randomUUID().toString();
writeExecutableFile(fname, "#!/usr/bin/bash\necho hello\n");
// ok
UIAppCmd.openBrowserAtPort("echo", 9029);
String command = fname;
boolean Result = UIAppCmd.checkAndLaunch(command, 80);
assertTrue(Result, command + "\nif found in filesystem should be true");
new File(fname).delete();
// fail
UIAppCmd.openBrowserAtPort("fail", 9029);
command = "_no_such_command_";
Result = UIAppCmd.checkAndLaunch("_no_such_command_", 80);
assertFalse(Result, command + "\nif not found in filesystemi should be false");
}
}
Loading