diff --git a/core/src/main/java/dev/streamx/cli/StreamxCommand.java b/core/src/main/java/dev/streamx/cli/StreamxCommand.java index c16b95aa..1d383669 100644 --- a/core/src/main/java/dev/streamx/cli/StreamxCommand.java +++ b/core/src/main/java/dev/streamx/cli/StreamxCommand.java @@ -18,6 +18,8 @@ import io.quarkus.runtime.QuarkusApplication; import io.quarkus.runtime.annotations.QuarkusMain; import jakarta.inject.Inject; +import java.text.SimpleDateFormat; +import java.util.Date; import org.jetbrains.annotations.Nullable; import picocli.CommandLine; import picocli.CommandLine.ArgGroup; @@ -39,6 +41,10 @@ }, versionProvider = VersionProvider.class) public class StreamxCommand implements QuarkusApplication { + private static final SimpleDateFormat DATE_FORMAT = + new SimpleDateFormat("yyyy_MM_dd__HH_mm_ss_SSS"); + private static final String LOG_FILE_PATH_PROPERTY_NAME = "%prod.quarkus.log.file.path"; + private static final String STREAMX_LOG_FILE_NAME_PATTERN = "%s/.streamx/logs/streamx-%s.log"; @Inject CommandLine.IFactory factory; @@ -67,6 +73,8 @@ public class StreamxCommand implements QuarkusApplication { public static void main(String... args) { initializeArgumentConfigSource(args); + overrideLogFileName(); + Quarkus.run(StreamxCommand.class, args); } @@ -121,6 +129,18 @@ private int executionStrategy(ParseResult parseResult) { } } + private static void overrideLogFileName() { + if (System.getProperty(LOG_FILE_PATH_PROPERTY_NAME) != null) { + return; + } + + String userHome = System.getProperty("user.home"); + String date = DATE_FORMAT.format(new Date()); + String streamxLogPath = String.format(STREAMX_LOG_FILE_NAME_PATTERN, userHome, date); + + System.setProperty(LOG_FILE_PATH_PROPERTY_NAME, streamxLogPath); + } + private void init() { bannerPrinter.initialize(commandLine, args); diff --git a/core/src/main/resources/application.properties b/core/src/main/resources/application.properties index 7dea5d25..9d7d0126 100644 --- a/core/src/main/resources/application.properties +++ b/core/src/main/resources/application.properties @@ -19,7 +19,7 @@ streamx.runner.generated-keys.location=${user.home}/.streamx/config/generated %prod.quarkus.log.console.enable=false %prod.quarkus.log.file.enable=true -# WARNING! %prod.quarkus.log.file.path is overridden by EntrypointMain +# WARNING! %prod.quarkus.log.file.path is overridden by StreamxCommand # Log name is overriden to log to date-specific log to allow multiple commands to be run paralleled. %prod.quarkus.log.file.path=${user.home}/.streamx/logs/streamx.log %prod.quarkus.log.file.rotation.max-backup-index=10 diff --git a/core/src/test/java/dev/streamx/cli/StreamxCommandTest.java b/core/src/test/java/dev/streamx/cli/StreamxCommandTest.java new file mode 100644 index 00000000..3f322284 --- /dev/null +++ b/core/src/test/java/dev/streamx/cli/StreamxCommandTest.java @@ -0,0 +1,43 @@ +package dev.streamx.cli; + +import java.io.IOException; +import java.nio.file.Files; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class StreamxCommandTest { + + public static final String TIMESTAMP_REGEX = + "[0-9]{4}_[0-9]{2}_[0-9]{2}__[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{3}"; + private static final String STREAMX_LOG_REGEX = + ".*/\\.streamx/logs/streamx-" + TIMESTAMP_REGEX + "\\.log"; + + @Test + void shouldOverrideProdFileLogName() throws IOException { + // given + String userHome = Files.createTempDirectory("").toFile().getAbsolutePath(); + System.clearProperty("%prod.quarkus.log.file.path"); + System.setProperty("user.home", userHome); + + // when + StreamxCommand.main(new String[] {}); + + // then + String fileName = System.getProperty("%prod.quarkus.log.file.path"); + Assertions.assertTrue(fileName.matches(STREAMX_LOG_REGEX)); + Assertions.assertTrue(fileName.startsWith(userHome)); + } + + @Test + void shouldUseProvidedProdFileLogName() { + // given + System.setProperty("%prod.quarkus.log.file.path", ".streamx.log"); + + // when + StreamxCommand.main(new String[] {}); + + // then + String fileName = System.getProperty("%prod.quarkus.log.file.path"); + Assertions.assertEquals(".streamx.log", fileName); + } +} diff --git a/distribution/pom.xml b/distribution/pom.xml index f88258bb..e8f05bbf 100644 --- a/distribution/pom.xml +++ b/distribution/pom.xml @@ -20,10 +20,6 @@ - - dev.streamx.cli - streamx-cli-entrypoint - dev.streamx.cli streamx-cli-core @@ -47,7 +43,7 @@ - dev.streamx.cli.EntrypointMain + dev.streamx.cli.StreamxCommand diff --git a/entrypoint/pom.xml b/entrypoint/pom.xml deleted file mode 100644 index 63bc6d6d..00000000 --- a/entrypoint/pom.xml +++ /dev/null @@ -1,52 +0,0 @@ - - - 4.0.0 - - dev.streamx.cli - streamx-cli - 1.1.3-SNAPSHOT - - - streamx-cli-entrypoint - - StreamX Cli : Entrypoint - - - 8 - 8 - UTF-8 - - - - - org.junit - junit-bom - 5.12.1 - pom - import - - - - - - - org.junit.jupiter - junit-jupiter - test - - - org.junit.jupiter - junit-jupiter-params - test - - - - - - - maven-surefire-plugin - 3.5.3 - - - - diff --git a/entrypoint/src/main/java/dev/streamx/cli/EntrypointMain.java b/entrypoint/src/main/java/dev/streamx/cli/EntrypointMain.java deleted file mode 100644 index 81edfde6..00000000 --- a/entrypoint/src/main/java/dev/streamx/cli/EntrypointMain.java +++ /dev/null @@ -1,61 +0,0 @@ -package dev.streamx.cli; - -import java.lang.reflect.Method; -import java.text.SimpleDateFormat; -import java.util.Date; - -public class EntrypointMain { - - private static final SimpleDateFormat DATE_FORMAT = - new SimpleDateFormat("yyyy_MM_dd__HH_mm_ss_SSS"); - private static final String LOG_FILE_PATH_PROPERTY_NAME = "%prod.quarkus.log.file.path"; - private static final String STREAMX_LOG_FILE_NAME_PATTERN = "%s/.streamx/logs/streamx-%s.log"; - - public static void main(String[] args) { - int javaVersion = getJavaVersion(); - - if (javaVersion < 17) { - System.out.println("Java 17 or higher is required!"); - return; - } - - overrideLogFileName(); - runStreamxCommand(args); - } - - private static int getJavaVersion() { - String version = System.getProperty("java.version"); - if (version.startsWith("1.")) { - version = version.substring(2, 3); - } else { - int dot = version.indexOf("."); - if (dot != -1) { - version = version.substring(0, dot); - } - } - return Integer.parseInt(version); - } - - private static void overrideLogFileName() { - if (System.getProperty(LOG_FILE_PATH_PROPERTY_NAME) != null) { - return; - } - - String userHome = System.getProperty("user.home"); - String date = DATE_FORMAT.format(new Date()); - String streamxLogPath = String.format(STREAMX_LOG_FILE_NAME_PATTERN, userHome, date); - - System.setProperty(LOG_FILE_PATH_PROPERTY_NAME, streamxLogPath); - } - - private static void runStreamxCommand(String[] args) { - try { - Class streamxCommand = Class.forName("dev.streamx.cli.StreamxCommand"); - Method main = streamxCommand.getDeclaredMethod("main", String[].class); - - main.invoke(null, new Object[]{args}); - } catch (Exception e) { - throw new RuntimeException(e); - } - } -} diff --git a/entrypoint/src/test/java/dev/streamx/cli/EntrypointMainTest.java b/entrypoint/src/test/java/dev/streamx/cli/EntrypointMainTest.java deleted file mode 100644 index a790c2e5..00000000 --- a/entrypoint/src/test/java/dev/streamx/cli/EntrypointMainTest.java +++ /dev/null @@ -1,88 +0,0 @@ -package dev.streamx.cli; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import java.nio.file.Files; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -public class EntrypointMainTest { - - public static final String TIMESTAMP_REGEX = - "[0-9]{4}_[0-9]{2}_[0-9]{2}__[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{3}"; - private static final String STREAMX_LOG_REGEX = - ".*/\\.streamx/logs/streamx-" + TIMESTAMP_REGEX + "\\.log"; - - @BeforeEach - void setup() { - StreamxCommand.clearLaunched(); - } - - @Test - void shouldLaunchStreamxCommand() { - // when - EntrypointMain.main(new String[] {}); - - // then - Assertions.assertTrue(StreamxCommand.isLaunched()); - } - - @ParameterizedTest - @ValueSource(strings = { - "1.8.0_211", - "9.0.1", - "11.0.4", - "12", - "12.0.1" - }) - void shouldFailTooLowJavaVersions(String javaVersion) { - // given - System.clearProperty("java.version"); - System.setProperty("java.version", javaVersion); - - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(byteArrayOutputStream)); - - // when - EntrypointMain.main(new String[] {}); - - // then - Assertions.assertFalse(StreamxCommand.isLaunched()); - Assertions.assertTrue( - byteArrayOutputStream.toString().contains("Java 17 or higher is required!") - ); - } - - @Test - void shouldOverrideProdFileLogName() throws IOException { - // given - String userHome = Files.createTempDirectory("").toFile().getAbsolutePath(); - System.clearProperty("%prod.quarkus.log.file.path"); - System.setProperty("user.home", userHome); - - // when - EntrypointMain.main(new String[] {}); - - // then - String fileName = System.getProperty("%prod.quarkus.log.file.path"); - Assertions.assertTrue(fileName.matches(STREAMX_LOG_REGEX)); - Assertions.assertTrue(fileName.startsWith(userHome)); - } - - @Test - void shouldUseProvidedProdFileLogName() { - // given - System.setProperty("%prod.quarkus.log.file.path", ".streamx.log"); - - // when - EntrypointMain.main(new String[] {}); - - // then - String fileName = System.getProperty("%prod.quarkus.log.file.path"); - Assertions.assertEquals(".streamx.log", fileName); - } -} diff --git a/entrypoint/src/test/java/dev/streamx/cli/StreamxCommand.java b/entrypoint/src/test/java/dev/streamx/cli/StreamxCommand.java deleted file mode 100644 index 5d1fc44c..00000000 --- a/entrypoint/src/test/java/dev/streamx/cli/StreamxCommand.java +++ /dev/null @@ -1,20 +0,0 @@ -package dev.streamx.cli; - -import java.util.concurrent.atomic.AtomicBoolean; - -public class StreamxCommand { - - private static final AtomicBoolean LAUNCHED = new AtomicBoolean(false); - - public static void main(String[] args) { - LAUNCHED.set(true); - } - - public static boolean isLaunched() { - return LAUNCHED.get(); - } - - public static void clearLaunched() { - LAUNCHED.set(false); - } -} \ No newline at end of file diff --git a/pom.xml b/pom.xml index e0ec4b0a..551882eb 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,6 @@ StreamX Cli : Parent - entrypoint core distribution e2e-tests @@ -64,11 +63,6 @@ import - - dev.streamx.cli - streamx-cli-entrypoint - ${project.version} - dev.streamx.cli streamx-cli-core @@ -234,6 +228,11 @@ check + + + dev.streamx.cli/StreamxCommand.class + BUNDLE