From 158e89cf2c7ab3376eb4caad11527470047825fc Mon Sep 17 00:00:00 2001 From: jonathan zollinger Date: Mon, 10 Jun 2024 11:45:18 -0600 Subject: [PATCH] wip: add ThreshrCli & Spec. copy over application.yaml from core --- cli/pom.xml | 2 +- cli/src/main/java/com/graqr/Application.java | 10 ----- cli/src/main/java/com/graqr/ThreshrCli.java | 43 ++++++++++++++++++ cli/src/main/resources/application.properties | 6 --- cli/src/main/resources/application.yaml | 19 ++++++++ .../groovy/com/graqr/ThreshrCliSpec.groovy | 44 ++++++++++++++++--- 6 files changed, 100 insertions(+), 24 deletions(-) delete mode 100644 cli/src/main/java/com/graqr/Application.java create mode 100644 cli/src/main/java/com/graqr/ThreshrCli.java delete mode 100644 cli/src/main/resources/application.properties create mode 100644 cli/src/main/resources/application.yaml diff --git a/cli/pom.xml b/cli/pom.xml index bd227cf..d731078 100644 --- a/cli/pom.xml +++ b/cli/pom.xml @@ -15,7 +15,7 @@ true false com.graqr.aot.generated - com.graqr.Application + com.graqr.ThreshrCli diff --git a/cli/src/main/java/com/graqr/Application.java b/cli/src/main/java/com/graqr/Application.java deleted file mode 100644 index 1899540..0000000 --- a/cli/src/main/java/com/graqr/Application.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.graqr; - -import io.micronaut.runtime.Micronaut; - -public class Application { - - public static void main(String[] args) { - Micronaut.run(Application.class, args); - } -} \ No newline at end of file diff --git a/cli/src/main/java/com/graqr/ThreshrCli.java b/cli/src/main/java/com/graqr/ThreshrCli.java new file mode 100644 index 0000000..29e9b2a --- /dev/null +++ b/cli/src/main/java/com/graqr/ThreshrCli.java @@ -0,0 +1,43 @@ +package com.graqr; + +import com.graqr.threshr.Threshr; +import com.graqr.threshr.ThreshrException; +import com.graqr.threshr.model.queryparam.Tcin; +import io.micronaut.configuration.picocli.PicocliRunner; +import jakarta.inject.Singleton; +import picocli.CommandLine; +import picocli.CommandLine.Command; +import picocli.CommandLine.Option; + +@Singleton +@Command(name = "threshr grocery query tool", mixinStandardHelpOptions = true) +public class ThreshrCli implements Runnable { + + Threshr threshr; + + @Option( + names = {"--tcin", "-t", "product-id-number"}, + required = false, + description = "", converter = TcinsConverter.class) + Tcin[] tcinValues; + + public ThreshrCli(Threshr threshr) { + this.threshr = threshr; + } + + + public static void main(String[] args) { + PicocliRunner.run(ThreshrCli.class, args); + } + + public void run() { + // TODO: do all the things :P + } + + static class TcinsConverter implements CommandLine.ITypeConverter { + @Override + public Tcin[] convert(String s) throws ThreshrException { + return new Tcin[]{new Tcin(s.split(","))}; + } + } +} \ No newline at end of file diff --git a/cli/src/main/resources/application.properties b/cli/src/main/resources/application.properties deleted file mode 100644 index 999eb28..0000000 --- a/cli/src/main/resources/application.properties +++ /dev/null @@ -1,6 +0,0 @@ -#Sat Jun 08 11:40:49 MDT 2024 -datasources.default.db-type=postgres -datasources.default.dialect=POSTGRES -micronaut.application.name=threshr-cli -datasources.default.schema-generate=CREATE_DROP -datasources.default.driver-class-name=org.postgresql.Driver diff --git a/cli/src/main/resources/application.yaml b/cli/src/main/resources/application.yaml new file mode 100644 index 0000000..b5ad78d --- /dev/null +++ b/cli/src/main/resources/application.yaml @@ -0,0 +1,19 @@ +micronaut: + application: + name: threshr-cli +--- +datasources: + default: + driver-class-name: org.postgresql.Driver + db-type: postgres + schema-generate: CREATE_DROP + dialect: POSTGRES +--- +threshr: + key: + channel: +--- +test: + datasources: + default: + url: \ No newline at end of file diff --git a/cli/src/test/groovy/com/graqr/ThreshrCliSpec.groovy b/cli/src/test/groovy/com/graqr/ThreshrCliSpec.groovy index c75777f..1e63bf8 100644 --- a/cli/src/test/groovy/com/graqr/ThreshrCliSpec.groovy +++ b/cli/src/test/groovy/com/graqr/ThreshrCliSpec.groovy @@ -1,19 +1,49 @@ package com.graqr -import io.micronaut.runtime.EmbeddedApplication +import io.micronaut.configuration.picocli.PicocliRunner +import io.micronaut.context.ApplicationContext +import io.micronaut.context.env.Environment import io.micronaut.test.extensions.spock.annotation.MicronautTest -import jakarta.inject.Inject +import spock.lang.Shared import spock.lang.Specification @MicronautTest class ThreshrCliSpec extends Specification { - @Inject - EmbeddedApplication application - void 'test it works'() { - expect: - application.running + @Shared + final PrintStream originalOut = System.out + @Shared + final PrintStream originalErr = System.err + + @Shared + ByteArrayOutputStream outputStream = new ByteArrayOutputStream() + ByteArrayOutputStream errStream = new ByteArrayOutputStream() + + void execute(String... args) { + try(ApplicationContext ctx = ApplicationContext.run(Environment.CLI, Environment.TEST) as ApplicationContext) { + PicocliRunner.run(ThreshrCli, ctx, args) + } + } + + + def setup() { + outputStream.reset() + errStream.reset() + System.setOut(new PrintStream(outputStream)) + System.setErr(new PrintStream(errStream)) + } + + def cleanup() { + System.setOut(originalOut) + System.setErr(originalErr) + } + + def "cli help can be queried successfully"(){ + when: + execute('--help') + then: + errStream.toString().trim().contains('Usage:') } }