GpgSignHandler.java

  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. import org.kohsuke.args4j.CmdLineException;
  12. import org.kohsuke.args4j.CmdLineParser;
  13. import org.kohsuke.args4j.OptionDef;
  14. import org.kohsuke.args4j.spi.Parameters;
  15. import org.kohsuke.args4j.spi.Setter;
  16. import org.kohsuke.args4j.spi.StringOptionHandler;

  17. /**
  18.  * Special handler for the <code>--gpg-sign</code> option of the
  19.  * <code>commit</code> command.
  20.  *
  21.  * The following rules apply:
  22.  * <ul>
  23.  * <li>If no key is given, i.e. just <code>--gpg-sign</code> is passed, then it
  24.  * is the same as <code>--gpg-sign=default</code></li>
  25.  * </ul>
  26.  *
  27.  * @since 5.3
  28.  */
  29. public class GpgSignHandler extends StringOptionHandler {

  30.     /**
  31.      * The value "default" which will be used when just the option is specified
  32.      * without any argument
  33.      */
  34.     public static final String DEFAULT = "default"; //$NON-NLS-1$

  35.     /**
  36.      * <p>
  37.      * Constructor for GpgSignHandler.
  38.      * </p>
  39.      *
  40.      * @param parser
  41.      *            The parser to which this handler belongs.
  42.      * @param option
  43.      *            The annotation.
  44.      * @param setter
  45.      *            Object to be used for setting value.
  46.      */
  47.     public GpgSignHandler(CmdLineParser parser, OptionDef option,
  48.             Setter<? super String> setter) {
  49.         super(parser, option, setter);
  50.     }

  51.     /** {@inheritDoc} */
  52.     @Override
  53.     public int parseArguments(Parameters params) throws CmdLineException {
  54.         String alias = params.getParameter(-1);
  55.         if ("--gpg-sign".equals(alias) || "-S".equals(alias)) { //$NON-NLS-1$ //$NON-NLS-2$
  56.             try {
  57.                 String key = params.getParameter(0);
  58.                 if (key == null || key.startsWith("-")) { //$NON-NLS-1$
  59.                     // ignore invalid values and assume default
  60.                     setter.addValue(DEFAULT);
  61.                     return 0;
  62.                 }

  63.                 // use what we have
  64.                 setter.addValue(key);
  65.                 return 1;
  66.             } catch (CmdLineException e) {
  67.                 // no additional value, assume default
  68.                 setter.addValue(DEFAULT);
  69.                 return 0;
  70.             }
  71.         }
  72.         return 0;
  73.     }

  74. }