This is an automated email from the ASF dual-hosted git repository. nicoloboschi pushed a commit to branch branch-2.11 in repository https://gitbox.apache.org/repos/asf/pulsar.git
commit 62978ae7f323c54e262dd23d62244b07fc2667cf Author: Nicolò Boschi <[email protected]> AuthorDate: Fri Nov 25 09:46:46 2022 +0100 [improve][cli] Pulsar shell: allow cloning an existing config (#18608) (cherry picked from commit 3d643437026ecbb566041e94ab6a7bfc9426136c) --- .../java/org/apache/pulsar/shell/ConfigShell.java | 57 +++++++++++++++++++--- .../org/apache/pulsar/shell/ConfigShellTest.java | 10 ++++ 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/shell/ConfigShell.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/shell/ConfigShell.java index 72710498017..c04918dec0b 100644 --- a/pulsar-client-tools/src/main/java/org/apache/pulsar/shell/ConfigShell.java +++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/shell/ConfigShell.java @@ -112,6 +112,7 @@ public class ConfigShell implements ShellCommandsProvider { commands.put("list", new CmdConfigList()); commands.put("create", new CmdConfigCreate()); + commands.put("clone", new CmdConfigClone()); commands.put("update", new CmdConfigUpdate()); commands.put("delete", new CmdConfigDelete()); commands.put("use", new CmdConfigUse()); @@ -256,6 +257,9 @@ public class ConfigShell implements ShellCommandsProvider { @Parameters(commandDescription = "Create a new configuration.") private class CmdConfigCreate extends CmdConfigPut { + @Parameter(description = "Configuration name", required = true) + protected String name; + @Override @SneakyThrows boolean verifyCondition() { @@ -266,11 +270,20 @@ public class ConfigShell implements ShellCommandsProvider { } return true; } + + @Override + String name() { + return name; + } } @Parameters(commandDescription = "Update an existing configuration.") private class CmdConfigUpdate extends CmdConfigPut { + @Parameter(description = "Configuration name", required = true) + @JCommanderCompleter.ParameterCompleter(type = JCommanderCompleter.ParameterCompleter.Type.CONFIGS) + protected String name; + @Override @SneakyThrows boolean verifyCondition() { @@ -285,14 +298,15 @@ public class ConfigShell implements ShellCommandsProvider { } return true; } + + @Override + String name() { + return name; + } } private abstract class CmdConfigPut implements RunnableWithResult { - @Parameter(description = "Configuration name", required = true) - @JCommanderCompleter.ParameterCompleter(type = JCommanderCompleter.ParameterCompleter.Type.CONFIGS) - protected String name; - @Parameter(names = {"--url"}, description = "URL of the config") protected String url; @@ -309,6 +323,7 @@ public class ConfigShell implements ShellCommandsProvider { if (!verifyCondition()) { return false; } + final String name = name(); final String value; if (inlineValue != null) { if (inlineValue.startsWith("base64:")) { @@ -346,11 +361,41 @@ public class ConfigShell implements ShellCommandsProvider { return true; } - + abstract String name(); abstract boolean verifyCondition(); } + + private class CmdConfigClone implements RunnableWithResult { + + @Parameter(description = "Configuration to clone", required = true) + @JCommanderCompleter.ParameterCompleter(type = JCommanderCompleter.ParameterCompleter.Type.CONFIGS) + protected String cloneFrom; + + @Parameter(names = {"--name"}, description = "Name of the new config", required = true) + protected String newName; + + @Override + @SneakyThrows + public boolean run() { + if (DEFAULT_CONFIG.equals(newName) || configStore.getConfig(newName) != null) { + print("'" + newName + "' already exists."); + return false; + } + final ConfigStore.ConfigEntry config = configStore.getConfig(cloneFrom); + if (config == null) { + print("Config '" + config + "' does not exist."); + return false; + } + + final ConfigStore.ConfigEntry entry = new ConfigStore.ConfigEntry(newName, config.getValue()); + configStore.putConfig(entry); + reloadIfCurrent(entry); + return true; + } + } + private void reloadIfCurrent(ConfigStore.ConfigEntry entry) throws Exception { if (currentConfig.equals(entry.getName())) { final Properties properties = new Properties(); @@ -382,7 +427,7 @@ public class ConfigShell implements ShellCommandsProvider { } if (propertyValue == null) { - print("-v parameter is required. you can pass an empty value to empty the property. (-v= )"); + print("-v parameter is required. You can pass an empty value to empty the property. (-v= )"); return false; } diff --git a/pulsar-client-tools/src/test/java/org/apache/pulsar/shell/ConfigShellTest.java b/pulsar-client-tools/src/test/java/org/apache/pulsar/shell/ConfigShellTest.java index db186ff4a85..3a5a94ca5ca 100644 --- a/pulsar-client-tools/src/test/java/org/apache/pulsar/shell/ConfigShellTest.java +++ b/pulsar-client-tools/src/test/java/org/apache/pulsar/shell/ConfigShellTest.java @@ -143,6 +143,16 @@ public class ConfigShellTest { "--file", newClientConf.toFile().getAbsolutePath()})); assertTrue(output.isEmpty()); verify(pulsarShell, times(2)).reload(any()); + + assertTrue(runCommand(new String[]{"clone", "myclient", + "--name", "myclient-copied"})); + assertTrue(output.isEmpty()); + verify(pulsarShell, times(2)).reload(any()); + + assertTrue(runCommand(new String[]{"view", "myclient-copied"})); + assertEquals(output.get(0), "webServiceUrl=http://localhost:8081/\nbrokerServiceUrl" + + "=pulsar://localhost:6651/\n"); + output.clear(); } @Test
