Skip to content

Commit

Permalink
Argument validation in Groovy compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
LesTR committed Feb 15, 2019
1 parent 0305dec commit d531a8a
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions tools/src/main/java/cz/o2/proxima/tools/groovy/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
Expand All @@ -36,6 +38,7 @@
/**
* A compiler of conf files to groovy object.
*/
@Slf4j
public class Compiler {

private final Configuration conf = new Configuration(Configuration.VERSION_2_3_23);
Expand All @@ -56,7 +59,11 @@ private Compiler(String[] args) throws ParseException {
if (!parsed.hasOption("o")) {
throw new IllegalStateException("Missing config option 'o' for output");
}
output = parsed.getOptionValue("o");
output = parsed.getOptionValue("o").trim();
if (output.length() == 0) {
throw new IllegalArgumentException("Empty option 'o' value for output.");
}
log.debug("Configured output file: {}", output);
configs = parsed.getArgList();

conf.setDefaultEncoding(StandardCharsets.UTF_8.name());
Expand All @@ -67,7 +74,14 @@ private Compiler(String[] args) throws ParseException {

public void run() throws Exception {
Config config = configs.stream()
.map(f -> ConfigFactory.parseFile(new File(f)))
.map(f -> {
File c = new File(f);
if (!c.exists()) {
throw new IllegalArgumentException(
"Unable to find config file " + f + ". Check your configuration.");
}
return ConfigFactory.parseFile(new File(f));
})
.reduce(
ConfigFactory.empty(),
(l, r) -> l.withFallback(r))
Expand Down

0 comments on commit d531a8a

Please sign in to comment.