ShowCommands.java

  1. /*
  2.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> 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.debug;

  11. import java.io.IOException;
  12. import java.net.URL;

  13. import org.eclipse.jgit.pgm.Command;
  14. import org.eclipse.jgit.pgm.CommandCatalog;
  15. import org.eclipse.jgit.pgm.CommandRef;
  16. import org.eclipse.jgit.pgm.TextBuiltin;
  17. import org.eclipse.jgit.pgm.internal.CLIText;
  18. import org.eclipse.jgit.util.io.ThrowingPrintWriter;
  19. import org.kohsuke.args4j.Option;

  20. @Command(usage = "usage_displayAListOfAllRegisteredJgitCommands")
  21. class ShowCommands extends TextBuiltin {
  22.     @Option(name = "--pretty", metaVar = "metaVar_commandDetail", usage = "usage_alterTheDetailShown")
  23.     private Format pretty = Format.USAGE;

  24.     /** {@inheritDoc} */
  25.     @Override
  26.     protected void run() throws Exception {
  27.         final CommandRef[] list = CommandCatalog.all();

  28.         int width = 0;
  29.         for (CommandRef c : list)
  30.             width = Math.max(width, c.getName().length());
  31.         width += 2;

  32.         for (CommandRef c : list) {
  33.             errw.print(c.isCommon() ? '*' : ' ');
  34.             errw.print(' ');

  35.             errw.print(c.getName());
  36.             for (int i = c.getName().length(); i < width; i++)
  37.                 errw.print(' ');

  38.             pretty.print(errw, c);
  39.             errw.println();
  40.         }
  41.         errw.println();
  42.     }

  43.     enum Format {
  44.         /** */
  45.         USAGE {
  46.             @Override
  47.             void print(ThrowingPrintWriter err, CommandRef c) throws IOException {
  48.                 String usage = c.getUsage();
  49.                 if (usage != null && usage.length() > 0)
  50.                     err.print(CLIText.get().resourceBundle().getString(usage));
  51.             }
  52.         },

  53.         /** */
  54.         CLASSES {
  55.             @Override
  56.             void print(ThrowingPrintWriter err, CommandRef c) throws IOException {
  57.                 err.print(c.getImplementationClassName());
  58.             }
  59.         },

  60.         /** */
  61.         URLS {
  62.             @Override
  63.             void print(ThrowingPrintWriter err, CommandRef c) throws IOException {
  64.                 final ClassLoader ldr = c.getImplementationClassLoader();

  65.                 String cn = c.getImplementationClassName();
  66.                 cn = cn.replace('.', '/') + ".class"; //$NON-NLS-1$

  67.                 final URL url = ldr.getResource(cn);
  68.                 if (url == null) {
  69.                     err.print(CLIText.get().notFound);
  70.                     return;
  71.                 }

  72.                 String rn = url.toExternalForm();
  73.                 if (rn.endsWith(cn))
  74.                     rn = rn.substring(0, rn.length() - cn.length());

  75.                 err.print(rn);
  76.             }
  77.         };

  78.         abstract void print(ThrowingPrintWriter err, CommandRef c) throws IOException;
  79.     }
  80. }