Created
June 2, 2022 19:34
-
-
Save michael-simons/53b3b1d7bf9a28154550dd0a8e8d6014 to your computer and use it in GitHub Desktop.
A simple formatter for Cypher.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///usr/bin/env jbang "$0" "$@" ; exit $? | |
//JAVA 17 | |
//DEPS info.picocli:picocli:4.6.3 | |
//DEPS org.neo4j:neo4j-cypher-dsl-parser:2022.4.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.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.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); | |
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment