|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package com.wordnik.swagger.codegen.cmd; |
| 7 | + |
| 8 | +import com.wordnik.swagger.codegen.CliOption; |
| 9 | +import com.wordnik.swagger.codegen.CodegenConfig; |
| 10 | +import io.airlift.airline.Command; |
| 11 | +import io.airlift.airline.Option; |
| 12 | +import java.util.ServiceLoader; |
| 13 | +import static java.util.ServiceLoader.load; |
| 14 | + |
| 15 | +@Command(name = "config-help", description = "Config help for chosen lang") |
| 16 | +public class ConfigHelp implements Runnable { |
| 17 | + |
| 18 | + @Option(name = {"-l", "--lang"}, title = "language", required = true, |
| 19 | + description = "language to get config help for") |
| 20 | + private String lang; |
| 21 | + |
| 22 | + @Override |
| 23 | + public void run() { |
| 24 | + System.out.println(); |
| 25 | + CodegenConfig config = forName(lang); |
| 26 | + System.out.println("CONFIG OPTIONS"); |
| 27 | + for (CliOption langCliOption : config.cliOptions()) { |
| 28 | + System.out.println("\t" + langCliOption.getOpt()); |
| 29 | + System.out.println("\t " + langCliOption.getDescription()); |
| 30 | + System.out.println(); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Tries to load config class with SPI first, then with class name directly from classpath |
| 36 | + * @param name name of config, or full qualified class name in classpath |
| 37 | + * @return config class |
| 38 | + */ |
| 39 | + private static CodegenConfig forName(String name) { |
| 40 | + ServiceLoader<CodegenConfig> loader = load(CodegenConfig.class); |
| 41 | + for (CodegenConfig config : loader) { |
| 42 | + if (config.getName().equals(name)) { |
| 43 | + return config; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // else try to load directly |
| 48 | + try { |
| 49 | + return (CodegenConfig) Class.forName(name).newInstance(); |
| 50 | + } catch (Exception e) { |
| 51 | + throw new RuntimeException("Can't load config class with name ".concat(name), e); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
0 commit comments