-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31198 from iocanel/cli-extensions
Introduce CLI plugins
- Loading branch information
Showing
49 changed files
with
3,702 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.quarkus.cli; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import java.util.stream.Collectors; | ||
|
||
import io.quarkus.cli.common.OutputOptionMixin; | ||
import io.quarkus.cli.plugin.CliPluginsAdd; | ||
import io.quarkus.cli.plugin.CliPluginsList; | ||
import io.quarkus.cli.plugin.CliPluginsRemove; | ||
import io.quarkus.cli.plugin.CliPluginsSync; | ||
import picocli.CommandLine; | ||
import picocli.CommandLine.ParseResult; | ||
import picocli.CommandLine.Unmatched; | ||
|
||
@CommandLine.Command(name = "plugin", aliases = { "plug" }, header = "Configure plugins of the Quarkus CLI.", subcommands = { | ||
CliPluginsList.class, | ||
CliPluginsAdd.class, | ||
CliPluginsRemove.class, | ||
CliPluginsSync.class | ||
}) | ||
public class CliPlugins implements Callable<Integer> { | ||
|
||
@CommandLine.Mixin(name = "output") | ||
protected OutputOptionMixin output; | ||
|
||
@CommandLine.Spec | ||
protected CommandLine.Model.CommandSpec spec; | ||
|
||
@Unmatched // avoids throwing errors for unmatched arguments | ||
List<String> unmatchedArgs; | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
output.info("Listing plguins (default action, see --help)."); | ||
ParseResult result = spec.commandLine().getParseResult(); | ||
List<String> args = result.originalArgs().stream().filter(x -> !"plugin".equals(x) && !"plug".equals(x)) | ||
.collect(Collectors.toList()); | ||
CommandLine listCommand = spec.subcommands().get("list"); | ||
return listCommand.execute(args.toArray(new String[0])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
devtools/cli/src/main/java/io/quarkus/cli/plugin/CliPluginsAdd.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package io.quarkus.cli.plugin; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.TreeMap; | ||
import java.util.concurrent.Callable; | ||
|
||
import io.quarkus.cli.common.RunModeOption; | ||
import picocli.CommandLine; | ||
|
||
@CommandLine.Command(name = "add", header = "Add plugin(s) to the Quarkus CLI.") | ||
public class CliPluginsAdd extends CliPluginsBase implements Callable<Integer> { | ||
|
||
@CommandLine.Mixin | ||
RunModeOption runMode; | ||
|
||
@CommandLine.Option(names = { "-d", | ||
"--description" }, paramLabel = "Plugin description", order = 5, description = "The plugin description") | ||
Optional<String> description; | ||
|
||
@CommandLine.Parameters(arity = "1", paramLabel = "PLUGIN_NAME", description = " The plugin name or location (e.g. url, path or maven coordinates in GACTV form)") | ||
String nameOrLocation; | ||
|
||
@Override | ||
public Integer call() { | ||
try { | ||
output.debug("Add plugin with initial parameters: %s", this); | ||
output.throwIfUnmatchedArguments(spec.commandLine()); | ||
|
||
if (runMode.isDryRun()) { | ||
dryRunAdd(spec.commandLine().getHelp()); | ||
return CommandLine.ExitCode.OK; | ||
} | ||
|
||
return addPlugin(); | ||
} catch (Exception e) { | ||
return output.handleCommandException(e, | ||
"Unable to add plugin(s): " + nameOrLocation + " of type: " + type.map(PluginType::name).orElse("<any>") | ||
+ "." | ||
+ e.getMessage()); | ||
} | ||
} | ||
|
||
Integer addPlugin() throws IOException { | ||
PluginManager pluginManager = pluginManager(); | ||
Optional<Plugin> addedPlugin = pluginManager.addPlugin(nameOrLocation, description); | ||
|
||
return addedPlugin.map(plugin -> { | ||
PluginListTable table = new PluginListTable(List.of(new PluginListItem(true, plugin)), false); | ||
output.info("Added plugin:"); | ||
output.info(table.getContent()); | ||
return CommandLine.ExitCode.OK; | ||
}).orElseGet(() -> { | ||
output.error("No plugin available at: " + this.nameOrLocation); | ||
printHints(true); | ||
return CommandLine.ExitCode.USAGE; | ||
}); | ||
} | ||
|
||
private void printHints(boolean pluginListHint) { | ||
if (runMode.isBatchMode()) | ||
return; | ||
|
||
if (pluginListHint) { | ||
output.info("To see the list of installable plugins, use the 'plugin list' subcommand."); | ||
} | ||
} | ||
|
||
void dryRunAdd(CommandLine.Help help) { | ||
output.printText(new String[] { | ||
"\nAdd plugin to the CLI\n", | ||
"\t" + projectRoot().toString() | ||
}); | ||
Map<String, String> dryRunOutput = new TreeMap<>(); | ||
dryRunOutput.put("Name or Location", nameOrLocation); | ||
type.ifPresent(t -> dryRunOutput.put("Type", t.name())); | ||
output.info(help.createTextTable(dryRunOutput).toString()); | ||
}; | ||
} |
Oops, something went wrong.