Files
PhotonVision/chameleon-server/src/main/java/com/chameleonvision/util/Platform.java

99 lines
2.4 KiB
Java
Raw Normal View History

package com.chameleonvision.util;
2019-10-07 16:36:53 -04:00
import java.io.BufferedReader;
import java.io.IOException;
2019-10-07 16:36:53 -04:00
import java.nio.file.Files;
import java.nio.file.Paths;
2019-10-04 15:55:45 -04:00
public enum Platform {
WINDOWS_64("Windows x64"),
LINUX_64("Linux x64"),
LINUX_RASPBIAN("Linux Raspbian"),
LINUX_ARM64("Linux ARM64"),
2019-10-04 15:55:45 -04:00
MACOS_64("Mac OS x64"),
UNSUPPORTED("Unsupported Platform");
public final String value;
Platform(String value) {
this.value = value;
}
2019-10-07 16:36:53 -04:00
private static final String OS_NAME = System.getProperty("os.name");
private static final String OS_ARCH = System.getProperty("os.arch");
public static final Platform CurrentPlatform = getCurrentPlatform();
2019-10-07 16:36:53 -04:00
2019-10-04 15:55:45 -04:00
public boolean isWindows() {
return this == WINDOWS_64;
}
public boolean isLinux() {
2019-10-07 16:36:53 -04:00
return this == LINUX_64 || this == LINUX_RASPBIAN || this == LINUX_ARM64;
2019-10-04 15:55:45 -04:00
}
public boolean isMac() {
return this == MACOS_64;
}
private static ShellExec shell = new ShellExec(true, false);
public boolean isRoot() {
if (isLinux() || isMac()) {
try {
shell.execute("id", null, true, "-u");
} catch (IOException e) {
e.printStackTrace();
}
2019-10-10 15:07:56 -04:00
while (!shell.isOutputCompleted()) {
// ignored
}
if (shell.getExitCode() == 0) {
var out = shell.getOutput();
out = out.split("\n")[0];
return out.equals("0");
}
} else if (isWindows()) {
return true;
} else {
return true;
}
return false;
}
2019-10-07 16:36:53 -04:00
private static boolean isRaspbian() {
try (BufferedReader reader = Files.newBufferedReader(Paths.get("/etc/os-release"))) {
String value = reader.readLine();
return value.contains("Raspbian");
} catch (IOException ex) {
return false;
}
}
2019-10-04 15:55:45 -04:00
public static Platform getCurrentPlatform() {
2019-10-07 16:36:53 -04:00
if (OS_NAME.contains("Windows")) {
if (OS_ARCH.equals("amd64")) return Platform.WINDOWS_64;
2019-10-04 15:55:45 -04:00
}
2019-10-07 16:36:53 -04:00
if (OS_NAME.contains("Linux")) {
if (OS_ARCH.equals("amd64")) return Platform.LINUX_64;
if (isRaspbian()) return Platform.LINUX_RASPBIAN;
if (OS_ARCH.contains("aarch")) return Platform.LINUX_ARM64;
2019-10-04 15:55:45 -04:00
}
2019-10-07 16:36:53 -04:00
if (OS_NAME.contains("Mac")) {
if (OS_ARCH.equals("amd64")) return Platform.MACOS_64;
2019-10-04 15:55:45 -04:00
}
2019-10-07 16:36:53 -04:00
System.out.printf("Unknown Platform! OS: %s, Architecture: %s", OS_NAME, OS_ARCH);
2019-10-04 15:55:45 -04:00
return Platform.UNSUPPORTED;
}
2019-10-07 16:36:53 -04:00
public String toString() {
if (this.equals(UNSUPPORTED)) {
return String.format("Unknown Platform. OS: %s, Architecture: %s", OS_NAME, OS_ARCH);
} else {
return this.value;
}
}
2019-10-04 15:55:45 -04:00
}