|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.hadoop.ozone.shell; |
| 19 | + |
| 20 | +import org.apache.hadoop.hdds.cli.GenericCli; |
| 21 | +import picocli.CommandLine; |
| 22 | +import picocli.CommandLine.Command; |
| 23 | +import picocli.shell.jline3.PicocliCommands.PicocliCommandsFactory; |
| 24 | + |
| 25 | +/** |
| 26 | + * Interactive Shell for all Ozone commands. |
| 27 | + */ |
| 28 | +public final class OzoneInteractiveShell { |
| 29 | + |
| 30 | + private OzoneInteractiveShell() { |
| 31 | + } |
| 32 | + |
| 33 | + public static void main(String[] argv) throws Exception { |
| 34 | + PicocliCommandsFactory factory = new PicocliCommandsFactory(); |
| 35 | + CommandLine topCmd = new CommandLine(new TopCommand(), factory); |
| 36 | + |
| 37 | + // Add known subcommands statically if they are in the same module. |
| 38 | + topCmd.addSubcommand("sh", new OzoneShell().getCmd()); |
| 39 | + topCmd.addSubcommand("tenant", new org.apache.hadoop.ozone.shell.tenant.TenantShell().getCmd()); |
| 40 | + topCmd.addSubcommand("s3", new org.apache.hadoop.ozone.shell.s3.S3Shell().getCmd()); |
| 41 | + |
| 42 | + // Dynamically add subcommands from other modules to avoid circular dependencies. |
| 43 | + addDynamicSubcommand(topCmd, "admin", "org.apache.hadoop.ozone.admin.OzoneAdmin"); |
| 44 | + addDynamicSubcommand(topCmd, "debug", "org.apache.hadoop.ozone.debug.OzoneDebug"); |
| 45 | + addDynamicSubcommand(topCmd, "fs", "org.apache.hadoop.fs.ozone.OzoneFsShell"); |
| 46 | + addDynamicSubcommand(topCmd, "repair", "org.apache.hadoop.ozone.repair.OzoneRepair"); |
| 47 | + |
| 48 | + Shell dummyShell = new Shell() { |
| 49 | + @Override |
| 50 | + public String name() { |
| 51 | + return "ozone"; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String prompt() { |
| 56 | + return "ozone"; |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + new REPL(dummyShell, topCmd, factory, null); |
| 61 | + } |
| 62 | + |
| 63 | + private static void addDynamicSubcommand(CommandLine topCmd, String name, String className) { |
| 64 | + try { |
| 65 | + Class<?> clazz = Class.forName(className); |
| 66 | + GenericCli instance = (GenericCli) clazz.getDeclaredConstructor().newInstance(); |
| 67 | + topCmd.addSubcommand(name, instance.getCmd()); |
| 68 | + } catch (Exception e) { |
| 69 | + // Ignore if the class is not present in the classpath |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Command(name = "ozone", description = "Interactive Shell for all Ozone commands", mixinStandardHelpOptions = true) |
| 74 | + private static class TopCommand implements Runnable { |
| 75 | + @Override |
| 76 | + public void run() { |
| 77 | + // Top level command doesn't do anything by itself in REPL |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments