diff --git a/build.gradle.kts b/build.gradle.kts index 4029a62..487d0e5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,8 @@ +import org.apache.tools.ant.filters.ReplaceTokens +import java.util.* + plugins { - id("groovy") + id("groovy") id("io.micronaut.application") version "4.5.3" id("com.gradleup.shadow") version "8.3.6" id("io.micronaut.openapi") version "4.5.3" @@ -10,7 +13,7 @@ plugins { version = project.properties["justserveCliVersion"]!! group = "org.justserve" -apply(from="gradle/asciidoc.gradle") +apply(from = "gradle/asciidoc.gradle") repositories { mavenCentral() @@ -38,11 +41,15 @@ java { sourceCompatibility = JavaVersion.toVersion("21") targetCompatibility = JavaVersion.toVersion("21") } -tasks.withType().configureEach { - // This makes the project version available as a system property to the test JVM. - // Micronaut can then read and inject it using @Value. - systemProperty("justserveCliVersion", version) + +tasks.withType { + val props = Properties() + file("gradle.properties").inputStream().use { props.load(it) } + filesMatching("**/application.yml") { + filter(mapOf("tokens" to props), ReplaceTokens::class.java) + } } + micronaut { testRuntime("spock2") openapi { @@ -74,5 +81,3 @@ graalvmNative.binaries { tasks.named("dockerfileNative") { jdkVersion = "21" } - - diff --git a/gradle.properties b/gradle.properties index c00a378..785c3fe 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,2 @@ micronautVersion=4.8.3 -justserveCliVersion=0.0.3-SNAPSHOT \ No newline at end of file +justserveCliVersion=0.0.3 \ No newline at end of file diff --git a/src/main/java/org/justserve/FulcrumCommand.java b/src/main/java/org/justserve/FulcrumCommand.java index c95b506..aaa92bb 100644 --- a/src/main/java/org/justserve/FulcrumCommand.java +++ b/src/main/java/org/justserve/FulcrumCommand.java @@ -1,18 +1,19 @@ package org.justserve; +import static org.justserve.JustServeUtils.justServePrint; +import static org.justserve.JustServeUtils.justServePrintErr; +import org.justserve.client.UserClient; +import org.justserve.model.UserHashRequestByEmail; + import io.micronaut.configuration.picocli.PicocliRunner; import io.micronaut.context.annotation.Value; import io.micronaut.http.HttpResponse; import io.micronaut.http.client.exceptions.HttpClientResponseException; import jakarta.inject.Inject; -import org.justserve.client.UserClient; -import org.justserve.model.UserHashRequestByEmail; +import jakarta.inject.Provider; import picocli.CommandLine.Command; import picocli.CommandLine.Option; -import static org.justserve.JustServeUtils.justServePrint; -import static org.justserve.JustServeUtils.justServePrintErr; - @Command(name = "justserve", description = "justserve-cli is a terminal tool to help specialists and admin using JustServe") public class FulcrumCommand implements Runnable { @@ -23,10 +24,11 @@ public class FulcrumCommand implements Runnable { @Option(names = {"version", "--version", "-v"}) boolean version = false; - String justServeCliVersion = "0.0.3-SNAPSHOT"; + @Value("${micronaut.application.version}") + String justserveCliVersion; @Inject - UserClient userClient; + Provider userClientProvider; @Value("${justserve.token}") String token; @@ -37,16 +39,17 @@ public static void main(String[] args) { public void run() { if (version) { - justServePrint(justServeCliVersion); + justServePrint(justserveCliVersion); return; } HttpResponse response; - if ("i-need-to-be-defined".equals(token) || null == token) { + if ("i-need-to-be-defined".equals(token) || null == token ) { justServePrintErr("The Authentication token is not assigned as an environment variable. " + "Please define the environment variable \"JUSTSERVE_TOKEN\" and try again."); return; } try { + UserClient userClient = userClientProvider.get(); response = userClient.getTempPassword(new UserHashRequestByEmail(email)); } catch (HttpClientResponseException e) { justServePrintErr("received an unexpected response from JustServe: %d (%s)%n", diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e1c1e83..2e77b36 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,14 +1,15 @@ micronaut: application: - name: fulcrum + name: justserve-cli + version: "@justserveCliVersion@" http: services: justserve: - url: ${JUSTSERVE_URL:`https://www.justserve.org`} + url: ${:https://www.justserve.org} client: read-timeout: 20s justserve: - token: ${JUSTSERVE_TOKEN:i-need-to-be-defined} + token: ${:i-need-to-be-defined} jackson: deserialization: ACCEPT_EMPTY_STRING_AS_NULL_OBJECT: true \ No newline at end of file diff --git a/src/test/groovy/org/justserve/FulcrumCommandSpec.groovy b/src/test/groovy/org/justserve/FulcrumCommandSpec.groovy index 02a4a78..2ae17db 100644 --- a/src/test/groovy/org/justserve/FulcrumCommandSpec.groovy +++ b/src/test/groovy/org/justserve/FulcrumCommandSpec.groovy @@ -2,6 +2,7 @@ package org.justserve import io.micronaut.configuration.picocli.PicocliRunner import io.micronaut.context.ApplicationContext +import io.micronaut.context.annotation.Value import io.micronaut.context.env.Environment import io.micronaut.test.extensions.spock.annotation.MicronautTest import spock.lang.Shared @@ -16,6 +17,9 @@ class FulcrumCommandSpec extends Specification { @Shared Pattern cliVersion + @Shared + String justServeUrl + @Shared Pattern blankRegex = Pattern.compile "^\\s*\$" @@ -31,6 +35,7 @@ class FulcrumCommandSpec extends Specification { def setupSpec() { + justServeUrl = System.getenv("JUSTSERVE_URL") ?: "https://www.justserve.org" def props = new Properties() new File('gradle.properties').withInputStream { stream -> props.load(stream) @@ -49,7 +54,8 @@ class FulcrumCommandSpec extends Specification { ctx = ApplicationContext.builder() .environments(Environment.CLI, Environment.TEST) .properties([ - "JUSTSERVE_TOKEN": System.getenv("TEST_TOKEN") + "justserve.token": System.getenv("TEST_TOKEN"), + "micronaut.http.services.justserve.url" : justServeUrl ]) .build() .start() @@ -58,6 +64,7 @@ class FulcrumCommandSpec extends Specification { .builder() .environments(Environment.CLI, Environment.TEST) .environmentVariableExcludes("JUSTSERVE_TOKEN") + .properties(["micronaut.http.services.justserve.url": justServeUrl]) .build() .start() } diff --git a/src/test/groovy/org/justserve/client/UserClientSpec.groovy b/src/test/groovy/org/justserve/client/UserClientSpec.groovy index 7e2cc3f..dd4647a 100644 --- a/src/test/groovy/org/justserve/client/UserClientSpec.groovy +++ b/src/test/groovy/org/justserve/client/UserClientSpec.groovy @@ -24,18 +24,21 @@ class UserClientSpec extends Specification { UserClient userClient void setupSpec() { + def justServeUrl = System.getenv("JUSTSERVE_URL") ?: "https://www.justserve.org" if (null != System.getenv("JUSTSERVE_TOKEN")) { throw new IllegalStateException("JUSTSERVE_TOKEN is set. Do not define this variable in testing.") } noAuthCtx = ApplicationContext.builder(EmbeddedServer) .environmentVariableExcludes("JUSTSERVE_TOKEN", "TEST_TOKEN") + .properties(["micronaut.http.services.justserve.url": justServeUrl]) .build() .start() noAuthUserClient = noAuthCtx.getBean(UserClient) ctx = ApplicationContext.builder(EmbeddedServer) .environmentVariableExcludes("JUSTSERVE_TOKEN") .properties([ - "JUSTSERVE_TOKEN": System.getenv("TEST_TOKEN") + "justserve.token" : System.getenv("TEST_TOKEN"), + "micronaut.http.services.justserve.url": justServeUrl ]) .build() .start()