001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.microservice.console;
014
015import java.io.*;
016import java.util.*;
017
018import org.apache.juneau.collections.*;
019import org.apache.juneau.config.*;
020import org.apache.juneau.cp.*;
021import org.apache.juneau.microservice.*;
022
023/**
024 * Implements the 'config' console command to get or set configuration.
025 *
026 * <h5 class='section'>See Also:</h5><ul>
027 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-microservice-core">juneau-microservice-core</a>
028 * </ul>
029 */
030public class ConfigCommand extends ConsoleCommand {
031
032   private final Messages mb = Messages.of(ConfigCommand.class, "Messages");
033
034   @Override /* ConsoleCommand */
035   public String getName() {
036      return "config";
037   }
038
039   @Override /* ConsoleCommand */
040   public String getSynopsis() {
041      return "config [get|set]";
042   }
043
044   @Override /* ConsoleCommand */
045   public String getInfo() {
046      return mb.getString("info");
047   }
048
049   @Override /* ConsoleCommand */
050   public String getDescription() {
051      return mb.getString("description");
052   }
053
054   @Override /* ConsoleCommand */
055   public boolean execute(Scanner in, PrintWriter out, Args args) {
056      Config conf = Microservice.getInstance().getConfig();
057      if (args.size() > 2) {
058         String option = args.getArg(1);
059         String key = args.getArg(2);
060         if (option.equals("get")) {
061            // config get <key>
062            if (args.size() == 3) {
063               String val = conf.get(key).orElse(null);
064               if (val != null)
065                  out.println(val);
066               else
067                  out.println(mb.getString("KeyNotFound", key));
068            } else {
069               out.println(mb.getString("TooManyArguments"));
070            }
071         } else if (option.equals("set")) {
072            // config set <key> <value>
073            if (args.size() == 4) {
074               conf.set(key, args.getArg(3));
075               out.println(mb.getString("ConfigSet"));
076            } else if (args.size() < 4) {
077               out.println(mb.getString("InvalidArguments"));
078            } else {
079               out.println(mb.getString("TooManyArguments"));
080            }
081         } else if (option.equals("remove")) {
082            // config remove <key>
083            if (args.size() == 3) {
084               if (conf.get(key).isPresent()) {
085                  conf.remove(key);
086                  out.println(mb.getString("ConfigRemove", key));
087               } else {
088                  out.println(mb.getString("KeyNotFound", key));
089               }
090            } else {
091               out.println(mb.getString("TooManyArguments"));
092            }
093         } else {
094            out.println(mb.getString("InvalidArguments"));
095         }
096      } else {
097         out.println(mb.getString("InvalidArguments"));
098      }
099      return false;
100   }
101}