blob: df199c00eda7b6a22039681306f7a100c6da1d9e (
plain) (
blame)
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.security.tool;
import org.apache.commons.cli.CommandLine;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* @author vekterli
*/
public class CliUtils {
public static String optionOrThrow(CommandLine arguments, String option) {
var value = arguments.getOptionValue(option);
if (value == null) {
throw new IllegalArgumentException("Required argument '--%s' must be provided".formatted(option));
}
return value;
}
public static InputStream inputStreamFromFileOrStream(String pathOrDash, InputStream stdIn) throws IOException {
if ("-".equals(pathOrDash)) {
return stdIn;
} else {
var inputPath = Paths.get(pathOrDash);
if (!inputPath.toFile().exists()) {
throw new IllegalArgumentException("Input file '%s' does not exist".formatted(inputPath.toString()));
}
return Files.newInputStream(inputPath);
}
}
public static OutputStream outputStreamToFileOrStream(String pathOrDash, OutputStream stdOut) throws IOException {
if ("-".equals(pathOrDash)) {
return stdOut;
} else {
// TODO fail if file already exists?
return Files.newOutputStream(Paths.get(pathOrDash));
}
}
}
|