Adding config-help command

This commit is contained in:
hrachya 2015-05-28 15:02:32 -07:00
parent 1f360961e0
commit ba7025d54b
3 changed files with 60 additions and 2 deletions

View File

@ -1,5 +1,6 @@
package com.wordnik.swagger.codegen;
import com.wordnik.swagger.codegen.cmd.ConfigHelp;
import com.wordnik.swagger.codegen.cmd.Generate;
import com.wordnik.swagger.codegen.cmd.Langs;
import com.wordnik.swagger.codegen.cmd.Meta;
@ -27,7 +28,8 @@ public class SwaggerCodegen {
Generate.class,
Meta.class,
Langs.class,
Help.class
Help.class,
ConfigHelp.class
);
builder.build().parse(args).run();

View File

@ -0,0 +1,54 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.wordnik.swagger.codegen.cmd;
import com.wordnik.swagger.codegen.CliOption;
import com.wordnik.swagger.codegen.CodegenConfig;
import io.airlift.airline.Command;
import io.airlift.airline.Option;
import java.util.ServiceLoader;
import static java.util.ServiceLoader.load;
@Command(name = "config-help", description = "Config help for chosen lang")
public class ConfigHelp implements Runnable {
@Option(name = {"-l", "--lang"}, title = "language", required = true,
description = "language to get config help for")
private String lang;
@Override
public void run() {
System.out.println();
CodegenConfig config = forName(lang);
System.out.println("CONFIG OPTIONS");
for (CliOption langCliOption : config.cliOptions()) {
System.out.println("\t" + langCliOption.getOpt());
System.out.println("\t " + langCliOption.getDescription());
System.out.println();
}
}
/**
* Tries to load config class with SPI first, then with class name directly from classpath
* @param name name of config, or full qualified class name in classpath
* @return config class
*/
private static CodegenConfig forName(String name) {
ServiceLoader<CodegenConfig> loader = load(CodegenConfig.class);
for (CodegenConfig config : loader) {
if (config.getName().equals(name)) {
return config;
}
}
// else try to load directly
try {
return (CodegenConfig) Class.forName(name).newInstance();
} catch (Exception e) {
throw new RuntimeException("Can't load config class with name ".concat(name), e);
}
}
}

View File

@ -61,7 +61,9 @@ public class Generate implements Runnable {
"the format of name=value,name=value")
private String systemProperties;
@Option( name= {"-c", "--config"}, title = "configuration file", description = "path to json configuration file")
@Option( name= {"-c", "--config"}, title = "configuration file", description = "Path to json configuration file. " +
"File content should be in a json format {\"optionKey\":\"optionValue\", \"optionKey1\":\"optionValue1\"...} " +
"Supported options can be different for each language. Run config-help -l {lang} command for language specific config options.")
private String configFile;
@Override