|
48 | 48 | import java.nio.charset.Charset; |
49 | 49 | import java.util.List; |
50 | 50 | import java.util.Locale; |
51 | | -import java.util.UUID; |
52 | | -import java.util.logging.Level; |
53 | | -import java.util.logging.Logger; |
| 51 | +import jenkins.cli.listeners.CLIContext; |
| 52 | +import jenkins.cli.listeners.CLIListener; |
54 | 53 | import jenkins.model.Jenkins; |
| 54 | +import jenkins.util.Listeners; |
55 | 55 | import jenkins.util.SystemProperties; |
56 | 56 | import org.jvnet.hudson.annotation_indexer.Index; |
57 | 57 | import org.jvnet.tiger_types.Types; |
@@ -242,70 +242,73 @@ public int main(List<String> args, Locale locale, InputStream stdin, PrintStream |
242 | 242 | this.locale = locale; |
243 | 243 | CmdLineParser p = getCmdLineParser(); |
244 | 244 |
|
| 245 | + Authentication auth = getTransportAuthentication2(); |
| 246 | + CLIContext context = new CLIContext(getName(), args, auth); |
| 247 | + |
245 | 248 | // add options from the authenticator |
246 | 249 | SecurityContext sc = null; |
247 | 250 | Authentication old = null; |
248 | | - Authentication auth; |
249 | 251 | try { |
250 | 252 | // TODO as in CLIRegisterer this may be doing too much work |
251 | 253 | sc = SecurityContextHolder.getContext(); |
252 | 254 | old = sc.getAuthentication(); |
253 | 255 |
|
254 | | - sc.setAuthentication(auth = getTransportAuthentication2()); |
| 256 | + sc.setAuthentication(auth); |
255 | 257 |
|
256 | 258 | if (!(this instanceof HelpCommand || this instanceof WhoAmICommand)) |
257 | 259 | Jenkins.get().checkPermission(Jenkins.READ); |
258 | 260 | p.parseArgument(args.toArray(new String[0])); |
259 | | - LOGGER.log(Level.FINE, "Invoking CLI command {0}, with {1} arguments, as user {2}.", |
260 | | - new Object[] {getName(), args.size(), auth.getName()}); |
| 261 | + |
| 262 | + Listeners.notify(CLIListener.class, true, listener -> listener.onExecution(context)); |
261 | 263 | int res = run(); |
262 | | - LOGGER.log(Level.FINE, "Executed CLI command {0}, with {1} arguments, as user {2}, return code {3}", |
263 | | - new Object[] {getName(), args.size(), auth.getName(), res}); |
| 264 | + Listeners.notify(CLIListener.class, true, listener -> listener.onCompleted(context, res)); |
| 265 | + |
264 | 266 | return res; |
265 | | - } catch (CmdLineException e) { |
266 | | - logFailedCommandAndPrintExceptionErrorMessage(args, e); |
267 | | - printUsage(stderr, p); |
268 | | - return 2; |
269 | | - } catch (IllegalStateException e) { |
270 | | - logFailedCommandAndPrintExceptionErrorMessage(args, e); |
271 | | - return 4; |
272 | | - } catch (IllegalArgumentException e) { |
273 | | - logFailedCommandAndPrintExceptionErrorMessage(args, e); |
274 | | - return 3; |
275 | | - } catch (AbortException e) { |
276 | | - logFailedCommandAndPrintExceptionErrorMessage(args, e); |
277 | | - return 5; |
278 | | - } catch (AccessDeniedException e) { |
279 | | - logFailedCommandAndPrintExceptionErrorMessage(args, e); |
280 | | - return 6; |
281 | | - } catch (BadCredentialsException e) { |
282 | | - // to the caller, we can't reveal whether the user didn't exist or the password didn't match. |
283 | | - // do that to the server log instead |
284 | | - String id = UUID.randomUUID().toString(); |
285 | | - logAndPrintError(e, "Bad Credentials. Search the server log for " + id + " for more details.", |
286 | | - "CLI login attempt failed: " + id, Level.INFO); |
287 | | - return 7; |
288 | 267 | } catch (Throwable e) { |
289 | | - String errorMsg = "Unexpected exception occurred while performing " + getName() + " command."; |
290 | | - logAndPrintError(e, errorMsg, errorMsg, Level.WARNING); |
291 | | - Functions.printStackTrace(e, stderr); |
292 | | - return 1; |
| 268 | + int exitCode = handleException(e, context, p); |
| 269 | + Listeners.notify(CLIListener.class, true, listener -> listener.onThrowable(context, e)); |
| 270 | + return exitCode; |
293 | 271 | } finally { |
294 | 272 | if (sc != null) |
295 | 273 | sc.setAuthentication(old); // restore |
296 | 274 | } |
297 | 275 | } |
298 | 276 |
|
299 | | - private void logFailedCommandAndPrintExceptionErrorMessage(List<String> args, Throwable e) { |
300 | | - Authentication auth = getTransportAuthentication2(); |
301 | | - String logMessage = String.format("Failed call to CLI command %s, with %d arguments, as user %s.", |
302 | | - getName(), args.size(), auth != null ? auth.getName() : "<unknown>"); |
303 | | - |
304 | | - logAndPrintError(e, e.getMessage(), logMessage, Level.FINE); |
| 277 | + /** |
| 278 | + * Determines command stderr output and return the exit code as described on {@link #main(List, Locale, InputStream, PrintStream, PrintStream)} |
| 279 | + * */ |
| 280 | + protected int handleException(Throwable e, CLIContext context, CmdLineParser p) { |
| 281 | + int exitCode; |
| 282 | + if (e instanceof CmdLineException) { |
| 283 | + exitCode = 2; |
| 284 | + printError(e.getMessage()); |
| 285 | + printUsage(stderr, p); |
| 286 | + } else if (e instanceof IllegalArgumentException) { |
| 287 | + exitCode = 3; |
| 288 | + printError(e.getMessage()); |
| 289 | + } else if (e instanceof IllegalStateException) { |
| 290 | + exitCode = 4; |
| 291 | + printError(e.getMessage()); |
| 292 | + } else if (e instanceof AbortException) { |
| 293 | + exitCode = 5; |
| 294 | + printError(e.getMessage()); |
| 295 | + } else if (e instanceof AccessDeniedException) { |
| 296 | + exitCode = 6; |
| 297 | + printError(e.getMessage()); |
| 298 | + } else if (e instanceof BadCredentialsException) { |
| 299 | + exitCode = 7; |
| 300 | + printError( |
| 301 | + "Bad Credentials. Search the server log for " + context.getCorrelationId() + " for more details."); |
| 302 | + } else { |
| 303 | + exitCode = 1; |
| 304 | + printError("Unexpected exception occurred while performing " + getName() + " command."); |
| 305 | + Functions.printStackTrace(e, stderr); |
| 306 | + } |
| 307 | + return exitCode; |
305 | 308 | } |
306 | 309 |
|
307 | | - private void logAndPrintError(Throwable e, String errorMessage, String logMessage, Level logLevel) { |
308 | | - LOGGER.log(logLevel, logMessage, e); |
| 310 | + |
| 311 | + private void printError(String errorMessage) { |
309 | 312 | this.stderr.println(); |
310 | 313 | this.stderr.println("ERROR: " + errorMessage); |
311 | 314 | } |
@@ -541,8 +544,6 @@ public static CLICommand clone(String name) { |
541 | 544 | return null; |
542 | 545 | } |
543 | 546 |
|
544 | | - private static final Logger LOGGER = Logger.getLogger(CLICommand.class.getName()); |
545 | | - |
546 | 547 | private static final ThreadLocal<CLICommand> CURRENT_COMMAND = new ThreadLocal<>(); |
547 | 548 |
|
548 | 549 | /*package*/ static CLICommand setCurrent(CLICommand cmd) { |
|
0 commit comments