Skip to content

Commit ba7025d

Browse files
hrachyahrachya
authored andcommitted
Adding config-help command
1 parent 1f36096 commit ba7025d

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/SwaggerCodegen.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.wordnik.swagger.codegen;
22

3+
import com.wordnik.swagger.codegen.cmd.ConfigHelp;
34
import com.wordnik.swagger.codegen.cmd.Generate;
45
import com.wordnik.swagger.codegen.cmd.Langs;
56
import com.wordnik.swagger.codegen.cmd.Meta;
@@ -27,7 +28,8 @@ public static void main(String[] args) {
2728
Generate.class,
2829
Meta.class,
2930
Langs.class,
30-
Help.class
31+
Help.class,
32+
ConfigHelp.class
3133
);
3234

3335
builder.build().parse(args).run();
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}

modules/swagger-codegen-cli/src/main/java/com/wordnik/swagger/codegen/cmd/Generate.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public class Generate implements Runnable {
6161
"the format of name=value,name=value")
6262
private String systemProperties;
6363

64-
@Option( name= {"-c", "--config"}, title = "configuration file", description = "path to json configuration file")
64+
@Option( name= {"-c", "--config"}, title = "configuration file", description = "Path to json configuration file. " +
65+
"File content should be in a json format {\"optionKey\":\"optionValue\", \"optionKey1\":\"optionValue1\"...} " +
66+
"Supported options can be different for each language. Run config-help -l {lang} command for language specific config options.")
6567
private String configFile;
6668

6769
@Override

0 commit comments

Comments
 (0)