Removing plugin that isn't installed shouldn't trigger usage information

The usage information for `elasticsearch-plugin` is quiet verbose and makes the
actual error message that is shown when trying to remove an non-existing plugin
hard to spot. This changes the error code to not trigger printing the usage
information.

Closes #21250
This commit is contained in:
Christoph Büscher 2016-11-02 11:55:28 +01:00
parent d53e1d213f
commit f4594d4302
2 changed files with 34 additions and 13 deletions

View File

@ -19,6 +19,19 @@
package org.elasticsearch.plugins;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.apache.lucene.util.IOUtils;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.SettingCommand;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.node.internal.InternalSettingsPreparer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
@ -26,18 +39,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.apache.lucene.util.IOUtils;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.SettingCommand;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.node.internal.InternalSettingsPreparer;
import static org.elasticsearch.cli.Terminal.Verbosity.VERBOSE;
/**
@ -67,7 +68,7 @@ final class RemovePluginCommand extends SettingCommand {
final Path pluginDir = env.pluginsFile().resolve(pluginName);
if (Files.exists(pluginDir) == false) {
throw new UserException(
ExitCodes.USAGE,
ExitCodes.CONFIG,
"plugin " + pluginName + " not found; run 'elasticsearch-plugin list' to get list of installed plugins");
}

View File

@ -20,6 +20,7 @@
package org.elasticsearch.plugins;
import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.MockTerminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.settings.Settings;
@ -27,7 +28,9 @@ import org.elasticsearch.env.Environment;
import org.elasticsearch.test.ESTestCase;
import org.junit.Before;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
@ -43,6 +46,7 @@ public class RemovePluginCommandTests extends ESTestCase {
private Path home;
private Environment env;
@Override
@Before
public void setUp() throws Exception {
super.setUp();
@ -130,8 +134,24 @@ public class RemovePluginCommandTests extends ESTestCase {
assertThat(terminal.getOutput(), not(containsString(expectedConfigDirPreservedMessage(configDir))));
}
public void testRemoveUninstalledPluginErrors() throws Exception {
UserException e = expectThrows(UserException.class, () -> removePlugin("fake", home));
assertEquals(ExitCodes.CONFIG, e.exitCode);
assertEquals("plugin fake not found; run 'elasticsearch-plugin list' to get list of installed plugins", e.getMessage());
MockTerminal terminal = new MockTerminal();
new RemovePluginCommand().main(new String[] { "-Epath.home=" + home, "fake" }, terminal);
try (BufferedReader reader = new BufferedReader(new StringReader(terminal.getOutput()))) {
assertEquals("-> Removing fake...", reader.readLine());
assertEquals("ERROR: plugin fake not found; run 'elasticsearch-plugin list' to get list of installed plugins",
reader.readLine());
assertNull(reader.readLine());
}
}
private String expectedConfigDirPreservedMessage(final Path configDir) {
return "-> Preserving plugin config files [" + configDir + "] in case of upgrade, delete manually if not needed";
}
}