Search in sources :

Example 1 with BenderSchema

use of com.nextdoor.bender.config.BenderConfig.BenderSchema in project bender by Nextdoor.

the class CreateSchema method main.

public static void main(String[] args) throws ParseException, InterruptedException, IOException {
    /*
     * Parse cli arguments
     */
    Options options = new Options();
    options.addOption(Option.builder().longOpt("out-file").hasArg().desc("Filename to output schema to. Default: schema.json").build());
    options.addOption(Option.builder().longOpt("docson").hasArg(false).desc("Create a schema that is able to be read by docson").build());
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(options, args);
    String filename = cmd.getOptionValue("out-file", "schema.json");
    /*
     * Write schema
     */
    BenderSchema schema = new BenderSchema();
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    JsonNode node = schema.getSchema();
    if (cmd.hasOption("docson")) {
        modifyNode(node);
    }
    mapper.writeValue(new File(filename), node);
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) BenderSchema(com.nextdoor.bender.config.BenderConfig.BenderSchema) JsonNode(com.fasterxml.jackson.databind.JsonNode) CommandLineParser(org.apache.commons.cli.CommandLineParser) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 2 with BenderSchema

use of com.nextdoor.bender.config.BenderConfig.BenderSchema in project bender by Nextdoor.

the class ValidateSchema method main.

public static void main(String[] args) throws ParseException, InterruptedException, IOException {
    /*
     * Parse cli arguments
     */
    Options options = new Options();
    options.addOption(Option.builder().longOpt("schema").hasArg().desc("Filename to output schema to. Default: schema.json").build());
    options.addOption(Option.builder().longOpt("configs").hasArgs().desc("List of config files to validate against schema.").build());
    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(options, args);
    String schemaFilename = cmd.getOptionValue("schema", "schema.json");
    String[] configFilenames = cmd.getOptionValues("configs");
    /*
     * Validate config files against schema
     */
    BenderSchema schema = new BenderSchema(new File(schemaFilename));
    boolean hasFailures = false;
    for (String configFilename : configFilenames) {
        StringBuilder sb = new StringBuilder();
        Files.lines(Paths.get(configFilename), StandardCharsets.UTF_8).forEach(p -> sb.append(p + "\n"));
        System.out.println("Attempting to validate " + configFilename);
        try {
            ObjectMapper mapper = BenderConfig.getObjectMapper(configFilename);
            BenderConfig.validate(sb.toString(), mapper, schema);
            System.out.println("Valid");
        } catch (ConfigurationException e) {
            System.out.println("Invalid");
            e.printStackTrace();
        }
    }
    if (hasFailures) {
        System.exit(1);
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) BenderSchema(com.nextdoor.bender.config.BenderConfig.BenderSchema) ConfigurationException(com.nextdoor.bender.config.ConfigurationException) CommandLineParser(org.apache.commons.cli.CommandLineParser) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 3 with BenderSchema

use of com.nextdoor.bender.config.BenderConfig.BenderSchema in project bender by Nextdoor.

the class WriteSchema method main.

public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
    String filename = args[0];
    new File(filename).getParentFile().mkdirs();
    BenderSchema schema = new BenderSchema();
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    JsonNode node = schema.getSchema();
    mapper.writeValue(new File(filename), node);
}
Also used : BenderSchema(com.nextdoor.bender.config.BenderConfig.BenderSchema) JsonNode(com.fasterxml.jackson.databind.JsonNode) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 BenderSchema (com.nextdoor.bender.config.BenderConfig.BenderSchema)3 File (java.io.File)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 CommandLine (org.apache.commons.cli.CommandLine)2 CommandLineParser (org.apache.commons.cli.CommandLineParser)2 DefaultParser (org.apache.commons.cli.DefaultParser)2 Options (org.apache.commons.cli.Options)2 ConfigurationException (com.nextdoor.bender.config.ConfigurationException)1