27
27
28
28
import joptsimple .OptionSet ;
29
29
import joptsimple .OptionSpec ;
30
- import org .apache .http .impl .client .CloseableHttpClient ;
31
30
31
+ import org .apache .http .impl .client .CloseableHttpClient ;
32
32
import org .springframework .boot .cli .command .options .OptionHandler ;
33
33
import org .springframework .boot .cli .command .status .ExitStatus ;
34
34
import org .springframework .boot .cli .util .Log ;
@@ -78,46 +78,61 @@ public class InitCommandOptionHandler extends OptionHandler {
78
78
79
79
@ Override
80
80
protected void options () {
81
- this .target = option (Arrays .asList ("target" ),
82
- "URL of the service to use" ).withRequiredArg ().defaultsTo (ProjectGenerationRequest .DEFAULT_SERVICE_URL );
83
- this .listMetadata = option (Arrays .asList ("list" , "l" ), "List the capabilities of the service. Use it to " +
84
- "discover the dependencies and the types that are available." );
81
+ this .target = option (Arrays .asList ("target" ), "URL of the service to use" )
82
+ .withRequiredArg ().defaultsTo (
83
+ ProjectGenerationRequest .DEFAULT_SERVICE_URL );
84
+ this .listMetadata = option (Arrays .asList ("list" , "l" ),
85
+ "List the capabilities of the service. Use it to "
86
+ + "discover the dependencies and the types that are available." );
85
87
86
88
// Project generation settings
87
89
this .bootVersion = option (Arrays .asList ("boot-version" , "bv" ),
88
90
"Spring Boot version to use (e.g. 1.2.0.RELEASE)" ).withRequiredArg ();
89
91
this .dependencies = option (Arrays .asList ("dependencies" , "d" ),
90
- "Comma separated list of dependencies to include in the generated project" ).withRequiredArg ();
92
+ "Comma separated list of dependencies to include in the generated project" )
93
+ .withRequiredArg ();
91
94
this .javaVersion = option (Arrays .asList ("java-version" , "jv" ),
92
95
"Java version to use (e.g. 1.8)" ).withRequiredArg ();
93
- this .packaging = option (Arrays .asList ("packaging" , "p" ), "Packaging type to use (e.g. jar)" ).withRequiredArg ();
94
-
95
- this .build = option ("build" , "The build system to use (e.g. maven, gradle). To be used alongside " +
96
- "--format to uniquely identify one type that is supported by the service. " +
97
- "Use --type in case of conflict" ).withRequiredArg ().defaultsTo ("maven" );
98
- this .format = option ("format" , "The format of the generated content (e.g. build for a build file, " +
99
- "project for a project archive). To be used alongside --build to uniquely identify one type " +
100
- "that is supported by the service. Use --type in case of conflict" )
96
+ this .packaging = option (Arrays .asList ("packaging" , "p" ),
97
+ "Packaging type to use (e.g. jar)" ).withRequiredArg ();
98
+
99
+ this .build = option (
100
+ "build" ,
101
+ "The build system to use (e.g. maven, gradle). To be used alongside "
102
+ + "--format to uniquely identify one type that is supported by the service. "
103
+ + "Use --type in case of conflict" ).withRequiredArg ().defaultsTo (
104
+ "maven" );
105
+ this .format = option (
106
+ "format" ,
107
+ "The format of the generated content (e.g. build for a build file, "
108
+ + "project for a project archive). To be used alongside --build to uniquely identify one type "
109
+ + "that is supported by the service. Use --type in case of conflict" )
101
110
.withRequiredArg ().defaultsTo ("project" );
102
- this .type = option (Arrays .asList ("type" , "t" ), "The project type to use. Not normally needed if you " +
103
- "use --build and/or --format. Check the capabilities of the service (--list) for " +
104
- "more details." ).withRequiredArg ();
111
+ this .type = option (
112
+ Arrays .asList ("type" , "t" ),
113
+ "The project type to use. Not normally needed if you "
114
+ + "use --build and/or --format. Check the capabilities of the service (--list) for "
115
+ + "more details." ).withRequiredArg ();
105
116
106
117
// Others
107
- this .extract = option (Arrays .asList ("extract" , "x" ), "Extract the project archive" );
108
- this .force = option (Arrays .asList ("force" , "f" ), "Force overwrite of existing files" );
109
- this .output = option (Arrays .asList ("output" , "o" ),
110
- "Location of the generated project. Can be an absolute or a relative reference and " +
111
- "should refer to a directory when --extract is used." ).withRequiredArg ();
118
+ this .extract = option (Arrays .asList ("extract" , "x" ),
119
+ "Extract the project archive" );
120
+ this .force = option (Arrays .asList ("force" , "f" ),
121
+ "Force overwrite of existing files" );
122
+ this .output = option (
123
+ Arrays .asList ("output" , "o" ),
124
+ "Location of the generated project. Can be an absolute or a relative reference and "
125
+ + "should refer to a directory when --extract is used." )
126
+ .withRequiredArg ();
112
127
}
113
128
114
129
@ Override
115
130
protected ExitStatus run (OptionSet options ) throws Exception {
116
- if (options .has (listMetadata )) {
117
- return listServiceCapabilities (options , httpClient );
131
+ if (options .has (this . listMetadata )) {
132
+ return listServiceCapabilities (options , this . httpClient );
118
133
}
119
134
else {
120
- return generateProject (options , httpClient );
135
+ return generateProject (options , this . httpClient );
121
136
}
122
137
}
123
138
@@ -151,7 +166,8 @@ public ProjectGenerationRequest createProjectGenerationRequest(OptionSet options
151
166
return request ;
152
167
}
153
168
154
- protected ExitStatus listServiceCapabilities (OptionSet options , CloseableHttpClient httpClient ) throws IOException {
169
+ protected ExitStatus listServiceCapabilities (OptionSet options ,
170
+ CloseableHttpClient httpClient ) throws IOException {
155
171
ListMetadataCommand command = new ListMetadataCommand (httpClient );
156
172
Log .info (command .generateReport (determineServiceUrl (options )));
157
173
return ExitStatus .OK ;
@@ -161,19 +177,22 @@ protected ExitStatus generateProject(OptionSet options, CloseableHttpClient http
161
177
ProjectGenerationRequest request = createProjectGenerationRequest (options );
162
178
boolean forceValue = options .has (this .force );
163
179
try {
164
- ProjectGenerationResponse entity = new InitializrServiceHttpInvoker (httpClient ).generate (request );
180
+ ProjectGenerationResponse entity = new InitializrServiceHttpInvoker (
181
+ httpClient ).generate (request );
165
182
if (options .has (this .extract )) {
166
183
if (isZipArchive (entity )) {
167
- return extractProject (entity , options .valueOf (this .output ), forceValue );
184
+ return extractProject (entity , options .valueOf (this .output ),
185
+ forceValue );
168
186
}
169
187
else {
170
188
Log .info ("Could not extract '" + entity .getContentType () + "'" );
171
189
}
172
190
}
173
- String outputFileName = entity .getFileName () != null ? entity .getFileName () : options .valueOf (this .output );
191
+ String outputFileName = entity .getFileName () != null ? entity .getFileName ()
192
+ : options .valueOf (this .output );
174
193
if (outputFileName == null ) {
175
- Log .error ("Could not save the project, the server did not set a preferred " +
176
- "file name. Use --output to specify the output location for the project." );
194
+ Log .error ("Could not save the project, the server did not set a preferred "
195
+ + "file name. Use --output to specify the output location for the project." );
177
196
return ExitStatus .ERROR ;
178
197
}
179
198
return writeProject (entity , outputFileName , forceValue );
@@ -192,8 +211,8 @@ private String determineServiceUrl(OptionSet options) {
192
211
return options .valueOf (this .target );
193
212
}
194
213
195
- private ExitStatus writeProject (ProjectGenerationResponse entity , String outputFileName , boolean overwrite )
196
- throws IOException {
214
+ private ExitStatus writeProject (ProjectGenerationResponse entity ,
215
+ String outputFileName , boolean overwrite ) throws IOException {
197
216
198
217
File f = new File (outputFileName );
199
218
if (f .exists ()) {
@@ -204,8 +223,9 @@ private ExitStatus writeProject(ProjectGenerationResponse entity, String outputF
204
223
}
205
224
}
206
225
else {
207
- Log .error ("File '" + f .getName () + "' already exists. Use --force if you want to " +
208
- "overwrite or --output to specify an alternate location." );
226
+ Log .error ("File '" + f .getName ()
227
+ + "' already exists. Use --force if you want to "
228
+ + "overwrite or --output to specify an alternate location." );
209
229
return ExitStatus .ERROR ;
210
230
}
211
231
}
@@ -232,21 +252,26 @@ private boolean isZipArchive(ProjectGenerationResponse entity) {
232
252
}
233
253
}
234
254
235
- private ExitStatus extractProject (ProjectGenerationResponse entity , String outputValue , boolean overwrite ) throws IOException {
236
- File output = outputValue != null ? new File (outputValue ) : new File (System .getProperty ("user.dir" ));
255
+ private ExitStatus extractProject (ProjectGenerationResponse entity ,
256
+ String outputValue , boolean overwrite ) throws IOException {
257
+ File output = outputValue != null ? new File (outputValue ) : new File (
258
+ System .getProperty ("user.dir" ));
237
259
if (!output .exists ()) {
238
260
output .mkdirs ();
239
261
}
240
- ZipInputStream zipIn = new ZipInputStream (new ByteArrayInputStream (entity .getContent ()));
262
+ ZipInputStream zipIn = new ZipInputStream (new ByteArrayInputStream (
263
+ entity .getContent ()));
241
264
try {
242
265
ZipEntry entry = zipIn .getNextEntry ();
243
266
while (entry != null ) {
244
267
File f = new File (output , entry .getName ());
245
268
if (f .exists () && !overwrite ) {
246
269
StringBuilder sb = new StringBuilder ();
247
270
sb .append (f .isDirectory () ? "Directory" : "File" )
248
- .append (" '" ).append (f .getName ()).append ("' already exists. Use --force if you want to " +
249
- "overwrite or --output to specify an alternate location." );
271
+ .append (" '" )
272
+ .append (f .getName ())
273
+ .append ("' already exists. Use --force if you want to "
274
+ + "overwrite or --output to specify an alternate location." );
250
275
Log .error (sb .toString ());
251
276
return ExitStatus .ERROR ;
252
277
}
@@ -268,7 +293,8 @@ private ExitStatus extractProject(ProjectGenerationResponse entity, String outpu
268
293
}
269
294
270
295
private void extractZipEntry (ZipInputStream in , File outputFile ) throws IOException {
271
- BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (outputFile ));
296
+ BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (
297
+ outputFile ));
272
298
try {
273
299
StreamUtils .copy (in , out );
274
300
}
0 commit comments