|
| 1 | +package com.github.slamdev.openapispringgenerator.cli; |
| 2 | + |
| 3 | +import com.github.slamdev.openapispringgenerator.lib.generator.OpenAPIYamlCodegen; |
| 4 | +import io.swagger.codegen.v3.ClientOptInput; |
| 5 | +import io.swagger.codegen.v3.DefaultGenerator; |
| 6 | +import io.swagger.codegen.v3.config.CodegenConfigurator; |
| 7 | +import io.swagger.codegen.v3.generators.openapi.OpenAPIGenerator; |
| 8 | +import picocli.CommandLine; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | + |
| 14 | +@CommandLine.Command(name = "convert", mixinStandardHelpOptions = true, description = "convert to OpenAPI v3") |
| 15 | +public class Convert implements Runnable { |
| 16 | + |
| 17 | + @CommandLine.Option(names = {"-f", "--input-file"}, required = true, description = "path to a spec file") |
| 18 | + private Path specFile; |
| 19 | + |
| 20 | + @CommandLine.Option(names = {"-o", "--output-file"}, required = true, description = "path to store the generated file") |
| 21 | + private Path outputFile; |
| 22 | + |
| 23 | + @Override |
| 24 | + public void run() { |
| 25 | + Path abs = outputFile.toAbsolutePath(); |
| 26 | + CodegenConfigurator configurator = new CodegenConfigurator(); |
| 27 | + configurator.setLang(OpenAPIYamlCodegen.class.getName()); |
| 28 | + configurator.setOutputDir(abs.getParent().toString()); |
| 29 | + configurator.addAdditionalProperty(OpenAPIGenerator.OUTPUT_NAME, abs.getFileName().toString()); |
| 30 | + configurator.addAdditionalProperty(OpenAPIGenerator.FLATTEN_SPEC, true); |
| 31 | + try { |
| 32 | + configurator.setInputSpec(new String(Files.readAllBytes(specFile))); |
| 33 | + } catch (IOException e) { |
| 34 | + throw new IllegalStateException(e); |
| 35 | + } |
| 36 | + ClientOptInput input = configurator.toClientOptInput(); |
| 37 | + DefaultGenerator generator = new DefaultGenerator(); |
| 38 | + generator.opts(input); |
| 39 | + generator.setGenerateSwaggerMetadata(false); |
| 40 | + generator.generate(); |
| 41 | + } |
| 42 | +} |
0 commit comments