001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.microservice.console;
018
019import static org.apache.juneau.commons.utils.Utils.*;
020
021import java.io.*;
022import java.util.*;
023
024import org.apache.juneau.collections.*;
025import org.apache.juneau.cp.*;
026import org.apache.juneau.microservice.*;
027
028/**
029 * Implements the 'restart' console command to gracefully shut down and restart the microservice.
030 *
031 * <h5 class='section'>See Also:</h5><ul>
032 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauMicroserviceCoreBasics">juneau-microservice-core Basics</a>
033 * </ul>
034 */
035public class HelpCommand extends ConsoleCommand {
036
037   private final Messages mb = Messages.of(HelpCommand.class, "Messages");
038
039   @Override /* Overridden from ConsoleCommand */
040   public boolean execute(Scanner in, PrintWriter out, Args args) throws Exception {
041      var commands = Microservice.getInstance().getConsoleCommands();
042      if (args.size() == 1) {
043         out.println(mb.getString("ListOfAvailableCommands"));
044         commands.forEach((k, v) -> out.append("\t").append(v.getName()).append(" -- ").append(indent(v.getInfo())).println());
045         out.println();
046      } else {
047         var cc = commands.get(args.getArg(1));
048         if (cc == null) {
049            out.println(mb.getString("CommandNotFound"));
050         } else {
051            var info = cc.getInfo();
052            var synopsis = cc.getSynopsis();
053            var description = cc.getDescription();
054            var examples = cc.getExamples();
055
056            out.append(mb.getString("NAME")).append("\n\t").append(cc.getName()).append(info == null ? "" : " -- " + indent(info)).println();
057
058            if (nn(synopsis))
059               out.append('\n').append(mb.getString("SYNOPSIS")).append("\n\t").append(indent(synopsis)).println();
060
061            if (nn(description))
062               out.append('\n').append(mb.getString("DESCRIPTION")).append("\n\t").append(indent(description)).println();
063
064            if (nn(examples))
065               out.append('\n').append(mb.getString("EXAMPLES")).append("\n\t").append(indent(examples)).println();
066         }
067      }
068      return false;
069   }
070
071   @Override /* Overridden from ConsoleCommand */
072   public String getDescription() { return mb.getString("description"); }
073
074   @Override /* Overridden from ConsoleCommand */
075   public String getExamples() { return mb.getString("examples"); }
076
077   @Override /* Overridden from ConsoleCommand */
078   public String getInfo() { return mb.getString("info"); }
079
080   @Override /* Overridden from ConsoleCommand */
081   public String getName() { return "help"; }
082
083   @Override /* Overridden from ConsoleCommand */
084   public String getSynopsis() { return "help [command]"; }
085
086   private static String indent(String in) {
087      if (in == null)
088         return "";
089      return in.replaceAll("\n", "\n\t");
090   }
091}