in core/src/main/java/hudson/cli/declarative/CLIRegisterer.java [196:270]
public int main(List<String> args, Locale locale, InputStream stdin, PrintStream stdout, PrintStream stderr) {
this.stdout = stdout;
this.stderr = stderr;
this.locale = locale;
List<MethodBinder> binders = new ArrayList<>();
CmdLineParser parser = bindMethod(binders);
try {
SecurityContext sc = SecurityContextHolder.getContext();
Authentication old = sc.getAuthentication();
try {
// fill up all the binders
parser.parseArgument(args);
Authentication auth = getTransportAuthentication();
sc.setAuthentication(auth); // run the CLI with the right credential
jenkins.checkPermission(Jenkins.READ);
// resolve them
Object instance = null;
for (MethodBinder binder : binders)
instance = binder.call(instance);
if (instance instanceof Integer)
return (Integer) instance;
else
return 0;
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
if (t instanceof Exception)
throw (Exception) t;
throw e;
} finally {
sc.setAuthentication(old); // restore
}
} catch (CmdLineException e) {
stderr.println();
stderr.println("ERROR: " + e.getMessage());
printUsage(stderr, parser);
return 2;
} catch (IllegalStateException e) {
stderr.println();
stderr.println("ERROR: " + e.getMessage());
return 4;
} catch (IllegalArgumentException e) {
stderr.println();
stderr.println("ERROR: " + e.getMessage());
return 3;
} catch (AbortException e) {
stderr.println();
stderr.println("ERROR: " + e.getMessage());
return 5;
} catch (AccessDeniedException e) {
stderr.println();
stderr.println("ERROR: " + e.getMessage());
return 6;
} catch (BadCredentialsException e) {
// to the caller, we can't reveal whether the user didn't exist or the password didn't match.
// do that to the server log instead
String id = UUID.randomUUID().toString();
LOGGER.log(Level.INFO, "CLI login attempt failed: " + id, e);
stderr.println();
stderr.println("ERROR: Bad Credentials. Search the server log for " + id + " for more details.");
return 7;
} catch (Throwable e) {
final String errorMsg = String.format("Unexpected exception occurred while performing %s command.",
getName());
stderr.println();
stderr.println("ERROR: " + errorMsg);
LOGGER.log(Level.WARNING, errorMsg, e);
Functions.printStackTrace(e, stderr);
return 1;
}
}