From fe60f2834424b9ccb7deba4e81173b5960c42013 Mon Sep 17 00:00:00 2001 From: Geoff Bourne Date: Sat, 22 Feb 2025 14:29:59 -0600 Subject: [PATCH] toml-path: match usage of yaml-path --- .../itzg/helpers/files/TomlPathCommand.java | 20 +++++++------------ .../helpers/files/TomlPathCommandTest.java | 8 ++++---- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/main/java/me/itzg/helpers/files/TomlPathCommand.java b/src/main/java/me/itzg/helpers/files/TomlPathCommand.java index e1bd435b..e556d88c 100644 --- a/src/main/java/me/itzg/helpers/files/TomlPathCommand.java +++ b/src/main/java/me/itzg/helpers/files/TomlPathCommand.java @@ -1,35 +1,29 @@ package me.itzg.helpers.files; -import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.dataformat.toml.TomlMapper; import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.DocumentContext; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.ParseContext; import com.jayway.jsonpath.spi.json.JacksonJsonProvider; -import java.nio.file.Path; -import java.util.Map; +import java.io.File; import java.util.concurrent.Callable; import picocli.CommandLine.Command; import picocli.CommandLine.ExitCode; +import picocli.CommandLine.Option; import picocli.CommandLine.Parameters; @Command(name = "toml-path", description = "Extracts a path from a TOML file using json-path syntax") public class TomlPathCommand implements Callable { - public static final TypeReference> MAP_TYPE = new TypeReference>() { - }; + @Option(names = "--file", paramLabel = "FILE", description = "A TOML file to query. If not set, reads stdin") + File tomlFile; - @Parameters(index = "0", arity = "1", + @Parameters(arity = "1", paramLabel = "query", description = "JSON path expression where root element $ can be omitted") String query; - @Parameters(index = "1", arity = "0..1", - paramLabel = "file", - description = "TOML file or reads stdin") - Path path; - @Override public Integer call() throws Exception { @@ -40,8 +34,8 @@ public Integer call() throws Exception { ); final DocumentContext context; - if (path != null) { - context = parseContext.parse(path.toFile()); + if (tomlFile != null) { + context = parseContext.parse(tomlFile); } else { context = parseContext.parse(System.in); diff --git a/src/test/java/me/itzg/helpers/files/TomlPathCommandTest.java b/src/test/java/me/itzg/helpers/files/TomlPathCommandTest.java index f6661148..1b1d6ff5 100644 --- a/src/test/java/me/itzg/helpers/files/TomlPathCommandTest.java +++ b/src/test/java/me/itzg/helpers/files/TomlPathCommandTest.java @@ -1,6 +1,6 @@ package me.itzg.helpers.files; -import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOut; +import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOutNormalized; import static org.assertj.core.api.Assertions.assertThat; import java.nio.file.Paths; @@ -14,11 +14,11 @@ class TomlPathCommandTest { @ParameterizedTest @ValueSource(strings = {"$.bind", ".bind"}) void extractsVelocityBind(String queryPath) throws Exception { - final String out = tapSystemOut(() -> { + final String out = tapSystemOutNormalized(() -> { final int exitCode = new CommandLine(new TomlPathCommand()) .execute( - queryPath, - Paths.get("src/test/resources/velocity.toml").toString() + "--file", Paths.get("src/test/resources/velocity.toml").toString(), + queryPath ); assertThat(exitCode).isEqualTo(ExitCode.OK);