|
| 1 | +package org.hydev.mcpm.client.arguments.parsers; |
| 2 | + |
| 3 | +import net.sourceforge.argparse4j.impl.Arguments; |
| 4 | +import net.sourceforge.argparse4j.inf.Namespace; |
| 5 | +import net.sourceforge.argparse4j.inf.Subparser; |
| 6 | +import org.hydev.mcpm.client.commands.entries.UninstallController; |
| 7 | +import org.hydev.mcpm.client.injector.PluginNotFoundException; |
| 8 | +import org.hydev.mcpm.client.uninstall.UninstallResult; |
| 9 | + |
| 10 | +import java.util.function.Consumer; |
| 11 | + |
| 12 | +/** |
| 13 | + * Command parser for the uninstallation use case |
| 14 | + * |
| 15 | + * @author Anushka (https://github.com/aanushkasharma) |
| 16 | + * @since 2022-11-27 |
| 17 | + */ |
| 18 | +public record UninstallParser(UninstallController controller) implements CommandParser { |
| 19 | + @Override |
| 20 | + public String name() { |
| 21 | + return "uninstall"; |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public String description() { |
| 26 | + return "Uninstall a plugin from file system"; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void configure(Subparser parser) { |
| 31 | + parser.addArgument("name") |
| 32 | + .help("Name of the plugin to uninstall"); |
| 33 | + parser.addArgument("-r", "--recursive").action(Arguments.storeTrue()) |
| 34 | + .help("Recursively remove orphan dependencies"); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void run(Namespace details, Consumer<String> log) |
| 39 | + { |
| 40 | + var name = details.getString("name"); |
| 41 | + try { |
| 42 | + // Uninstall |
| 43 | + var result = controller.uninstall(name, details.getBoolean("recursive")); |
| 44 | + |
| 45 | + // Print result |
| 46 | + if (result.state() == UninstallResult.State.FAILED_TO_DELETE) { |
| 47 | + log.accept("&cFailed to delete plugin file"); |
| 48 | + } |
| 49 | + if (result.state() == UninstallResult.State.SUCCESS) { |
| 50 | + log.accept("&aPlugin " + name + " uninstalled successfully!"); |
| 51 | + } |
| 52 | + } |
| 53 | + catch (PluginNotFoundException e) { |
| 54 | + log.accept("&cPlugin of the name " + name + " is not found"); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments