Refactor cmdline parsing into gerrit-server

Now that both gerrit-sshd and gerrit-httpd use CmdLineParser, it makes
sense that the handlers for particular classes exist in a module
accessible by both.  This change moves the command line parsing portion
of SshModule into gerrit-server in a class called CmdLineParserModule.

Change-Id: I9312996a2b14106c039bc4c25ed80a52e3df79e7
This commit is contained in:
Conley Owens
2012-05-31 10:34:49 -07:00
parent 327aba6f84
commit ee4ccd0ab7
13 changed files with 76 additions and 42 deletions

View File

@@ -0,0 +1,56 @@
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.args4j;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.server.account.GroupCache;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
public class AccountGroupIdHandler extends OptionHandler<AccountGroup.Id> {
private final GroupCache groupCache;
@Inject
public AccountGroupIdHandler(final GroupCache groupCache,
@Assisted final CmdLineParser parser, @Assisted final OptionDef option,
@Assisted final Setter<AccountGroup.Id> setter) {
super(parser, option, setter);
this.groupCache = groupCache;
}
@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;
}
@Override
public final String getDefaultMetaVariable() {
return "GROUP";
}
}

View File

@@ -0,0 +1,58 @@
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.args4j;
import com.google.gerrit.common.data.GroupReference;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.server.account.GroupBackend;
import com.google.gerrit.server.account.GroupBackends;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
public class AccountGroupUUIDHandler extends OptionHandler<AccountGroup.UUID> {
private final GroupBackend groupBackend;
@Inject
public AccountGroupUUIDHandler(final GroupBackend groupBackend,
@Assisted final CmdLineParser parser, @Assisted final OptionDef option,
@Assisted final Setter<AccountGroup.UUID> setter) {
super(parser, option, setter);
this.groupBackend = groupBackend;
}
@Override
public final int parseArguments(final Parameters params)
throws CmdLineException {
final String n = params.getParameter(0);
final GroupReference group = GroupBackends.findBestSuggestion(groupBackend, n);
if (group == null) {
throw new CmdLineException(owner, "Group \"" + n + "\" does not exist");
}
setter.addValue(group.getUUID());
return 1;
}
@Override
public final String getDefaultMetaVariable() {
return "GROUP";
}
}

View File

@@ -0,0 +1,98 @@
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.args4j;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AuthType;
import com.google.gerrit.server.account.AccountException;
import com.google.gerrit.server.account.AccountManager;
import com.google.gerrit.server.account.AccountResolver;
import com.google.gerrit.server.account.AuthRequest;
import com.google.gerrit.server.config.AuthConfig;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
public class AccountIdHandler extends OptionHandler<Account.Id> {
private final AccountResolver accountResolver;
private final AccountManager accountManager;
private final AuthType authType;
@Inject
public AccountIdHandler(final AccountResolver accountResolver,
final AccountManager accountManager,
final AuthConfig authConfig,
@Assisted final CmdLineParser parser, @Assisted final OptionDef option,
@Assisted final Setter<Account.Id> setter) {
super(parser, option, setter);
this.accountResolver = accountResolver;
this.accountManager = accountManager;
this.authType = authConfig.getAuthType();
}
@Override
public final int parseArguments(final Parameters params)
throws CmdLineException {
final String token = params.getParameter(0);
final Account.Id accountId;
try {
final Account a = accountResolver.find(token);
if (a != null) {
accountId = a.getId();
} else {
switch (authType) {
case HTTP_LDAP:
case CLIENT_SSL_CERT_LDAP:
case LDAP:
accountId = createAccountByLdap(token);
break;
default:
throw new CmdLineException(owner, "user \"" + token + "\" not found");
}
}
} catch (OrmException e) {
throw new CmdLineException(owner, "database is down");
}
setter.addValue(accountId);
return 1;
}
private Account.Id createAccountByLdap(String user)
throws CmdLineException {
if (!user.matches(Account.USER_NAME_PATTERN)) {
throw new CmdLineException(owner, "user \"" + user + "\" not found");
}
try {
AuthRequest req = AuthRequest.forUser(user);
req.setSkipAuthentication(true);
return accountManager.authenticate(req).getAccountId();
} catch (AccountException e) {
throw new CmdLineException(owner, "user \"" + user + "\" not found");
}
}
@Override
public final String getDefaultMetaVariable() {
return "EMAIL";
}
}

View File

@@ -0,0 +1,77 @@
// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.args4j;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
public class ChangeIdHandler extends OptionHandler<Change.Id> {
@Inject
private ReviewDb db;
@Inject
public ChangeIdHandler(
final ReviewDb db,
@Assisted final CmdLineParser parser, @Assisted final OptionDef option,
@Assisted final Setter<Change.Id> setter) {
super(parser, option, setter);
this.db = db;
}
@Override
public final int parseArguments(final Parameters params)
throws CmdLineException {
final String token = params.getParameter(0);
final String[] tokens = token.split(",");
if (tokens.length != 3) {
throw new CmdLineException(owner, "change should be specified as "
+ "<project>,<branch>,<change-id>");
}
try {
final Change.Key key = Change.Key.parse(tokens[2]);
final Project.NameKey project = new Project.NameKey(tokens[0]);
final Branch.NameKey branch = new Branch.NameKey(project, tokens[1]);
for (final Change change : db.changes().byBranchKey(branch, key)) {
setter.addValue(change.getId());
return 1;
}
} catch (IllegalArgumentException e) {
throw new CmdLineException(owner, "Change-Id is not valid");
} catch (OrmException e) {
throw new CmdLineException(owner, "Database error: " + e.getMessage());
}
throw new CmdLineException(owner, "\"" + token + "\": change not found");
}
@Override
public final String getDefaultMetaVariable() {
return "CHANGE";
}
}

View File

@@ -0,0 +1,47 @@
// Copyright (C) 2012 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.args4j;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import org.eclipse.jgit.lib.ObjectId;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
public class ObjectIdHandler extends OptionHandler<ObjectId> {
@Inject
public ObjectIdHandler(@Assisted final CmdLineParser parser,
@Assisted final OptionDef option, @Assisted final Setter<ObjectId> setter) {
super(parser, option, setter);
}
@Override
public int parseArguments(Parameters params) throws CmdLineException {
final String n = params.getParameter(0);
setter.addValue(ObjectId.fromString(n));
return 1;
}
@Override
public String getDefaultMetaVariable() {
return "COMMIT";
}
}

View File

@@ -0,0 +1,56 @@
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.args4j;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
public class PatchSetIdHandler extends OptionHandler<PatchSet.Id> {
@Inject
public PatchSetIdHandler(@Assisted final CmdLineParser parser,
@Assisted final OptionDef option, @Assisted final Setter<PatchSet.Id> setter) {
super(parser, option, setter);
}
@Override
public final int parseArguments(final Parameters params)
throws CmdLineException {
final String token = params.getParameter(0);
final PatchSet.Id id;
try {
id = PatchSet.Id.parse(token);
} catch (IllegalArgumentException e) {
throw new CmdLineException(owner, "\"" + token
+ "\" is not a valid patch set");
}
setter.addValue(id);
return 1;
}
@Override
public final String getDefaultMetaVariable() {
return "CHANGE,PATCHSET";
}
}

View File

@@ -0,0 +1,87 @@
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.args4j;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.project.NoSuchProjectException;
import com.google.gerrit.server.project.ProjectControl;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
public class ProjectControlHandler extends OptionHandler<ProjectControl> {
private final ProjectControl.Factory projectControlFactory;
@Inject
public ProjectControlHandler(
final ProjectControl.Factory projectControlFactory,
@Assisted final CmdLineParser parser, @Assisted final OptionDef option,
@Assisted final Setter<ProjectControl> setter) {
super(parser, option, setter);
this.projectControlFactory = projectControlFactory;
}
@Override
public final int parseArguments(final Parameters params)
throws CmdLineException {
final String token = params.getParameter(0);
String projectName = token;
while (projectName.endsWith("/")) {
projectName = projectName.substring(0, projectName.length() - 1);
}
if (projectName.endsWith(".git")) {
// Be nice and drop the trailing ".git" suffix, which we never keep
// in our database, but clients might mistakenly provide anyway.
//
projectName = projectName.substring(0, projectName.length() - 4);
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);
}
final ProjectControl control;
try {
Project.NameKey nameKey = new Project.NameKey(projectName);
control = projectControlFactory.validateFor(nameKey);
} catch (NoSuchProjectException e) {
throw new CmdLineException(owner, "'" + token + "': not a Gerrit project");
}
setter.addValue(control);
return 1;
}
@Override
public final String getDefaultMetaVariable() {
return "PROJECT";
}
}

View File

@@ -0,0 +1,54 @@
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.args4j;
import com.google.gerrit.server.util.SocketUtil;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
import java.net.SocketAddress;
public class SocketAddressHandler extends OptionHandler<SocketAddress> {
@Inject
public SocketAddressHandler(@Assisted final CmdLineParser parser,
@Assisted final OptionDef option, @Assisted final Setter<SocketAddress> setter) {
super(parser, option, setter);
}
@Override
public final int parseArguments(final Parameters params)
throws CmdLineException {
final String token = params.getParameter(0);
try {
setter.addValue(SocketUtil.parse(token, 0));
} catch (IllegalArgumentException e) {
throw new CmdLineException(owner, e.getMessage());
}
return 1;
}
@Override
public final String getDefaultMetaVariable() {
return "HOST:PORT";
}
}

View File

@@ -0,0 +1,47 @@
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.args4j;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
public class SubcommandHandler extends OptionHandler<String> {
@Inject
public SubcommandHandler(@Assisted final CmdLineParser parser,
@Assisted final OptionDef option, @Assisted final Setter<String> setter) {
super(parser, option, setter);
}
@Override
public final int parseArguments(final Parameters params)
throws CmdLineException {
setter.addValue(params.getParameter(0));
owner.stopOptionParsing();
return 1;
}
@Override
public final String getDefaultMetaVariable() {
return "COMMAND";
}
}