Skip to content

Commit

Permalink
chore(deps): replace jcommander with picocli
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Burgazzoli <lburgazzoli@gmail.com>
  • Loading branch information
lburgazzoli committed Jun 10, 2023
1 parent bdf5c17 commit a203276
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 113 deletions.
6 changes: 2 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ awaitility = "4.2.0"
commonsIo = "2.13.0"
commonCompress = "1.23.0"
autoService = "1.1.0"
jool = "0.9.15"
jcommander = "1.82"
errorprone = "2.19.1"
vertx = "4.4.3"
picocli = "4.7.4"

versionsPlugin = "0.46.0"
errorPronePlugin = "3.0.1"
Expand All @@ -37,8 +36,7 @@ testcontainers = { module = "org.testcontainers:testcontainers", version.ref = "
protoc = { module = "com.google.protobuf:protoc", version.ref = "protoc" }
failsafe = { module = "net.jodah:failsafe", version.ref = "failsafe" }
awaitility = { module = "org.awaitility:awaitility", version.ref = "awaitility" }
jcommander = { module = "com.beust:jcommander", version.ref = "jcommander"}
jool = { module = "org.jooq:jool-java-8", version.ref = "jool"}
picocli = { module = "info.picocli:picocli", version.ref = "picocli"}

commonsIo = { module = "commons-io:commons-io", version.ref = "commonsIo" }
commonsCompress = { module = "org.apache.commons:commons-compress", version.ref = "commonCompress" }
Expand Down
5 changes: 3 additions & 2 deletions jetcd-ctl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ project.tasks.publish.enabled = false

dependencies {
implementation project(':jetcd-core')
implementation libs.jool
implementation libs.jcommander
implementation libs.picocli

runtimeOnly libs.bundles.log4j
}
Expand All @@ -37,6 +36,8 @@ shadowJar {
archiveBaseName.set('jetcdctl')
archiveClassifier.set('')
archiveVersion.set('')

mergeServiceFiles()
}

application {
Expand Down
44 changes: 25 additions & 19 deletions jetcd-ctl/src/main/java/io/etcd/jetcd/examples/ctl/CommandGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.etcd.jetcd.examples.ctl;

import org.jooq.lambda.fi.util.function.CheckedConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -25,32 +24,39 @@
import io.etcd.jetcd.kv.GetResponse;
import io.etcd.jetcd.options.GetOption;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import picocli.CommandLine;

import static com.google.common.base.Charsets.UTF_8;

@Parameters(separators = "=", commandDescription = "Gets the key")
class CommandGet implements CheckedConsumer<Client> {
@CommandLine.Command(name = "get", description = "Gets the key")
class CommandGet implements Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(CommandGet.class);

@Parameter(arity = 1, description = "<key>")
String key = "";
@CommandLine.ParentCommand
Main main;

@Parameter(names = "--rev", description = "Specify the kv revision")
Long rev = 0L;
@CommandLine.Parameters(index = "0", arity = "1", description = "key")
String key;

@Override
public void accept(Client client) throws Exception {
GetResponse getResponse = client.getKVClient()
.get(ByteSequence.from(key, UTF_8), GetOption.newBuilder().withRevision(rev).build()).get();
@CommandLine.Option(names = "--rev", description = "Revision to start watching", defaultValue = "0")
Long rev;

if (getResponse.getKvs().isEmpty()) {
// key does not exist
return;
@Override
public void run() {
try (Client client = Client.builder().endpoints(main.endpoints).build()) {
GetResponse getResponse = client.getKVClient().get(
ByteSequence.from(key, UTF_8),
GetOption.newBuilder().withRevision(rev).build()).get();

if (getResponse.getKvs().isEmpty()) {
// key does not exist
return;
}

LOGGER.info(key);
LOGGER.info(getResponse.getKvs().get(0).getValue().toString(UTF_8));
} catch (Exception e) {
LOGGER.warn(e.getMessage());
}

LOGGER.info(key);
LOGGER.info(getResponse.getKvs().get(0).getValue().toString(UTF_8));
}
}
35 changes: 21 additions & 14 deletions jetcd-ctl/src/main/java/io/etcd/jetcd/examples/ctl/CommandPut.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,38 @@

package io.etcd.jetcd.examples.ctl;

import java.util.List;

import org.jooq.lambda.fi.util.function.CheckedConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Charsets;

@Parameters(separators = "=", commandDescription = "Puts the given key into the store")
class CommandPut implements CheckedConsumer<Client> {
import picocli.CommandLine;

@CommandLine.Command(name = "put", description = "Puts the given key into the store")
class CommandPut implements Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(CommandPut.class);

@Parameter(arity = 2, description = "<key> <value>")
List<String> keyValue;
@CommandLine.ParentCommand
Main main;

@Override
public void accept(Client client) throws Exception {
client.getKVClient()
.put(ByteSequence.from(keyValue.get(0), Charsets.UTF_8), ByteSequence.from(keyValue.get(1), Charsets.UTF_8)).get();
@CommandLine.Parameters(index = "0", arity = "1", description = "key")
String key;
@CommandLine.Parameters(index = "1", arity = "1", description = "value")
String val;

LOGGER.info("OK");
@Override
public void run() {
try (Client client = Client.builder().endpoints(main.endpoints).build()) {
client.getKVClient().put(
ByteSequence.from(key, Charsets.UTF_8),
ByteSequence.from(val, Charsets.UTF_8)).get();

LOGGER.info("OK");
} catch (Exception e) {
LOGGER.warn(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.function.Consumer;

import org.jooq.lambda.fi.util.function.CheckedConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -28,50 +28,55 @@
import io.etcd.jetcd.Watch.Watcher;
import io.etcd.jetcd.options.WatchOption;
import io.etcd.jetcd.watch.WatchEvent;
import io.etcd.jetcd.watch.WatchResponse;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Charsets;

@Parameters(separators = "=", commandDescription = "Watches events stream for a key")
class CommandWatch implements CheckedConsumer<Client> {
import picocli.CommandLine;

@CommandLine.Command(name = "watch", description = "Watches events stream for a key")
class CommandWatch implements Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(CommandWatch.class);

@Parameter(arity = 1, description = "<key>")
String key;
@CommandLine.ParentCommand
Main main;

@Parameter(names = "--rev", description = "Revision to start watching")
Long rev = 0L;
@CommandLine.Parameters(arity = "1", description = "<key>")
String key;

@Parameter(names = { "-m", "--max-events" }, description = "the maximum number of events to receive")
Integer maxEvents = Integer.MAX_VALUE;
@CommandLine.Option(names = "--rev", description = "Revision to start watching", defaultValue = "0")
Long rev;
@CommandLine.Option(names = { "-m", "--max-events" }, description = "the maximum number of events to receive")
Integer maxEvents;

@Override
public void accept(Client client) throws Exception {
CountDownLatch latch = new CountDownLatch(maxEvents);
Watcher watcher = null;
public void run() {
CountDownLatch latch = new CountDownLatch(maxEvents != null ? maxEvents : Integer.MAX_VALUE);

try {
try (Client client = Client.builder().endpoints(main.endpoints).build()) {
ByteSequence watchKey = ByteSequence.from(key, Charsets.UTF_8);
WatchOption watchOpts = WatchOption.newBuilder().withRevision(rev).build();
WatchOption watchOpts = WatchOption.newBuilder().withRevision(rev != null ? rev : 0).build();

watcher = client.getWatchClient().watch(watchKey, watchOpts, response -> {
Consumer<WatchResponse> consumer = response -> {
for (WatchEvent event : response.getEvents()) {
LOGGER.info("type={}, key={}, value={}", event.getEventType().toString(),
LOGGER.info("type={}, key={}, value={}",
event.getEventType().toString(),
Optional.ofNullable(event.getKeyValue().getKey()).map(bs -> bs.toString(Charsets.UTF_8)).orElse(""),
Optional.ofNullable(event.getKeyValue().getValue()).map(bs -> bs.toString(Charsets.UTF_8)).orElse(""));
}

latch.countDown();
});
};

latch.await();
} catch (Exception e) {
if (watcher != null) {
watcher.close();
try (Watcher ignored = client.getWatchClient().watch(watchKey, watchOpts, consumer)) {
// close the watcher
}

throw e;
latch.await();

LOGGER.info("done");
} catch (Exception e) {
LOGGER.warn(e.getMessage());
}
}
}
66 changes: 16 additions & 50 deletions jetcd-ctl/src/main/java/io/etcd/jetcd/examples/ctl/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,23 @@

package io.etcd.jetcd.examples.ctl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.etcd.jetcd.Client;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;

public class Main {
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);

public static void main(String[] args) {
Args global = new Args();
CommandGet getCmd = new CommandGet();
CommandPut putCmd = new CommandPut();
CommandWatch watchCmd = new CommandWatch();

JCommander jc = JCommander.newBuilder().addObject(global).addCommand("get", getCmd).addCommand("put", putCmd)
.addCommand("watch", watchCmd).build();

jc.parse(args);

String cmd = jc.getParsedCommand();
if (cmd == null || global.help) {
jc.usage();
return;
}

try (Client client = Client.builder().endpoints(global.endpoints.split(",")).build()) {
switch (cmd) {
case "get":
getCmd.accept(client);
break;
case "put":
putCmd.accept(client);
break;
case "watch":
watchCmd.accept(client);
break;
}
} catch (Exception e) {
LOGGER.error(cmd + " Error {}", e);
System.exit(1);
}
import picocli.CommandLine;

@CommandLine.Command(name = "jetcdctl", version = "1.0", mixinStandardHelpOptions = true, subcommands = {
CommandWatch.class,
CommandGet.class,
CommandPut.class
})
public class Main implements Runnable {
@CommandLine.Option(names = { "--endpoints" }, description = "gRPC endpoints", defaultValue = "http://127.0.0.1:2379")
String endpoints;

@Override
public void run() {
}

public static class Args {
@Parameter(names = { "--endpoints" }, description = "gRPC endpoints ")
private String endpoints = "http://127.0.0.1:2379";

@Parameter(names = { "-h", "--help" }, help = true)
private boolean help = false;
public static void main(String[] args) {
int exitCode = new CommandLine(new Main()).execute(args);
System.exit(exitCode);
}
}

0 comments on commit a203276

Please sign in to comment.