Skip to content

Commit

Permalink
toml-path: match usage of yaml-path (#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
itzg authored Feb 22, 2025
1 parent f1b4985 commit 61dd4de
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
20 changes: 7 additions & 13 deletions src/main/java/me/itzg/helpers/files/TomlPathCommand.java
Original file line number Diff line number Diff line change
@@ -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<Integer> {

public static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<Map<String, Object>>() {
};
@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 {

Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/me/itzg/helpers/files/TomlPathCommandTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 61dd4de

Please sign in to comment.