Search in sources :

Example 31 with CmdLineException

use of org.kohsuke.args4j.CmdLineException in project gerrit by GerritCodeReview.

the class ParameterParser method parse.

<T> boolean parse(T param, ListMultimap<String, String> in, HttpServletRequest req, HttpServletResponse res) throws IOException {
    CmdLineParser clp = parserFactory.create(param);
    DynamicOptions pluginOptions = new DynamicOptions(param, injector, dynamicBeans);
    pluginOptions.parseDynamicBeans(clp);
    pluginOptions.setDynamicBeans();
    pluginOptions.onBeanParseStart();
    try {
        clp.parseOptionMap(in);
    } catch (CmdLineException | NumberFormatException e) {
        if (!clp.wasHelpRequestedByOption()) {
            replyError(req, res, SC_BAD_REQUEST, e.getMessage(), e);
            return false;
        }
    }
    if (clp.wasHelpRequestedByOption()) {
        StringWriter msg = new StringWriter();
        clp.printQueryStringUsage(req.getRequestURI(), msg);
        msg.write('\n');
        msg.write('\n');
        clp.printUsage(msg, null);
        msg.write('\n');
        CacheHeaders.setNotCacheable(res);
        replyBinaryResult(req, res, BinaryResult.create(msg.toString()).setContentType("text/plain"));
        return false;
    }
    pluginOptions.onBeanParseEnd();
    return true;
}
Also used : DynamicOptions(com.google.gerrit.server.DynamicOptions) CmdLineParser(com.google.gerrit.util.cli.CmdLineParser) StringWriter(java.io.StringWriter) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 32 with CmdLineException

use of org.kohsuke.args4j.CmdLineException in project gerrit by GerritCodeReview.

the class ProjectControlHandler method parseArguments.

@Override
public final int parseArguments(final Parameters params) throws CmdLineException {
    String projectName = params.getParameter(0);
    while (projectName.endsWith("/")) {
        projectName = projectName.substring(0, projectName.length() - 1);
    }
    while (projectName.startsWith("/")) {
        // Be nice and drop the leading "/" if supplied by an absolute path.
        // We don't have a file system hierarchy, just a flat namespace in
        // the database's Project entities. We never encode these with a
        // leading '/' but users might accidentally include them in Git URLs.
        //
        projectName = projectName.substring(1);
    }
    String nameWithoutSuffix = ProjectUtil.stripGitSuffix(projectName);
    Project.NameKey nameKey = new Project.NameKey(nameWithoutSuffix);
    ProjectControl control;
    try {
        control = projectControlFactory.controlFor(nameKey, user.get());
        permissionBackend.user(user).project(nameKey).check(ProjectPermission.ACCESS);
    } catch (AuthException e) {
        throw new CmdLineException(owner, new NoSuchProjectException(nameKey).getMessage());
    } catch (NoSuchProjectException e) {
        throw new CmdLineException(owner, e.getMessage());
    } catch (PermissionBackendException | IOException e) {
        log.warn("Cannot load project " + nameWithoutSuffix, e);
        throw new CmdLineException(owner, new NoSuchProjectException(nameKey).getMessage());
    }
    setter.addValue(control);
    return 1;
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) AuthException(com.google.gerrit.extensions.restapi.AuthException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IOException(java.io.IOException) ProjectControl(com.google.gerrit.server.project.ProjectControl) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 33 with CmdLineException

use of org.kohsuke.args4j.CmdLineException in project gerrit by GerritCodeReview.

the class TimestampHandler method parseArguments.

@Override
public int parseArguments(Parameters params) throws CmdLineException {
    String timestamp = params.getParameter(0);
    try {
        DateFormat fmt = new SimpleDateFormat(TIMESTAMP_FORMAT);
        fmt.setTimeZone(TimeZone.getTimeZone("UTC"));
        setter.addValue(new Timestamp(fmt.parse(timestamp).getTime()));
        return 1;
    } catch (ParseException e) {
        throw new CmdLineException(owner, String.format("Invalid timestamp: %s; expected format: %s", timestamp, TIMESTAMP_FORMAT), e);
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Timestamp(java.sql.Timestamp) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 34 with CmdLineException

use of org.kohsuke.args4j.CmdLineException in project gerrit by GerritCodeReview.

the class AccountGroupIdHandler method parseArguments.

@Override
public final int parseArguments(final Parameters params) throws CmdLineException {
    final String n = params.getParameter(0);
    final AccountGroup group = groupCache.get(new AccountGroup.NameKey(n));
    if (group == null) {
        throw new CmdLineException(owner, "Group \"" + n + "\" does not exist");
    }
    setter.addValue(group.getId());
    return 1;
}
Also used : AccountGroup(com.google.gerrit.reviewdb.client.AccountGroup) CmdLineException(org.kohsuke.args4j.CmdLineException)

Example 35 with CmdLineException

use of org.kohsuke.args4j.CmdLineException in project gerrit by GerritCodeReview.

the class AccountIdHandler method parseArguments.

@Override
public int parseArguments(Parameters params) throws CmdLineException {
    String token = params.getParameter(0);
    Account.Id accountId;
    try {
        Account a = accountResolver.find(db.get(), token);
        if (a != null) {
            accountId = a.getId();
        } else {
            switch(authType) {
                case HTTP_LDAP:
                case CLIENT_SSL_CERT_LDAP:
                case LDAP:
                    accountId = createAccountByLdap(token);
                    break;
                case CUSTOM_EXTENSION:
                case DEVELOPMENT_BECOME_ANY_ACCOUNT:
                case HTTP:
                case LDAP_BIND:
                case OAUTH:
                case OPENID:
                case OPENID_SSO:
                default:
                    throw new CmdLineException(owner, "user \"" + token + "\" not found");
            }
        }
    } catch (OrmException | IOException e) {
        throw new CmdLineException(owner, "database is down");
    }
    setter.addValue(accountId);
    return 1;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) OrmException(com.google.gwtorm.server.OrmException) IOException(java.io.IOException) CmdLineException(org.kohsuke.args4j.CmdLineException)

Aggregations

CmdLineException (org.kohsuke.args4j.CmdLineException)105 CmdLineParser (org.kohsuke.args4j.CmdLineParser)80 IOException (java.io.IOException)16 File (java.io.File)14 ArrayList (java.util.ArrayList)11 PrintStream (java.io.PrintStream)7 StringWriter (java.io.StringWriter)6 List (java.util.List)5 FileOutputStream (java.io.FileOutputStream)4 Path (java.nio.file.Path)4 CmdLineParser (com.google.gerrit.util.cli.CmdLineParser)3 FeatureExtractors (io.anserini.ltr.feature.FeatureExtractors)3 Qrels (io.anserini.util.Qrels)3 Directory (org.apache.lucene.store.Directory)3 FSDirectory (org.apache.lucene.store.FSDirectory)3 ConsoleReporter (com.codahale.metrics.ConsoleReporter)2 MetricRegistry (com.codahale.metrics.MetricRegistry)2 Project (com.google.gerrit.reviewdb.client.Project)2 OrmException (com.google.gwtorm.server.OrmException)2 Hudson (hudson.model.Hudson)2