Skip to content

Commit a6c5b18

Browse files
committed
refactor command line related code
1 parent 72f2f2e commit a6c5b18

File tree

7 files changed

+212
-44
lines changed

7 files changed

+212
-44
lines changed
Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
1+
/**
2+
* Copyright (C) 2022 Oleg Aleksandrov
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
* <p>
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* <p>
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
113
package io.squark.swaggercombine
214

3-
enum class CommandLineArguments {
15+
/**
16+
* Represents a command line property.
17+
*
18+
* @author Oleg Aleksandrov
19+
* @since 0.2.0
20+
*/
21+
enum class CommandLineArguments(
22+
/**
23+
* Contains the current value for this command line property.
24+
*/
25+
val value: String
26+
) {
27+
/**
28+
* The command line key to the path to output file.
29+
*/
30+
OUTPUT_FILE("--outputFile"),
31+
32+
/**
33+
* The command line key to the strip base path property.
34+
*/
35+
STRIP_BASE_PATH("--stripBasePath"),
36+
/**
37+
* The command line key to the path to input file.
38+
*/
39+
INPUT_FILE("--inputFile"),
440
}

src/main/java/io/squark/swaggercombine/CommandLineProperties.kt

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright (C) 2022 Oleg Aleksandrov
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
* <p>
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* <p>
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package io.squark.swaggercombine
14+
15+
/**
16+
* Utility class for parse command line arguments.
17+
*
18+
* @author Oleg Aleksandrov
19+
* @since 0.2.0
20+
*/
21+
class CommandLinePropertiesParser(args: Array<String>) {
22+
private val properties: Map<CommandLineArguments, List<String>> = parseCommandLineProperties(args)
23+
24+
/**
25+
* Returns list of values associated to given command line argument.
26+
* @since 0.2.0
27+
*/
28+
fun get(arg: CommandLineArguments): List<String> {
29+
return properties[arg] ?: emptyList()
30+
}
31+
32+
private fun parseCommandLineProperties(args: Array<String>): Map<CommandLineArguments, List<String>> {
33+
val properties = mutableMapOf<CommandLineArguments, MutableList<String>>()
34+
35+
for (i in args.indices) {
36+
val arg = args[i]
37+
if (arg.startsWith("--")) {
38+
val key: CommandLineArguments = parseCommandLineKey(arg)
39+
?: throw RuntimeException("Unknown command line argument: $arg")
40+
41+
val value = args[i + 1]
42+
43+
val arguments = properties.getOrDefault(key, mutableListOf())
44+
arguments.add(value)
45+
properties[key] = arguments
46+
}
47+
}
48+
49+
return properties
50+
}
51+
52+
private fun parseCommandLineKey(key: String): CommandLineArguments? {
53+
return CommandLineArguments.values().firstOrNull { key == it.value }
54+
}
55+
}

src/main/java/io/squark/swaggercombine/Swagger.kt

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/main/java/io/squark/swaggercombine/SwaggerCombine.java

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
/**
22
* Copyright (C) 2007 Erik Håkansson
33
* Copyright (C) 2022 Oleg Aleksandrov
4-
*
5-
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6-
* in compliance with the License. You may obtain a copy of the License at
7-
*
4+
* <p>
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
* <p>
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software distributed under the License
11-
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12-
* or implied. See the License for the specific language governing permissions and limitations under
13-
* the License.
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
11+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
1413
*/
1514
package io.squark.swaggercombine;
1615

@@ -19,7 +18,6 @@
1918
import java.util.ArrayList;
2019
import java.util.List;
2120
import java.util.Map;
22-
import java.util.Properties;
2321

2422
import org.apache.commons.io.IOUtils;
2523

@@ -50,12 +48,12 @@ public class SwaggerCombine {
5048

5149
private Swagger firstSwagger;
5250
private List<Swagger> swaggers = new ArrayList<>();
53-
private Properties properties;
51+
private final CommandLinePropertiesParser properties;
5452

55-
public SwaggerCombine(List<String> files, Properties properties) throws Exception {
53+
public SwaggerCombine(CommandLinePropertiesParser properties) throws Exception {
5654
this.properties = properties;
5755
boolean first = true;
58-
for (String fileName : files) {
56+
for (String fileName : properties.get(CommandLineArguments.INPUT_FILE)) {
5957
File file = new File(fileName);
6058
if (!file.exists()) {
6159
throw new Exception("File not found: " + file.getAbsolutePath());
@@ -72,9 +70,14 @@ public SwaggerCombine(List<String> files, Properties properties) throws Exceptio
7270
}
7371

7472
public Swagger combine() {
73+
String useStripBasePath =
74+
properties.get(CommandLineArguments.STRIP_BASE_PATH)
75+
.stream()
76+
.findFirst()
77+
.orElse("false");
78+
79+
boolean stripBasePath = Boolean.parseBoolean(useStripBasePath);
7580

76-
boolean stripBasePath = (properties != null &&
77-
Boolean.parseBoolean(properties.getProperty("--stripBasePath", "false")));
7881
for (Swagger swagger : swaggers) {
7982
if (swagger.getTags() != null) {
8083
for (Tag tag : swagger.getTags()) {
@@ -148,22 +151,15 @@ public Swagger combine() {
148151
}
149152

150153
public static void main(String[] args) throws Exception {
151-
if (args.length < 2) {
154+
CommandLinePropertiesParser properties = new CommandLinePropertiesParser(args);
155+
156+
if (properties.get(CommandLineArguments.INPUT_FILE).size() < 2) {
152157
System.out.println("usage: swagger-combine base.json swagger2.json swagger3.json");
153158
System.out.println("Please use at least two input files");
154159
System.exit(1);
155160
}
156-
List<String> arguments = new ArrayList<>();
157-
Properties properties = new Properties();
158-
for (String arg : args) {
159-
if (arg.startsWith("--")) {
160-
String[] split = arg.split("=");
161-
properties.setProperty(split[0], split[1]);
162-
} else {
163-
arguments.add(arg);
164-
}
165-
}
166-
SwaggerCombine swaggerCombine = new SwaggerCombine(arguments, properties);
161+
162+
SwaggerCombine swaggerCombine = new SwaggerCombine(properties);
167163
Swagger combined = swaggerCombine.combine();
168164

169165
ObjectMapper mapper = Json.mapper();
@@ -175,10 +171,12 @@ public static void main(String[] args) throws Exception {
175171
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
176172
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
177173

178-
String outputFile = properties.getProperty("--outputFile");
179-
if (outputFile == null) {
180-
outputFile = "result.json";
181-
}
174+
String outputFile =
175+
properties.get(CommandLineArguments.OUTPUT_FILE)
176+
.stream()
177+
.findFirst()
178+
.orElse("result.json");
179+
182180
File output = new File(outputFile);
183181
mapper.writer(new DefaultPrettyPrinter()).writeValue(output, combined);
184182

src/main/java/io/squark/swaggercombine/SwaggerSpecificationParser.kt

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Copyright (C) 2022 Oleg Aleksandrov
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
* <p>
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* <p>
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package io.squark.swaggercombine
14+
15+
import org.junit.Assert
16+
import org.junit.Test
17+
18+
/**
19+
* Tests for [CommandLinePropertiesParser]
20+
*
21+
* @author Oleg Aleksandrov
22+
* @since 0.2.0
23+
*/
24+
class CommandLinePropertiesParserTest {
25+
26+
@Test
27+
fun `Inputs set up correctly`() {
28+
val inputFileName1 = "test1.json"
29+
val inputFileName2 = "test2.json"
30+
31+
val parser = CommandLinePropertiesParser(
32+
arrayOf(
33+
CommandLineArguments.INPUT_FILE.value,
34+
inputFileName1,
35+
CommandLineArguments.INPUT_FILE.value,
36+
inputFileName2,
37+
)
38+
)
39+
40+
val inputs = parser.get(CommandLineArguments.INPUT_FILE)
41+
Assert.assertTrue("Inputs should contains $inputFileName1", inputs.contains(inputFileName1))
42+
Assert.assertTrue("Inputs should contains $inputFileName2", inputs.contains(inputFileName2))
43+
}
44+
45+
@Test
46+
fun `Output set up correctly`() {
47+
val outputFileName = "output.json"
48+
val parser = CommandLinePropertiesParser(
49+
arrayOf(
50+
CommandLineArguments.OUTPUT_FILE.value,
51+
outputFileName,
52+
)
53+
)
54+
55+
val outputs = parser.get(CommandLineArguments.OUTPUT_FILE)
56+
Assert.assertTrue("Outputs should contains $outputFileName", outputs.contains(outputFileName))
57+
}
58+
59+
@Test
60+
fun `Strip base path set up correctly`() {
61+
val stripBasePath = true
62+
val parser = CommandLinePropertiesParser(
63+
arrayOf(
64+
CommandLineArguments.STRIP_BASE_PATH.value,
65+
stripBasePath.toString()
66+
)
67+
)
68+
69+
val stripBasePaths = parser.get(CommandLineArguments.STRIP_BASE_PATH)
70+
Assert.assertTrue("Strip base paths should contains $stripBasePath", stripBasePaths.contains(stripBasePath.toString()))
71+
}
72+
73+
@Test
74+
fun `Default properties value`() {
75+
val parser = CommandLinePropertiesParser(arrayOf())
76+
77+
Assert.assertTrue("Inputs should be empty", parser.get(CommandLineArguments.INPUT_FILE).isEmpty())
78+
Assert.assertTrue("Output file name should be empty", parser.get(CommandLineArguments.OUTPUT_FILE).isEmpty())
79+
Assert.assertTrue("Strip base path should be empty", parser.get(CommandLineArguments.STRIP_BASE_PATH).isEmpty())
80+
}
81+
82+
@Test(expected = RuntimeException::class)
83+
fun `Wrong argument`() {
84+
CommandLinePropertiesParser(
85+
arrayOf(
86+
"--wrong argument",
87+
"wrong argument",
88+
)
89+
)
90+
}
91+
}

0 commit comments

Comments
 (0)