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 '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/JuneauMicroserviceCoreBasics">juneau-microservice-core Basics</a> 033 * </ul> 034 */ 035public class ConfigCommand extends ConsoleCommand { 036 037 private final Messages mb = Messages.of(ConfigCommand.class, "Messages"); 038 039 @Override /* Overridden from ConsoleCommand */ 040 public boolean execute(Scanner in, PrintWriter out, Args args) { 041 var conf = Microservice.getInstance().getConfig(); 042 if (args.size() > 2) { 043 var option = args.getArg(1); 044 var key = args.getArg(2); 045 if (option.equals("get")) { 046 // config get <key> 047 if (args.size() == 3) { 048 var val = conf.get(key).orElse(null); 049 if (nn(val)) 050 out.println(val); 051 else 052 out.println(mb.getString("KeyNotFound", key)); 053 } else { 054 out.println(mb.getString("TooManyArguments")); 055 } 056 } else if (option.equals("set")) { 057 // config set <key> <value> 058 if (args.size() == 4) { 059 conf.set(key, args.getArg(3)); 060 out.println(mb.getString("ConfigSet")); 061 } else if (args.size() < 4) { 062 out.println(mb.getString("InvalidArguments")); 063 } else { 064 out.println(mb.getString("TooManyArguments")); 065 } 066 } else if (option.equals("remove")) { 067 // config remove <key> 068 if (args.size() == 3) { 069 if (conf.get(key).isPresent()) { 070 conf.remove(key); 071 out.println(mb.getString("ConfigRemove", key)); 072 } else { 073 out.println(mb.getString("KeyNotFound", key)); 074 } 075 } else { 076 out.println(mb.getString("TooManyArguments")); 077 } 078 } else { 079 out.println(mb.getString("InvalidArguments")); 080 } 081 } else { 082 out.println(mb.getString("InvalidArguments")); 083 } 084 return false; 085 } 086 087 @Override /* Overridden from ConsoleCommand */ 088 public String getDescription() { return mb.getString("description"); } 089 090 @Override /* Overridden from ConsoleCommand */ 091 public String getInfo() { return mb.getString("info"); } 092 093 @Override /* Overridden from ConsoleCommand */ 094 public String getName() { return "config"; } 095 096 @Override /* Overridden from ConsoleCommand */ 097 public String getSynopsis() { return "config [get|set]"; } 098}