mirror of
https://codeberg.org/PGPainless/vks-java.git
synced 2025-09-14 13:49:40 +02:00
Add vks-java-cli Command Line Frontend
This commit is contained in:
parent
352f6d3c7f
commit
181af197b5
7 changed files with 313 additions and 1 deletions
74
vks-java-cli/src/main/java/pgp/vks/client/cli/GetCmd.java
Normal file
74
vks-java-cli/src/main/java/pgp/vks/client/cli/GetCmd.java
Normal file
|
@ -0,0 +1,74 @@
|
|||
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package pgp.vks.client.cli;
|
||||
|
||||
import pgp.vks.client.Get;
|
||||
import pgp.vks.client.VKS;
|
||||
import picocli.CommandLine;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
|
||||
@CommandLine.Command(name = "get", description = "Retrieve an OpenPGP certificate from the key server")
|
||||
public class GetCmd implements Runnable {
|
||||
|
||||
@CommandLine.Mixin
|
||||
VKSCLI.KeyServerMixin keyServerMixin;
|
||||
|
||||
@CommandLine.ArgGroup(exclusive = true, multiplicity = "1")
|
||||
Exclusive by;
|
||||
|
||||
static class Exclusive {
|
||||
@CommandLine.Option(names = {"-f", "--by-fingerprint"}, description = "Retrieve a key by its fingerprint (NOT prefixed with '0x')")
|
||||
String fingerprint;
|
||||
|
||||
@CommandLine.Option(names = {"-i", "--by-keyid"}, description = "Retrieve a key by its decimal key ID or that of one of its subkeys.")
|
||||
Long keyId;
|
||||
|
||||
@CommandLine.Option(names = {"-e", "--by-email"}, description = "Retrieve a key by email address.")
|
||||
String email;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
VKS vks;
|
||||
try {
|
||||
vks = keyServerMixin.parent.getApi();
|
||||
} catch (MalformedURLException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
|
||||
Get get = vks.get();
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
if (by.fingerprint != null) {
|
||||
inputStream = get.byFingerprint(by.fingerprint);
|
||||
} else if (by.keyId != null) {
|
||||
inputStream = get.byKeyId(by.keyId);
|
||||
} else if (by.email != null) {
|
||||
inputStream = get.byEmail(by.email);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Missing --by-* option.");
|
||||
}
|
||||
|
||||
int read;
|
||||
byte[] buf = new byte[4096];
|
||||
while ((read = inputStream.read(buf)) != -1) {
|
||||
System.out.write(buf, 0, read);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue