gerrit approve: Cleanup option parsing to reduce unnecessary code
A lot of this code is just unnecessary complexity, instead we can pass through the ApprovalType and save quite a few lines of code. Change-Id: I02f51beeb35bf9e464c243bd0aa63336d9646dce Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
parent
f72824741b
commit
5619228992
@ -49,7 +49,7 @@ public class ApproveCommand extends BaseCommand {
|
|||||||
@Override
|
@Override
|
||||||
protected final CmdLineParser newCmdLineParser() {
|
protected final CmdLineParser newCmdLineParser() {
|
||||||
final CmdLineParser parser = super.newCmdLineParser();
|
final CmdLineParser parser = super.newCmdLineParser();
|
||||||
for (CmdOption c : optionList) {
|
for (ApproveOption c : optionList) {
|
||||||
parser.addOption(c, c);
|
parser.addOption(c, c);
|
||||||
}
|
}
|
||||||
return parser;
|
return parser;
|
||||||
@ -82,14 +82,14 @@ public class ApproveCommand extends BaseCommand {
|
|||||||
@Inject
|
@Inject
|
||||||
private FunctionState.Factory functionStateFactory;
|
private FunctionState.Factory functionStateFactory;
|
||||||
|
|
||||||
private List<CmdOption> optionList;
|
private List<ApproveOption> optionList;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void start() {
|
public final void start() {
|
||||||
startThread(new CommandRunnable() {
|
startThread(new CommandRunnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() throws Exception {
|
public void run() throws Exception {
|
||||||
getApprovalNames();
|
initOptionList();
|
||||||
parseCommandLine();
|
parseCommandLine();
|
||||||
|
|
||||||
final Transaction txn = db.beginTransaction();
|
final Transaction txn = db.beginTransaction();
|
||||||
@ -113,9 +113,8 @@ public class ApproveCommand extends BaseCommand {
|
|||||||
sb.append(patchSetId.get());
|
sb.append(patchSetId.get());
|
||||||
sb.append(": ");
|
sb.append(": ");
|
||||||
|
|
||||||
for (CmdOption co : optionList) {
|
for (ApproveOption co : optionList) {
|
||||||
ApprovalCategory.Id category =
|
final ApprovalCategory.Id category = co.getCategoryId();
|
||||||
new ApprovalCategory.Id(co.approvalKey());
|
|
||||||
PatchSetApproval.Key psaKey =
|
PatchSetApproval.Key psaKey =
|
||||||
new PatchSetApproval.Key(patchSetId, currentUser.getAccountId(),
|
new PatchSetApproval.Key(patchSetId, currentUser.getAccountId(),
|
||||||
category);
|
category);
|
||||||
@ -175,7 +174,7 @@ public class ApproveCommand extends BaseCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void addApproval(final PatchSetApproval.Key psaKey,
|
private void addApproval(final PatchSetApproval.Key psaKey,
|
||||||
final Short score, final Change c, final CmdOption co,
|
final Short score, final Change c, final ApproveOption co,
|
||||||
final Transaction txn) throws OrmException, UnloggedFailure {
|
final Transaction txn) throws OrmException, UnloggedFailure {
|
||||||
PatchSetApproval psa = db.patchSetApprovals().get(psaKey);
|
PatchSetApproval psa = db.patchSetApprovals().get(psaKey);
|
||||||
boolean insert = false;
|
boolean insert = false;
|
||||||
@ -203,8 +202,8 @@ public class ApproveCommand extends BaseCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getApprovalNames() {
|
private void initOptionList() {
|
||||||
optionList = new ArrayList<CmdOption>();
|
optionList = new ArrayList<ApproveOption>();
|
||||||
|
|
||||||
for (ApprovalType type : approvalTypes.getApprovalTypes()) {
|
for (ApprovalType type : approvalTypes.getApprovalTypes()) {
|
||||||
String usage = "";
|
String usage = "";
|
||||||
@ -213,14 +212,13 @@ public class ApproveCommand extends BaseCommand {
|
|||||||
|
|
||||||
for (ApprovalCategoryValue v : type.getValues()) {
|
for (ApprovalCategoryValue v : type.getValues()) {
|
||||||
usage +=
|
usage +=
|
||||||
String.format("%3s", CmdOption.format(v.getValue())) + ": "
|
String.format("%3s", ApproveOption.format(v.getValue())) + ": "
|
||||||
+ v.getName() + "\n";
|
+ v.getName() + "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
optionList.add(new CmdOption("--"
|
final String name =
|
||||||
+ category.getName().toLowerCase().replace(' ', '-'), usage, category
|
"--" + category.getName().toLowerCase().replace(' ', '-');
|
||||||
.getId().get(), type.getMin().getValue(), type.getMax().getValue(),
|
optionList.add(new ApproveOption(name, usage, type));
|
||||||
category.getName()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,6 +14,9 @@
|
|||||||
|
|
||||||
package com.google.gerrit.server.ssh.commands;
|
package com.google.gerrit.server.ssh.commands;
|
||||||
|
|
||||||
|
import com.google.gerrit.client.data.ApprovalType;
|
||||||
|
import com.google.gerrit.client.reviewdb.ApprovalCategory;
|
||||||
|
|
||||||
import org.kohsuke.args4j.CmdLineException;
|
import org.kohsuke.args4j.CmdLineException;
|
||||||
import org.kohsuke.args4j.CmdLineParser;
|
import org.kohsuke.args4j.CmdLineParser;
|
||||||
import org.kohsuke.args4j.Option;
|
import org.kohsuke.args4j.Option;
|
||||||
@ -24,91 +27,58 @@ import org.kohsuke.args4j.spi.Setter;
|
|||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
|
|
||||||
class CmdOption implements Option, Setter<Short> {
|
final class ApproveOption implements Option, Setter<Short> {
|
||||||
private String metaVar;
|
private final String name;
|
||||||
private boolean multiValued;
|
private final String usage;
|
||||||
private String name;
|
private final ApprovalType type;
|
||||||
private boolean required;
|
|
||||||
private String usage;
|
|
||||||
|
|
||||||
private String approvalKey;
|
|
||||||
private Short approvalMax;
|
|
||||||
private Short approvalMin;
|
|
||||||
private String descrName;
|
|
||||||
|
|
||||||
private Short value;
|
private Short value;
|
||||||
|
|
||||||
public CmdOption(final String name, final String usage, final String key,
|
ApproveOption(final String name, final String usage, final ApprovalType type) {
|
||||||
final Short min, final Short max, final String descrName) {
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.usage = usage;
|
this.usage = usage;
|
||||||
|
this.type = type;
|
||||||
this.metaVar = "";
|
|
||||||
this.multiValued = false;
|
|
||||||
this.required = false;
|
|
||||||
this.value = null;
|
|
||||||
|
|
||||||
this.approvalKey = key;
|
|
||||||
this.approvalMax = max;
|
|
||||||
this.approvalMin = min;
|
|
||||||
this.descrName = descrName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final String[] aliases() {
|
public String[] aliases() {
|
||||||
return new String[0];
|
return new String[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final Class<? extends OptionHandler<Short>> handler() {
|
public Class<? extends OptionHandler<Short>> handler() {
|
||||||
return Handler.class;
|
return Handler.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final String metaVar() {
|
public String metaVar() {
|
||||||
return metaVar;
|
return "N";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final boolean multiValued() {
|
public boolean multiValued() {
|
||||||
return multiValued;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final String name() {
|
public String name() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final boolean required() {
|
public boolean required() {
|
||||||
return required;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final String usage() {
|
public String usage() {
|
||||||
return usage;
|
return usage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final Short value() {
|
public Short value() {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final String approvalKey() {
|
|
||||||
return approvalKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final Short approvalMax() {
|
|
||||||
return approvalMax;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final Short approvalMin() {
|
|
||||||
return approvalMin;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final String descrName() {
|
|
||||||
return descrName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class<? extends Annotation> annotationType() {
|
public Class<? extends Annotation> annotationType() {
|
||||||
return null;
|
return null;
|
||||||
@ -129,13 +99,17 @@ class CmdOption implements Option, Setter<Short> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ApprovalCategory.Id getCategoryId() {
|
||||||
|
return type.getCategory().getId();
|
||||||
|
}
|
||||||
|
|
||||||
public static class Handler extends OneArgumentOptionHandler<Short> {
|
public static class Handler extends OneArgumentOptionHandler<Short> {
|
||||||
private final CmdOption cmdOption;
|
private final ApproveOption cmdOption;
|
||||||
|
|
||||||
public Handler(final CmdLineParser parser, final OptionDef option,
|
public Handler(final CmdLineParser parser, final OptionDef option,
|
||||||
final Setter<Short> setter) {
|
final Setter<Short> setter) {
|
||||||
super(parser, option, setter);
|
super(parser, option, setter);
|
||||||
this.cmdOption = (CmdOption) setter;
|
this.cmdOption = (ApproveOption) setter;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -147,8 +121,8 @@ class CmdOption implements Option, Setter<Short> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final short value = Short.parseShort(argument);
|
final short value = Short.parseShort(argument);
|
||||||
final short min = cmdOption.approvalMin;
|
final short min = cmdOption.type.getMin().getValue();
|
||||||
final short max = cmdOption.approvalMax;
|
final short max = cmdOption.type.getMax().getValue();
|
||||||
|
|
||||||
if (value < min || value > max) {
|
if (value < min || value > max) {
|
||||||
final String name = cmdOption.name();
|
final String name = cmdOption.name();
|
Loading…
x
Reference in New Issue
Block a user