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:
@@ -19,7 +19,7 @@ import com.google.common.util.concurrent.Atomics;
|
||||
import com.google.gerrit.extensions.annotations.RequiresCapability;
|
||||
import com.google.gerrit.server.CurrentUser;
|
||||
import com.google.gerrit.server.account.CapabilityControl;
|
||||
import com.google.gerrit.sshd.args4j.SubcommandHandler;
|
||||
import com.google.gerrit.server.args4j.SubcommandHandler;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.assistedinject.Assisted;
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.reviewdb.client.AccountGroup;
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
import com.google.gerrit.reviewdb.client.PatchSet;
|
||||
import com.google.gerrit.server.CmdLineParserModule;
|
||||
import com.google.gerrit.server.PeerDaemonUser;
|
||||
import com.google.gerrit.server.RemotePeer;
|
||||
import com.google.gerrit.server.account.AccountManager;
|
||||
@@ -38,18 +39,8 @@ import com.google.gerrit.server.plugins.StartPluginListener;
|
||||
import com.google.gerrit.server.project.ProjectControl;
|
||||
import com.google.gerrit.server.ssh.SshInfo;
|
||||
import com.google.gerrit.server.util.RequestScopePropagator;
|
||||
import com.google.gerrit.sshd.args4j.AccountGroupIdHandler;
|
||||
import com.google.gerrit.sshd.args4j.AccountGroupUUIDHandler;
|
||||
import com.google.gerrit.sshd.args4j.AccountIdHandler;
|
||||
import com.google.gerrit.sshd.args4j.ChangeIdHandler;
|
||||
import com.google.gerrit.sshd.args4j.ObjectIdHandler;
|
||||
import com.google.gerrit.sshd.args4j.PatchSetIdHandler;
|
||||
import com.google.gerrit.sshd.args4j.ProjectControlHandler;
|
||||
import com.google.gerrit.sshd.args4j.SocketAddressHandler;
|
||||
import com.google.gerrit.sshd.commands.DefaultCommandModule;
|
||||
import com.google.gerrit.sshd.commands.QueryShell;
|
||||
import com.google.gerrit.util.cli.CmdLineParser;
|
||||
import com.google.gerrit.util.cli.OptionHandlerUtil;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.internal.UniqueAnnotations;
|
||||
import com.google.inject.servlet.RequestScoped;
|
||||
@@ -59,7 +50,6 @@ import org.apache.sshd.server.CommandFactory;
|
||||
import org.apache.sshd.server.PublickeyAuthenticator;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.kohsuke.args4j.spi.OptionHandler;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
import java.util.Map;
|
||||
@@ -83,7 +73,7 @@ public class SshModule extends FactoryModule {
|
||||
bind(SshScope.class).in(SINGLETON);
|
||||
|
||||
configureRequestScope();
|
||||
configureCmdLineParser();
|
||||
install(new CmdLineParserModule());
|
||||
configureAliases();
|
||||
|
||||
install(SshKeyCacheImpl.module());
|
||||
@@ -155,22 +145,4 @@ public class SshModule extends FactoryModule {
|
||||
|
||||
install(new GerritRequestModule());
|
||||
}
|
||||
|
||||
private void configureCmdLineParser() {
|
||||
factory(CmdLineParser.Factory.class);
|
||||
|
||||
registerOptionHandler(Account.Id.class, AccountIdHandler.class);
|
||||
registerOptionHandler(AccountGroup.Id.class, AccountGroupIdHandler.class);
|
||||
registerOptionHandler(AccountGroup.UUID.class, AccountGroupUUIDHandler.class);
|
||||
registerOptionHandler(Change.Id.class, ChangeIdHandler.class);
|
||||
registerOptionHandler(ObjectId.class, ObjectIdHandler.class);
|
||||
registerOptionHandler(PatchSet.Id.class, PatchSetIdHandler.class);
|
||||
registerOptionHandler(ProjectControl.class, ProjectControlHandler.class);
|
||||
registerOptionHandler(SocketAddress.class, SocketAddressHandler.class);
|
||||
}
|
||||
|
||||
private <T> void registerOptionHandler(Class<T> type,
|
||||
Class<? extends OptionHandler<T>> impl) {
|
||||
install(OptionHandlerUtil.moduleFor(type, impl));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
// 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.sshd.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";
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
// 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.sshd.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";
|
||||
}
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
// 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.sshd.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";
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
// 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.sshd.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";
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
// 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.sshd.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";
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
// 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.sshd.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";
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
// 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.sshd.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";
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
// 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.sshd.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";
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
// 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.sshd.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";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user