|
| 1 | +package com.amazon.tools.cli; |
| 2 | + |
| 3 | + |
| 4 | +import com.amazon.ion.IonReader; |
| 5 | +import com.amazon.ion.IonWriter; |
| 6 | +import com.amazon.ion.system.IonReaderBuilder; |
| 7 | +import picocli.CommandLine; |
| 8 | +import picocli.CommandLine.Command; |
| 9 | +import picocli.CommandLine.HelpCommand; |
| 10 | +import picocli.CommandLine.Option; |
| 11 | +import picocli.CommandLine.Parameters; |
| 12 | + |
| 13 | +import java.io.ByteArrayInputStream; |
| 14 | +import java.io.File; |
| 15 | +import java.io.FileDescriptor; |
| 16 | +import java.io.FileInputStream; |
| 17 | +import java.io.FileNotFoundException; |
| 18 | +import java.io.FileOutputStream; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.io.OutputStream; |
| 22 | +import java.io.SequenceInputStream; |
| 23 | +import java.util.Arrays; |
| 24 | + |
| 25 | +@Command( |
| 26 | + name = SimpleIonCli.NAME, |
| 27 | + version = SimpleIonCli.VERSION, |
| 28 | + subcommands = {HelpCommand.class}, |
| 29 | + mixinStandardHelpOptions = true |
| 30 | +) |
| 31 | +class SimpleIonCli { |
| 32 | + |
| 33 | + public static final String NAME = "jion"; |
| 34 | + public static final String VERSION = "2024-10-31"; |
| 35 | + //TODO: Replace with InputStream.nullInputStream in JDK 11+ |
| 36 | + public static final InputStream EMPTY = new ByteArrayInputStream(new byte[0]); |
| 37 | + |
| 38 | + public static void main(String[] args) { |
| 39 | + CommandLine commandLine = new CommandLine(new SimpleIonCli()) |
| 40 | + .setCaseInsensitiveEnumValuesAllowed(true) |
| 41 | + .setUsageHelpAutoWidth(true); |
| 42 | + System.exit(commandLine.execute(args)); |
| 43 | + } |
| 44 | + |
| 45 | + @Option(names={"-f", "--format", "--output-format"}, defaultValue = "pretty", |
| 46 | + description = "Output format, from the set (text | pretty | binary | none).", |
| 47 | + paramLabel = "<format>", |
| 48 | + scope = CommandLine.ScopeType.INHERIT) |
| 49 | + OutputFormat outputFormat; |
| 50 | + |
| 51 | + @Option(names={"-o", "--output"}, paramLabel = "FILE", description = "Output file", |
| 52 | + scope = CommandLine.ScopeType.INHERIT) |
| 53 | + File outputFile; |
| 54 | + |
| 55 | + @Command(name = "cat", aliases = {"process"}, |
| 56 | + description = "concatenate FILE(s) in the requested Ion output format", |
| 57 | + mixinStandardHelpOptions = true) |
| 58 | + int cat( @Parameters(paramLabel = "FILE") File... files) { |
| 59 | + |
| 60 | + if (outputFormat == OutputFormat.EVENTS) { |
| 61 | + System.err.println("'events' output format is not supported"); |
| 62 | + return CommandLine.ExitCode.USAGE; |
| 63 | + } |
| 64 | + |
| 65 | + //TODO: This is not resilient to problems with a single file. Should it be? |
| 66 | + try (InputStream in = getInputStream(files); |
| 67 | + IonReader reader = IonReaderBuilder.standard().build(in); |
| 68 | + OutputStream out = getOutputStream(outputFile); |
| 69 | + IonWriter writer = outputFormat.createIonWriter(out)) { |
| 70 | + // getInputStream will look for stdin if we don't supply |
| 71 | + writer.writeValues(reader); |
| 72 | + } catch (IOException e) { |
| 73 | + System.err.println(e.getMessage()); |
| 74 | + return CommandLine.ExitCode.SOFTWARE; |
| 75 | + } |
| 76 | + |
| 77 | + // process files |
| 78 | + return CommandLine.ExitCode.OK; |
| 79 | + } |
| 80 | + |
| 81 | + private static InputStream getInputStream(File... files) { |
| 82 | + if (files == null || files.length == 0) return new FileInputStream(FileDescriptor.in); |
| 83 | + |
| 84 | + // As convenient as this formulation is I'm not sure of the ordering guarantees here |
| 85 | + // Revisit if that is ever problematic |
| 86 | + return Arrays.stream(files) |
| 87 | + .map(SimpleIonCli::getInputStream) |
| 88 | + .reduce(EMPTY, SequenceInputStream::new); |
| 89 | + } |
| 90 | + |
| 91 | + private static InputStream getInputStream(File inputFile) { |
| 92 | + try { |
| 93 | + return new FileInputStream(inputFile); |
| 94 | + } catch (FileNotFoundException e) { |
| 95 | + throw cloak(e); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Removing some boilerplate from checked-exception consuming paths, without RuntimeException wrapping |
| 100 | + // JLS Section 18.4 covers type inference for generic methods, |
| 101 | + // including the rule that `throws T` is inferred as RuntimeException if possible. |
| 102 | + // See e.g. https://www.rainerhahnekamp.com/en/ignoring-exceptions-in-java/ |
| 103 | + private static <T extends Throwable> T cloak(Throwable t) throws T { |
| 104 | + @SuppressWarnings("unchecked") |
| 105 | + T result = (T) t; |
| 106 | + return result; |
| 107 | + } |
| 108 | + |
| 109 | + private static FileOutputStream getOutputStream(File outputFile) throws IOException { |
| 110 | + // non-line-buffered stdout, or the requested file output |
| 111 | + return outputFile == null ? new FileOutputStream(FileDescriptor.out) : new FileOutputStream(outputFile); |
| 112 | + } |
| 113 | +} |
0 commit comments