-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathformat_cypher.java
executable file
·58 lines (48 loc) · 1.82 KB
/
format_cypher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 17
//DEPS info.picocli:picocli:4.7.4
//DEPS org.neo4j:neo4j-cypher-dsl-parser:2023.5.0
import picocli.CommandLine;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import org.neo4j.cypherdsl.core.Statement;
import org.neo4j.cypherdsl.core.TreeNode;
import org.neo4j.cypherdsl.core.renderer.Configuration;
import org.neo4j.cypherdsl.core.renderer.Renderer;
import org.neo4j.cypherdsl.parser.CypherParser;
public class format_cypher implements Callable<Integer> {
public static void main(String... args) {
int exitCode = new CommandLine(new format_cypher()).execute(args);
System.exit(exitCode);
}
@CommandLine.Option(names = "--print-tree")
boolean printTree = false;
@CommandLine.Parameters(index = "0", description = "Cypher to format", arity = "0")
String input;
@Override
public Integer call() {
var cypher = this.input == null ? readStdin() : this.input;
if (cypher.isBlank()) {
throw new IllegalArgumentException("Please enter some valid cypher");
}
var statement = CypherParser.parse(cypher);
if (printTree) {
TreeNode.from(statement).printTo(System.out::append, node -> node.getValue() instanceof Statement s ? s.getCypher() : node.getValue().toString());
} else {
System.out.println(Renderer.getRenderer(Configuration.prettyPrinting()).render(statement));
}
return 0;
}
private static String readStdin() {
System.err.println("Reading from stdin, end with EOF (most likely CTRL+D).");
try (var in = new BufferedReader(new InputStreamReader(System.in))) {
return in.lines().collect(Collectors.joining(System.lineSeparator()));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}