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.microservice.*;
021import org.apache.juneau.cp.*;
022
023/**
024 * Implements the 'config' console command to get or set configuration.
025 */
026public class ConfigCommand extends ConsoleCommand {
027
028   private final Messages mb = Messages.of(ConfigCommand.class, "Messages");
029
030   @Override /* ConsoleCommand */
031   public String getName() {
032      return "config";
033   }
034
035   @Override /* ConsoleCommand */
036   public String getSynopsis() {
037      return "config [get|set]";
038   }
039
040   @Override /* ConsoleCommand */
041   public String getInfo() {
042      return mb.getString("info");
043   }
044
045   @Override /* ConsoleCommand */
046   public String getDescription() {
047      return mb.getString("description");
048   }
049
050   @Override /* ConsoleCommand */
051   public boolean execute(Scanner in, PrintWriter out, Args args) {
052      Config conf = Microservice.getInstance().getConfig();
053      if (args.size() > 2) {
054         String option = args.getArg(1);
055         String key = args.getArg(2);
056         if (option.equals("get")) {
057            // config get <key>
058            if (args.size() == 3) {
059               String val = conf.getString(key);
060               if (val != null)
061                  out.println(val);
062               else
063                  out.println(mb.getString("KeyNotFound", key));
064            } else {
065               out.println(mb.getString("TooManyArguments"));
066            }
067         } else if (option.equals("set")) {
068            // config set <key> <value>
069            if (args.size() == 4) {
070               conf.set(key, args.getArg(3));
071               out.println(mb.getString("ConfigSet"));
072            } else if (args.size() < 4) {
073               out.println(mb.getString("InvalidArguments"));
074            } else {
075               out.println(mb.getString("TooManyArguments"));
076            }
077         } else if (option.equals("remove")) {
078            // config remove <key>
079            if (args.size() == 3) {
080               if (conf.getString(key) != null) {
081                  conf.remove(key);
082                  out.println(mb.getString("ConfigRemove", key));
083               } else {
084                  out.println(mb.getString("KeyNotFound", key));
085               }
086            } else {
087               out.println(mb.getString("TooManyArguments"));
088            }
089         } else {
090            out.println(mb.getString("InvalidArguments"));
091         }
092      } else {
093         out.println(mb.getString("InvalidArguments"));
094      }
095      return false;
096   }
097}