Line data Source code
1 : // Copyright (C) 2009 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.args4j; 16 : 17 : import static com.google.gerrit.util.cli.Localizable.localizable; 18 : 19 : import com.google.gerrit.entities.AccountGroup; 20 : import com.google.gerrit.entities.InternalGroup; 21 : import com.google.gerrit.server.account.GroupCache; 22 : import com.google.inject.Inject; 23 : import com.google.inject.assistedinject.Assisted; 24 : import java.util.Optional; 25 : import org.kohsuke.args4j.CmdLineException; 26 : import org.kohsuke.args4j.CmdLineParser; 27 : import org.kohsuke.args4j.OptionDef; 28 : import org.kohsuke.args4j.spi.OptionHandler; 29 : import org.kohsuke.args4j.spi.Parameters; 30 : import org.kohsuke.args4j.spi.Setter; 31 : 32 : public class AccountGroupIdHandler extends OptionHandler<AccountGroup.Id> { 33 : private final GroupCache groupCache; 34 : 35 : @Inject 36 : public AccountGroupIdHandler( 37 : final GroupCache groupCache, 38 : @Assisted final CmdLineParser parser, 39 : @Assisted final OptionDef option, 40 : @Assisted final Setter<AccountGroup.Id> setter) { 41 1 : super(parser, option, setter); 42 1 : this.groupCache = groupCache; 43 1 : } 44 : 45 : @Override 46 : public final int parseArguments(Parameters params) throws CmdLineException { 47 0 : final String n = params.getParameter(0); 48 0 : Optional<InternalGroup> group = groupCache.get(AccountGroup.nameKey(n)); 49 0 : if (!group.isPresent()) { 50 0 : throw new CmdLineException(owner, localizable("Group \"%s\" does not exist"), n); 51 : } 52 0 : setter.addValue(group.get().getId()); 53 0 : return 1; 54 : } 55 : 56 : @Override 57 : public final String getDefaultMetaVariable() { 58 0 : return "GROUP"; 59 : } 60 : }