Skip to content

Commit 7d716ce

Browse files
dfa1claude
andcommitted
feat(cli): add export and schema subcommands
export writes CSV to stdout; schema prints machine-readable dtype. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1619a6b commit 7d716ce

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.github.dfa1.vortex.cli;
2+
3+
import io.github.dfa1.vortex.csv.CsvExporter;
4+
import io.github.dfa1.vortex.csv.ExportOptions;
5+
6+
import java.io.IOException;
7+
import java.io.OutputStreamWriter;
8+
import java.io.Writer;
9+
import java.nio.charset.StandardCharsets;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
13+
final class ExportCommand {
14+
15+
private ExportCommand() {
16+
}
17+
18+
static int run(String[] args) {
19+
if (args.length != 2) {
20+
System.err.println("usage: export <file.vortex>");
21+
return 1;
22+
}
23+
Path path = Path.of(args[1]);
24+
if (!Files.exists(path)) {
25+
System.err.println("file not found: " + path);
26+
return 2;
27+
}
28+
try {
29+
Writer stdout = new OutputStreamWriter(System.out, StandardCharsets.UTF_8);
30+
CsvExporter.exportCsv(path, stdout, ExportOptions.defaults());
31+
stdout.flush();
32+
return 0;
33+
} catch (IOException e) {
34+
System.err.println("error: " + e.getMessage());
35+
return 3;
36+
}
37+
}
38+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package io.github.dfa1.vortex.cli;
2+
3+
import io.github.dfa1.vortex.core.DType;
4+
import io.github.dfa1.vortex.io.VortexReader;
5+
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
10+
final class SchemaCommand {
11+
12+
private SchemaCommand() {
13+
}
14+
15+
static int run(String[] args) {
16+
if (args.length != 2) {
17+
System.err.println("usage: schema <file.vortex>");
18+
return 1;
19+
}
20+
Path path = Path.of(args[1]);
21+
if (!Files.exists(path)) {
22+
System.err.println("file not found: " + path);
23+
return 2;
24+
}
25+
try (VortexReader reader = VortexReader.open(path)) {
26+
System.out.println(formatDType(reader.dtype()));
27+
return 0;
28+
} catch (IOException e) {
29+
System.err.println("error: " + e.getMessage());
30+
return 3;
31+
}
32+
}
33+
34+
private static String formatDType(DType dtype) {
35+
return switch (dtype) {
36+
case DType.Struct s -> {
37+
var sb = new StringBuilder("struct<");
38+
for (int i = 0; i < s.fieldNames().size(); i++) {
39+
if (i > 0) {
40+
sb.append(", ");
41+
}
42+
sb.append(s.fieldNames().get(i)).append(": ").append(formatDType(s.fieldTypes().get(i)));
43+
}
44+
sb.append('>');
45+
yield sb.toString();
46+
}
47+
case DType.Primitive(var pt, var nullable) -> pt.name() + (nullable ? "?" : "");
48+
case DType.Utf8(var nullable) -> "utf8" + (nullable ? "?" : "");
49+
case DType.Binary(var nullable) -> "binary" + (nullable ? "?" : "");
50+
case DType.Bool(var nullable) -> "bool" + (nullable ? "?" : "");
51+
case DType.Null ignored -> "null";
52+
case DType.Decimal(var p, var s, var nullable) -> "decimal(" + p + "," + s + ")" + (nullable ? "?" : "");
53+
case DType.List(var elem, var nullable) -> "list<" + formatDType(elem) + ">" + (nullable ? "?" : "");
54+
case DType.FixedSizeList(var elem, var size, var nullable) ->
55+
"list<" + formatDType(elem) + ">[" + size + "]" + (nullable ? "?" : "");
56+
case DType.Extension(var id, var storage, var meta, var nullable) ->
57+
"ext<" + id + ">" + (nullable ? "?" : "");
58+
};
59+
}
60+
}

cli/src/main/java/io/github/dfa1/vortex/cli/VortexCli.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public static void main(String[] args) {
1717
}
1818
int exit = switch (args[0]) {
1919
case "inspect" -> InspectCommand.run(args);
20+
case "export" -> ExportCommand.run(args);
2021
case "import" -> ImportCommand.run(args);
22+
case "schema" -> SchemaCommand.run(args);
2123
default -> {
2224
System.err.println("unknown subcommand: " + args[0]);
2325
printUsage(System.err);
@@ -30,6 +32,8 @@ public static void main(String[] args) {
3032
static void printUsage(PrintStream out) {
3133
out.println("Usage: java -jar vortex.jar <subcommand> [args]");
3234
out.println(" inspect <file.vortex> print file structure");
35+
out.println(" export <file.vortex> write CSV to stdout");
3336
out.println(" import <file.csv> [out.vortex] convert CSV to Vortex");
37+
out.println(" schema <file.vortex> print dtype (machine-readable)");
3438
}
3539
}

0 commit comments

Comments
 (0)