|
| 1 | +package io.cucumber.messages.cli; |
| 2 | + |
| 3 | +import picocli.CommandLine; |
| 4 | +import picocli.CommandLine.Model.CommandSpec; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.OutputStream; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.util.function.BiFunction; |
| 12 | + |
| 13 | +import static java.nio.file.Files.newOutputStream; |
| 14 | +import static java.nio.file.StandardOpenOption.CREATE; |
| 15 | +import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; |
| 16 | +import static java.util.Objects.requireNonNull; |
| 17 | + |
| 18 | +final class CommonOptions { |
| 19 | + private final CommandSpec spec; |
| 20 | + private final Path source; |
| 21 | + private final Path output; |
| 22 | + private final BiFunction<String, Integer, String> fileNameGenerator; |
| 23 | + |
| 24 | + |
| 25 | + CommonOptions(CommandSpec spec, Path source, Path output, BiFunction<String, Integer, String> fileNameGenerator) { |
| 26 | + this.spec = requireNonNull(spec); |
| 27 | + this.source = requireNonNull(source); |
| 28 | + this.output = output; |
| 29 | + this.fileNameGenerator = requireNonNull(fileNameGenerator); |
| 30 | + |
| 31 | + if (isSourceSystemIn()) { |
| 32 | + if (isDestinationDirectory()) { |
| 33 | + throw new CommandLine.ParameterException( |
| 34 | + spec.commandLine(), |
| 35 | + ("Invalid value '%s' for option '--output': When " + |
| 36 | + "reading from standard input, output can not " + |
| 37 | + "be a directory").formatted(output) |
| 38 | + ); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + private boolean isSourceSystemIn() { |
| 45 | + var fileName = source.getFileName(); |
| 46 | + return fileName != null && fileName.toString().equals("-"); |
| 47 | + } |
| 48 | + |
| 49 | + private boolean isDestinationDirectory() { |
| 50 | + return output != null && Files.isDirectory(output); |
| 51 | + } |
| 52 | + |
| 53 | + OutputStream outputPrintWriter() { |
| 54 | + if (output == null) { |
| 55 | + return System.out; |
| 56 | + } |
| 57 | + |
| 58 | + Path path = outputPath(); |
| 59 | + try { |
| 60 | + return newOutputStream(path, CREATE, TRUNCATE_EXISTING); |
| 61 | + } catch (IOException e) { |
| 62 | + throw new CommandLine.ParameterException( |
| 63 | + spec.commandLine(), |
| 64 | + ("Invalid value '%s' for option '--output': Could not " + |
| 65 | + "write to '%s'" |
| 66 | + ).formatted(output, path), e); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private Path outputPath() { |
| 71 | + if (!isDestinationDirectory()) { |
| 72 | + return output; |
| 73 | + } |
| 74 | + |
| 75 | + // Given a directory, decide on a file name |
| 76 | + var fileName = source.getFileName().toString(); |
| 77 | + var index = fileName.lastIndexOf("."); |
| 78 | + if (index >= 0) { |
| 79 | + fileName = fileName.substring(0, index); |
| 80 | + } |
| 81 | + var candidate = output.resolve(fileName + ".xml"); |
| 82 | + |
| 83 | + // Avoid overwriting existing files when we decided the file name. |
| 84 | + var counter = 1; |
| 85 | + while (Files.exists(candidate)) { |
| 86 | + candidate = output.resolve(fileNameGenerator.apply(fileName, counter++)); |
| 87 | + } |
| 88 | + return candidate; |
| 89 | + } |
| 90 | + |
| 91 | + InputStream sourceInputStream() { |
| 92 | + if (isSourceSystemIn()) { |
| 93 | + return System.in; |
| 94 | + } |
| 95 | + try { |
| 96 | + return Files.newInputStream(source); |
| 97 | + } catch (IOException e) { |
| 98 | + throw new CommandLine.ParameterException(spec.commandLine(), "Invalid argument, could not read '%s'".formatted(source), e); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments