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.Shorts.*;
020
021import java.io.*;
022import java.util.*;
023
024import org.apache.juneau.commons.runtime.*;
025import org.apache.juneau.marshall.cp.*;
026import org.apache.juneau.microservice.*;
027
028/**
029 * Implements the 'config' console command to get or set configuration.
030 *
031 * <h5 class='section'>See Also:</h5><ul>
032 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauMicroservice">juneau-microservice</a>
033 * </ul>
034 */
035@SuppressWarnings({
036   "java:S115", // Constants use UPPER_snakeCase convention (e.g., MKEY_invalidArguments)
037   "java:S1192" // Duplicated literals read more clearly inline than as constants.
038})
039public class ConfigCommand extends ConsoleCommand {
040
041   private final Messages mb = Messages.of(ConfigCommand.class, "Messages");
042
043   @Override /* Overridden from ConsoleCommand */
044   @SuppressWarnings({
045      "java:S3776" // Cognitive complexity acceptable for console command logic
046   })
047   public boolean execute(Scanner in, PrintWriter out, Args args) {
048      var conf = Microservice.getInstance().getConfig();
049      var size = args.argCount() + args.optionCount();
050      if (size > 2) {
051         var option = args.get(1).orElse("");
052         var key = args.get(2).orElse("");
053         if (eq(option, "get")) {
054            // config get <key>
055            if (size == 3) {
056               var val = conf.get(key).orElse(null);
057               if (nn(val))
058                  out.println(val);
059               else
060                  out.println(mb.getString("KeyNotFound", key));
061            } else {
062               out.println(mb.getString("TooManyArguments"));
063            }
064         } else if (eq(option, "set")) {
065            // config set <key> <value>
066            if (size == 4) {
067               conf.set(key, args.get(3).orElse(null));
068               out.println(mb.getString("ConfigSet"));
069            } else if (size < 4) {
070               out.println(mb.getString("InvalidArguments"));
071            } else {
072               out.println(mb.getString("TooManyArguments"));
073            }
074         } else if (eq(option, "remove")) {
075            // config remove <key>
076            if (size == 3) {
077               if (conf.get(key).isPresent()) {
078                  conf.remove(key);
079                  out.println(mb.getString("ConfigRemove", key));
080               } else {
081                  out.println(mb.getString("KeyNotFound", key));
082               }
083            } else {
084               out.println(mb.getString("TooManyArguments"));
085            }
086         } else {
087            out.println(mb.getString("InvalidArguments"));
088         }
089      } else {
090         out.println(mb.getString("InvalidArguments"));
091      }
092      return false;
093   }
094
095   @Override /* Overridden from ConsoleCommand */
096   public String getDescription() { return mb.getString("description"); }
097
098   @Override /* Overridden from ConsoleCommand */
099   public String getInfo() { return mb.getString("info"); }
100
101   @Override /* Overridden from ConsoleCommand */
102   public String getName() { return "config"; }
103
104   @Override /* Overridden from ConsoleCommand */
105   public String getSynopsis() { return "config [get|set]"; }
106}