Light, fluffy, and always free - The AWS Local Emulator alternative
feat(core): keep a host native binary's state in the user data directory (#5769)
Hector Ventura0a5809a55931
diff --git a/src/main/java/io/github/hectorvent/floci/config/HostDataDir.java b/src/main/java/io/github/hectorvent/floci/config/HostDataDir.javanew file mode 100644index 0000000000..03a40486a6--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/config/HostDataDir.java@@ -0,0 +1,58 @@+package io.github.hectorvent.floci.config;++import io.github.hectorvent.floci.core.common.docker.ContainerDetector;+import io.quarkus.runtime.ImageMode;++import java.nio.file.Path;+import java.util.Locale;+import java.util.Map;++/**+ * Where a standalone Floci binary keeps its state when nothing says otherwise.+ *+ * <p>The bundled default, {@code ./data}, is right for the image (which sets+ * {@code /app/data}) and for a developer running from the checkout, but a native binary on the+ * user's PATH would write into whatever directory it happens to be started from. A native run+ * outside a container therefore defaults to the platform's per-user data directory instead.+ * The JVM, dev mode and the image keep {@code ./data}.+ */+public final class HostDataDir {++ static final String BUNDLED_DEFAULT = "./data";+ static final String DIR_NAME = "floci-aws";++ private HostDataDir() {+ }++ /** The persistent path to use when none is configured. */+ public static String persistentPathDefault() {+ if (!applies()) {+ return BUNDLED_DEFAULT;+ }+ return forPlatform(System.getProperty("os.name", ""), System.getenv(),+ System.getProperty("user.home", ".")).toString();+ }++ static boolean applies() {+ return ImageMode.current() == ImageMode.NATIVE_RUN && !new ContainerDetector().isRunningInContainer();+ }++ static Path forPlatform(String osName, Map<String, String> env, String userHome) {+ String os = osName.toLowerCase(Locale.ROOT);+ if (os.startsWith("windows")) {+ String localAppData = env.get("LOCALAPPDATA");+ Path base = isSet(localAppData) ? Path.of(localAppData) : Path.of(userHome, "AppData", "Local");+ return base.resolve(DIR_NAME);+ }+ if (os.startsWith("mac")) {+ return Path.of(userHome, "Library", "Application Support", DIR_NAME);+ }+ String xdgDataHome = env.get("XDG_DATA_HOME");+ Path base = isSet(xdgDataHome) ? Path.of(xdgDataHome) : Path.of(userHome, ".local", "share");+ return base.resolve(DIR_NAME);+ }++ private static boolean isSet(String value) {+ return value != null && !value.isBlank();+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/config/HostDefaultsConfigSource.java b/src/main/java/io/github/hectorvent/floci/config/HostDefaultsConfigSource.javanew file mode 100644index 0000000000..36104964ec--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/config/HostDefaultsConfigSource.java@@ -0,0 +1,64 @@+package io.github.hectorvent.floci.config;++import org.eclipse.microprofile.config.spi.ConfigSource;++import java.util.Map;+import java.util.Set;++/**+ * Supplies {@link HostDataDir}'s per-user data directory as the storage path of a native binary+ * running outside a container.+ *+ * <p>The ordinal sits just above the bundled {@code application.yml} (255) and below every source+ * a user sets explicitly: {@code config/application.yml} in the working directory (265),+ * {@code .env} (295), environment variables (300) and system properties (400). So it only+ * replaces the bundled {@code ./data}; any path the user configures still wins. It is empty for+ * the JVM, dev mode and the image.+ */+public class HostDefaultsConfigSource implements ConfigSource {++ static final int ORDINAL = 260;+ static final String PERSISTENT_PATH = "floci.storage.persistent-path";+ static final String HOST_PERSISTENT_PATH = "floci.storage.host-persistent-path";++ private final Map<String, String> properties;++ public HostDefaultsConfigSource() {+ this(HostDataDir.applies());+ }++ HostDefaultsConfigSource(boolean nativeOnHost) {+ if (nativeOnHost) {+ String dir = HostDataDir.persistentPathDefault();+ // The binary and Docker share the host filesystem, so child containers bind the same path.+ this.properties = Map.of(PERSISTENT_PATH, dir, HOST_PERSISTENT_PATH, dir);+ } else {+ this.properties = Map.of();+ }+ }++ @Override+ public Map<String, String> getProperties() {+ return properties;+ }++ @Override+ public Set<String> getPropertyNames() {+ return properties.keySet();+ }++ @Override+ public String getValue(String propertyName) {+ return properties.get(propertyName);+ }++ @Override+ public String getName() {+ return "floci-host-defaults";+ }++ @Override+ public int getOrdinal() {+ return ORDINAL;+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java b/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.javaindex 49eb361684..dfc85f7938 100644--- a/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java+++ b/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java@@ -111,7 +111,7 @@ public class TlsConfigSource implements ConfigSource {String certPath = resolveProperty("floci.tls.cert-path", "");String keyPath = resolveProperty("floci.tls.key-path", "");String selfSigned = resolveProperty("floci.tls.self-signed", "true");- String persistentPath = resolveProperty("floci.storage.persistent-path", "./data");+ String persistentPath = resolveProperty("floci.storage.persistent-path", HostDataDir.persistentPathDefault());Path tlsDir = Path.of(persistentPath, TLS_DIR);resolvedTlsDir = tlsDir;diff --git a/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource b/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSourceindex 4045912cfa..52c0f16b32 100644--- a/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource+++ b/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource@@ -1 +1,2 @@io.github.hectorvent.floci.config.TlsConfigSource+io.github.hectorvent.floci.config.HostDefaultsConfigSourcediff --git a/src/test/java/io/github/hectorvent/floci/config/HostDataDirTest.java b/src/test/java/io/github/hectorvent/floci/config/HostDataDirTest.javanew file mode 100644index 0000000000..417e362e1f--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/config/HostDataDirTest.java@@ -0,0 +1,44 @@+package io.github.hectorvent.floci.config;++import org.junit.jupiter.api.Test;++import java.nio.file.Path;+import java.util.Map;++import static org.hamcrest.MatcherAssert.assertThat;+import static org.hamcrest.Matchers.equalTo;++class HostDataDirTest {++ @Test+ void linuxUsesXdgDataHomeWhenSet() {+ assertThat(HostDataDir.forPlatform("Linux", Map.of("XDG_DATA_HOME", "/srv/xdg"), "/home/u"),+ equalTo(Path.of("/srv/xdg", "floci-aws")));+ }++ @Test+ void linuxFallsBackToLocalShare() {+ assertThat(HostDataDir.forPlatform("Linux", Map.of("XDG_DATA_HOME", " "), "/home/u"),+ equalTo(Path.of("/home/u", ".local", "share", "floci-aws")));+ }++ @Test+ void macUsesApplicationSupport() {+ assertThat(HostDataDir.forPlatform("Mac OS X", Map.of(), "/Users/u"),+ equalTo(Path.of("/Users/u", "Library", "Application Support", "floci-aws")));+ }++ @Test+ void windowsUsesLocalAppData() {+ assertThat(HostDataDir.forPlatform("Windows 11", Map.of("LOCALAPPDATA", "C:/Users/u/AppData/Local"), "C:/Users/u"),+ equalTo(Path.of("C:/Users/u/AppData/Local", "floci-aws")));+ assertThat(HostDataDir.forPlatform("Windows 11", Map.of(), "C:/Users/u"),+ equalTo(Path.of("C:/Users/u", "AppData", "Local", "floci-aws")));+ }++ @Test+ void jvmRunsKeepTheBundledDefault() {+ // The test JVM is not a native image, so nothing moves.+ assertThat(HostDataDir.persistentPathDefault(), equalTo("./data"));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/config/HostDefaultsConfigSourceTest.java b/src/test/java/io/github/hectorvent/floci/config/HostDefaultsConfigSourceTest.javanew file mode 100644index 0000000000..14c0ca93bd--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/config/HostDefaultsConfigSourceTest.java@@ -0,0 +1,39 @@+package io.github.hectorvent.floci.config;++import org.junit.jupiter.api.Test;++import static org.hamcrest.MatcherAssert.assertThat;+import static org.hamcrest.Matchers.aMapWithSize;+import static org.hamcrest.Matchers.allOf;+import static org.hamcrest.Matchers.equalTo;+import static org.hamcrest.Matchers.greaterThan;+import static org.hamcrest.Matchers.lessThan;+import static org.hamcrest.Matchers.nullValue;++class HostDefaultsConfigSourceTest {++ @Test+ void emptyOutsideANativeHostRun() {+ HostDefaultsConfigSource source = new HostDefaultsConfigSource(false);+ assertThat(source.getProperties(), aMapWithSize(0));+ assertThat(source.getValue(HostDefaultsConfigSource.PERSISTENT_PATH), nullValue());+ }++ @Test+ void jvmConstructorIsEmpty() {+ assertThat(new HostDefaultsConfigSource().getProperties(), aMapWithSize(0));+ }++ @Test+ void nativeHostRunSetsBothPathsToTheSameDirectory() {+ HostDefaultsConfigSource source = new HostDefaultsConfigSource(true);+ String dir = source.getValue(HostDefaultsConfigSource.PERSISTENT_PATH);+ assertThat(source.getValue(HostDefaultsConfigSource.HOST_PERSISTENT_PATH), equalTo(dir));+ }++ @Test+ void ordinalOverridesOnlyTheBundledYaml() {+ // classpath application.yml is 255, config/application.yml in the working directory 265.+ assertThat(new HostDefaultsConfigSource(false).getOrdinal(), allOf(greaterThan(255), lessThan(265)));+ }+}