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