git receive-pack: Use args4j to parse --reviewer and --cc names
It is cleaner to pass the email to Account.Id through an args4j option handler than to do it with a custom conversion method. Change-Id: Ib8c829104015223a2dbf62b7023c885f777a42a7 Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
		| @@ -16,6 +16,7 @@ package com.google.gerrit.server.ssh; | ||||
|  | ||||
| import static com.google.inject.Scopes.SINGLETON; | ||||
|  | ||||
| import com.google.gerrit.client.reviewdb.Account; | ||||
| import com.google.gerrit.client.reviewdb.PatchSet; | ||||
| import com.google.gerrit.pgm.CmdLineParser; | ||||
| import com.google.gerrit.pgm.OptionHandlerFactory; | ||||
| @@ -26,6 +27,7 @@ import com.google.gerrit.server.RemotePeer; | ||||
| import com.google.gerrit.server.config.FactoryModule; | ||||
| import com.google.gerrit.server.config.GerritRequestModule; | ||||
| import com.google.gerrit.server.project.ProjectControl; | ||||
| import com.google.gerrit.server.ssh.commands.AccountIdHandler; | ||||
| import com.google.gerrit.server.ssh.commands.DefaultCommandModule; | ||||
| import com.google.gerrit.server.ssh.commands.PatchSetIdHandler; | ||||
| import com.google.gerrit.server.ssh.commands.ProjectControlHandler; | ||||
| @@ -101,6 +103,7 @@ public class SshModule extends FactoryModule { | ||||
|   private void configureCmdLineParser() { | ||||
|     factory(CmdLineParser.Factory.class); | ||||
|  | ||||
|     registerOptionHandler(Account.Id.class, AccountIdHandler.class); | ||||
|     registerOptionHandler(PatchSet.Id.class, PatchSetIdHandler.class); | ||||
|     registerOptionHandler(ProjectControl.class, ProjectControlHandler.class); | ||||
|   } | ||||
|   | ||||
| @@ -0,0 +1,64 @@ | ||||
| // 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.ssh.commands; | ||||
|  | ||||
| import com.google.gerrit.client.reviewdb.Account; | ||||
| import com.google.gerrit.server.account.AccountResolver; | ||||
| import com.google.gwtorm.client.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; | ||||
|  | ||||
|   @SuppressWarnings("unchecked") | ||||
|   @Inject | ||||
|   public AccountIdHandler(final AccountResolver accountResolver, | ||||
|       @Assisted final CmdLineParser parser, @Assisted final OptionDef option, | ||||
|       @Assisted final Setter setter) { | ||||
|     super(parser, option, setter); | ||||
|     this.accountResolver = accountResolver; | ||||
|   } | ||||
|  | ||||
|   @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) { | ||||
|         throw new CmdLineException(owner, "\"" + token + "\" is not registered"); | ||||
|       } | ||||
|       accountId = a.getId(); | ||||
|     } catch (OrmException e) { | ||||
|       throw new CmdLineException(owner, "database is down"); | ||||
|     } | ||||
|     setter.addValue(accountId); | ||||
|     return 1; | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public final String getDefaultMetaVariable() { | ||||
|     return "EMAIL"; | ||||
|   } | ||||
| } | ||||
| @@ -57,7 +57,6 @@ import com.google.gwtorm.client.OrmRunnable; | ||||
| import com.google.gwtorm.client.Transaction; | ||||
| import com.google.inject.Inject; | ||||
|  | ||||
| import org.kohsuke.args4j.CmdLineException; | ||||
| import org.kohsuke.args4j.Option; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
| @@ -115,25 +114,13 @@ final class Receive extends AbstractGitCommand { | ||||
|   private final Set<Account.Id> ccId = new HashSet<Account.Id>(); | ||||
|  | ||||
|   @Option(name = "--reviewer", aliases = {"--re"}, multiValued = true, metaVar = "EMAIL", usage = "request reviewer for change(s)") | ||||
|   void addReviewer(final String nameOrEmail) throws CmdLineException { | ||||
|     try { | ||||
|       reviewerId.add(toAccountId(nameOrEmail)); | ||||
|     } catch (NoSuchAccountException e) { | ||||
|       throw new CmdLineException(e.getMessage()); | ||||
|     } catch (OrmException e) { | ||||
|       throw new CmdLineException("database is down"); | ||||
|     } | ||||
|   void addReviewer(final Account.Id id) { | ||||
|     reviewerId.add(id); | ||||
|   } | ||||
|  | ||||
|   @Option(name = "--cc", aliases = {}, multiValued = true, metaVar = "EMAIL", usage = "CC user on change(s)") | ||||
|   void addCC(final String nameOrEmail) throws CmdLineException { | ||||
|     try { | ||||
|       ccId.add(toAccountId(nameOrEmail)); | ||||
|     } catch (NoSuchAccountException e) { | ||||
|       throw new CmdLineException(e.getMessage()); | ||||
|     } catch (OrmException e) { | ||||
|       throw new CmdLineException("database is down"); | ||||
|     } | ||||
|   void addCC(final Account.Id id) { | ||||
|     ccId.add(id); | ||||
|   } | ||||
|  | ||||
|   @Inject | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Shawn O. Pearce
					Shawn O. Pearce