1 /*
2 * Copyright (C) 2018, Salesforce. and others
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Distribution License v. 1.0 which is available at
6 * https://www.eclipse.org/org/documents/edl-v10.php.
7 *
8 * SPDX-License-Identifier: BSD-3-Clause
9 */
10 package org.eclipse.jgit.pgm.opt;
11
12 import org.kohsuke.args4j.CmdLineException;
13 import org.kohsuke.args4j.CmdLineParser;
14 import org.kohsuke.args4j.OptionDef;
15 import org.kohsuke.args4j.spi.Parameters;
16 import org.kohsuke.args4j.spi.Setter;
17 import org.kohsuke.args4j.spi.StringOptionHandler;
18
19 /**
20 * Special handler for the <code>--gpg-sign</code> option of the
21 * <code>commit</code> command.
22 *
23 * The following rules apply:
24 * <ul>
25 * <li>If no key is given, i.e. just <code>--gpg-sign</code> is passed, then it
26 * is the same as <code>--gpg-sign=default</code></li>
27 * </ul>
28 *
29 * @since 5.3
30 */
31 public class GpgSignHandler extends StringOptionHandler {
32
33 /**
34 * The value "default" which will be used when just the option is specified
35 * without any argument
36 */
37 public static final String DEFAULT = "default"; //$NON-NLS-1$
38
39 /**
40 * <p>
41 * Constructor for GpgSignHandler.
42 * </p>
43 *
44 * @param parser
45 * The parser to which this handler belongs.
46 * @param option
47 * The annotation.
48 * @param setter
49 * Object to be used for setting value.
50 */
51 public GpgSignHandler(CmdLineParser parser, OptionDef option,
52 Setter<? super String> setter) {
53 super(parser, option, setter);
54 }
55
56 /** {@inheritDoc} */
57 @Override
58 public int parseArguments(Parameters params) throws CmdLineException {
59 String alias = params.getParameter(-1);
60 if ("--gpg-sign".equals(alias) || "-S".equals(alias)) { //$NON-NLS-1$ //$NON-NLS-2$
61 try {
62 String key = params.getParameter(0);
63 if (key == null || key.startsWith("-")) { //$NON-NLS-1$
64 // ignore invalid values and assume default
65 setter.addValue(DEFAULT);
66 return 0;
67 }
68
69 // use what we have
70 setter.addValue(key);
71 return 1;
72 } catch (CmdLineException e) {
73 // no additional value, assume default
74 setter.addValue(DEFAULT);
75 return 0;
76 }
77 }
78 return 0;
79 }
80
81 }