From fca9a8ef919eea5cb2ec06de33bcece37b7fb811 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 4 Jul 2022 13:19:36 +0200 Subject: [PATCH] Add Export command --- .../main/java/pgp/cert_d/cli/PGPCertDCli.java | 2 + .../java/pgp/cert_d/cli/commands/Export.java | 40 +++++++++++++++++++ .../java/pgp/cert_d/cli/commands/Import.java | 2 +- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 pgpainless-cert-d-cli/src/main/java/pgp/cert_d/cli/commands/Export.java diff --git a/pgpainless-cert-d-cli/src/main/java/pgp/cert_d/cli/PGPCertDCli.java b/pgpainless-cert-d-cli/src/main/java/pgp/cert_d/cli/PGPCertDCli.java index a45a969..fe0f1b0 100644 --- a/pgpainless-cert-d-cli/src/main/java/pgp/cert_d/cli/PGPCertDCli.java +++ b/pgpainless-cert-d-cli/src/main/java/pgp/cert_d/cli/PGPCertDCli.java @@ -8,6 +8,7 @@ import org.pgpainless.certificate_store.CertificateReader; import org.pgpainless.certificate_store.SharedPGPCertificateDirectoryAdapter; import pgp.cert_d.BaseDirectoryProvider; import pgp.cert_d.SharedPGPCertificateDirectoryImpl; +import pgp.cert_d.cli.commands.Export; import pgp.cert_d.cli.commands.Get; import pgp.cert_d.cli.commands.Insert; import pgp.cert_d.cli.commands.Import; @@ -25,6 +26,7 @@ import java.sql.SQLException; name = "certificate-store", description = "Store and manage public OpenPGP certificates", subcommands = { + Export.class, Insert.class, Import.class, Get.class, diff --git a/pgpainless-cert-d-cli/src/main/java/pgp/cert_d/cli/commands/Export.java b/pgpainless-cert-d-cli/src/main/java/pgp/cert_d/cli/commands/Export.java new file mode 100644 index 0000000..4e61bc2 --- /dev/null +++ b/pgpainless-cert-d-cli/src/main/java/pgp/cert_d/cli/commands/Export.java @@ -0,0 +1,40 @@ +// SPDX-FileCopyrightText: 2022 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package pgp.cert_d.cli.commands; + +import org.bouncycastle.util.io.Streams; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pgp.cert_d.cli.PGPCertDCli; +import pgp.certificate_store.Certificate; +import picocli.CommandLine; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Iterator; + +@CommandLine.Command(name = "export", + description = "Export all certificates in the store to Standard Output") +public class Export implements Runnable { + + private static final Logger LOGGER = LoggerFactory.getLogger(Get.class); + + @Override + public void run() { + Iterator certificates = PGPCertDCli.getCertificateDirectory() + .getCertificates(); + while (certificates.hasNext()) { + try { + Certificate certificate = certificates.next(); + InputStream inputStream = certificate.getInputStream(); + Streams.pipeAll(inputStream, System.out); + inputStream.close(); + } catch (IOException e) { + LOGGER.error("IO Error", e); + System.exit(-1); + } + } + } +} diff --git a/pgpainless-cert-d-cli/src/main/java/pgp/cert_d/cli/commands/Import.java b/pgpainless-cert-d-cli/src/main/java/pgp/cert_d/cli/commands/Import.java index 4c2a484..a62d11a 100644 --- a/pgpainless-cert-d-cli/src/main/java/pgp/cert_d/cli/commands/Import.java +++ b/pgpainless-cert-d-cli/src/main/java/pgp/cert_d/cli/commands/Import.java @@ -20,7 +20,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; @CommandLine.Command(name = "import", - description = "Import certificates into the store from stdin") + description = "Import certificates into the store from Standard Input") public class Import implements Runnable { private static final Logger LOGGER = LoggerFactory.getLogger(Import.class);