From 3a286d374889b9da12489a5e7d450ee1122573c7 Mon Sep 17 00:00:00 2001 From: Greg Schueler Date: Mon, 14 Jun 2021 12:18:26 -0700 Subject: [PATCH] Demo using picocli --- README.md | 36 +++++++++ build.gradle | 24 +++--- .../java/org/rundeck/cli/demo/ext/Pico.java | 79 +++++++++++++++++++ ...k.client.tool.extension.RdCommandExtension | 1 + 4 files changed, 131 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/rundeck/cli/demo/ext/Pico.java diff --git a/README.md b/README.md index 3708d9f..fabe0ee 100644 --- a/README.md +++ b/README.md @@ -27,3 +27,39 @@ You can try this extension after building it using `rd` version 1.2 or later: scripting2 - demonstrates typed data output for yaml/json scripting Use "rd ext demo [command] help" to get help on any command. + +# Picocli + +**requires rd version 1.2.5 or later** + +The Pico command demonstrates using Picocli to handle command argument and subcommand parsing + +The build uses the 'shadow' gradle plugin to bundle Picocli into the extension jar so that it can be used standalone. + +``` +$ RD_EXT_DIR=$PWD/build/libs rd ext +A command was expected: rd ext [command] + +ext: a test subcommand + +Available commands: + + demo - subcommand demo + pico - Demo use of picocli. + +Use "rd ext [command] help" to get help on any command. +$ RD_EXT_DIR=$PWD/build/libs rd ext ext pico +What is your name? use --name . Or use wave to just wave at things +$ RD_EXT_DIR=$PWD/build/libs rd ext pico -n bob +Name is bob +$ RD_EXT_DIR=$PWD/build/libs rd ext pico wave +Missing required option '--thingies=' +Usage: pico wave -t= [-t=]... +wazzup + -t, --thingies= + +$ RD_EXT_DIR=$PWD/build/libs rd ext pico wave -t dirt -t sky +*waves at* +* dirt +* sky +``` diff --git a/build.gradle b/build.gradle index 4f3643e..0aef599 100644 --- a/build.gradle +++ b/build.gradle @@ -3,6 +3,9 @@ plugins { id 'java-library' id 'groovy' id 'idea' + id 'com.github.johnrengelman.shadow' version '5.2.0' + id "maven-publish" + id "nebula.maven-shadow-publish" version "17.2.1" } group 'org.rundeck.cli.demo.ext' @@ -15,23 +18,26 @@ repositories { mavenCentral() } -ext.versions=[ - toolbelt:'0.2.28', - rd:'1.3.9' +ext.versions = [ + toolbelt: '0.2.28', + rd : '1.3.9', + picocli : '4.2.0' ] +assemble.dependsOn(shadowJar) dependencies { compileOnly "org.projectlombok:lombok:1.18.12" annotationProcessor "org.projectlombok:lombok:1.18.12" - api "org.rundeck.cli:rd-cli-lib:${versions.rd}" - api "org.rundeck.cli-toolbelt:toolbelt-jewelcli:$versions.toolbelt" - implementation "org.rundeck.api:rd-api-client:${versions.rd}" + shadow "org.rundeck.cli:rd-cli-lib:${versions.rd}" + shadow "org.rundeck.cli-toolbelt:toolbelt-jewelcli:${versions.toolbelt}" + shadow "org.rundeck.api:rd-api-client:${versions.rd}" - implementation 'com.squareup.retrofit2:retrofit:2.7.1' - implementation 'com.squareup.retrofit2:converter-jackson:2.7.1' - implementation 'com.squareup.retrofit2:converter-simplexml:2.7.1' + implementation "info.picocli:picocli:${versions.picocli}" + shadow 'com.squareup.retrofit2:retrofit:2.7.1' + shadow 'com.squareup.retrofit2:converter-jackson:2.7.1' + shadow 'com.squareup.retrofit2:converter-simplexml:2.7.1' testImplementation "org.rundeck.cli:rd-cli-lib:${versions.rd}" diff --git a/src/main/java/org/rundeck/cli/demo/ext/Pico.java b/src/main/java/org/rundeck/cli/demo/ext/Pico.java new file mode 100644 index 0000000..ceecb37 --- /dev/null +++ b/src/main/java/org/rundeck/cli/demo/ext/Pico.java @@ -0,0 +1,79 @@ +package org.rundeck.cli.demo.ext; + +import lombok.Setter; +import org.rundeck.client.tool.extension.RdCommandExtension; +import org.rundeck.client.tool.extension.RdTool; +import org.rundeck.toolbelt.SubCommand; +import org.rundeck.toolbelt.ToolBelt; +import picocli.CommandLine; + +import java.util.List; +import java.util.concurrent.Callable; + +// using picocli @Command to define picocli interface +@CommandLine.Command( + name = "pico", + mixinStandardHelpOptions = true, + version = "0.1-SNAPSHOT", + description = "@|bold,underline Demo|@ use of @|fg(red) picocli|@.", + subcommands = { + Pico.Wave.class + } +) +@SubCommand(path = {"ext"}) +public class Pico + implements RdCommandExtension, ToolBelt.CommandInvoker, Callable +{ + @Setter RdTool rdTool; + + @CommandLine.Option(names = {"-n", "--name"}) + String name; + + @Override + public Integer call() throws Exception { + if (name != null) { + System.out.println("Name is " + name); + } else { + System.out.println("What is your name? use --name . Or use wave to just wave at things"); + } + return 0; + } + + /** + * Handle the argument list by invoking Picocli + * + * @param args + */ + @Override + public boolean run(final String[] args) { + return new CommandLine(this).execute(args) == 0; + } + + @Override + public String getDescription() { + return new CommandLine(this).getHelp().description(); + } + + @Override + public void getHelp() { + new CommandLine(this).usage(new CommandLine(this).getOut()); + } + + @CommandLine.Command(name = "wave", description = "wazzup") + static class Wave + implements Callable + + { + @CommandLine.Option(names = {"--thingies", "-t"}, required = true) + List thingies; + + @Override + public Integer call() throws Exception { + System.out.println("*waves at*"); + if (null != thingies) { + thingies.forEach(a -> System.out.printf("* %s%n", a)); + } + return 0; + } + } +} diff --git a/src/main/resources/META-INF/services/org.rundeck.client.tool.extension.RdCommandExtension b/src/main/resources/META-INF/services/org.rundeck.client.tool.extension.RdCommandExtension index 9c56803..51cc036 100644 --- a/src/main/resources/META-INF/services/org.rundeck.client.tool.extension.RdCommandExtension +++ b/src/main/resources/META-INF/services/org.rundeck.client.tool.extension.RdCommandExtension @@ -1 +1,2 @@ org.rundeck.cli.demo.ext.MyCommand +org.rundeck.cli.demo.ext.Pico