Add WIP status command

This commit is contained in:
Paul Schaub 2025-09-04 13:16:57 +02:00
parent e0952aaf60
commit 4c58775f17
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 119 additions and 13 deletions

View file

@ -5,19 +5,7 @@ import org.bouncycastle.openpgp.api.OpenPGPApi;
import org.bouncycastle.openpgp.api.bc.BcOpenPGPApi;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.pgpainless.bouncycastle.sop.operation.BCArmor;
import org.pgpainless.bouncycastle.sop.operation.BCDearmor;
import org.pgpainless.bouncycastle.sop.operation.BCDecrypt;
import org.pgpainless.bouncycastle.sop.operation.BCDetachedSign;
import org.pgpainless.bouncycastle.sop.operation.BCDetachedVerify;
import org.pgpainless.bouncycastle.sop.operation.BCEncrypt;
import org.pgpainless.bouncycastle.sop.operation.BCExtractCert;
import org.pgpainless.bouncycastle.sop.operation.BCGenerateKey;
import org.pgpainless.bouncycastle.sop.operation.BCInlineSign;
import org.pgpainless.bouncycastle.sop.operation.BCInlineVerify;
import org.pgpainless.bouncycastle.sop.operation.BCListProfiles;
import org.pgpainless.bouncycastle.sop.operation.BCMergeCerts;
import org.pgpainless.bouncycastle.sop.operation.BCVersion;
import org.pgpainless.bouncycastle.sop.operation.*;
import sop.SOP;
import sop.exception.SOPGPException;
import sop.operation.Armor;
@ -166,4 +154,8 @@ public class BouncyCastleSOP implements SOP {
public ValidateUserId validateUserId() {
throw new SOPGPException.UnsupportedSubcommand("validate-userid is not implemented.");
}
public BCStatus status() {
return new BCStatus(api);
}
}

View file

@ -0,0 +1,54 @@
package org.pgpainless.bouncycastle.sop.operation;
import org.bouncycastle.openpgp.api.OpenPGPApi;
import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.jetbrains.annotations.NotNull;
import sop.Ready;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Date;
public class BCStatus extends AbstractBCOperation {
private boolean json;
private Date time = null;
public BCStatus(OpenPGPApi api) {
super(api);
}
public BCStatus json(boolean json) {
this.json = json;
return this;
}
public BCStatus time(Date time) {
this.time = time;
return this;
}
public Ready cert(InputStream in) {
return new Ready() {
@Override
public void writeTo(@NotNull OutputStream outputStream) throws IOException {
OpenPGPCertificate cert = api.readKeyOrCertificate().parseCertificateOrKey(in);
OpenPGPCertificate.CertStatus status;
if (time == null) {
status = cert.getStatus();
} else {
status = cert.getStatus(time);
}
if (json) {
outputStream.write(status.toJsonString().getBytes(StandardCharsets.UTF_8));
} else {
outputStream.write(status.toString().getBytes(StandardCharsets.UTF_8));
}
}
};
}
}

View file

@ -0,0 +1,58 @@
package org.pgpainless;
import org.pgpainless.bouncycastle.sop.BouncyCastleSOP;
import org.pgpainless.bouncycastle.sop.operation.BCStatus;
import picocli.CommandLine;
import sop.Ready;
import sop.cli.picocli.SopCLI;
import sop.cli.picocli.commands.AbstractSopCmd;
import sop.exception.SOPGPException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
@CommandLine.Command(name = "status",
exitCodeOnInvalidInput = SOPGPException.UnsupportedOption.EXIT_CODE)
public class BCStatusCmd extends AbstractSopCmd {
@CommandLine.Parameters(arity = "0..1")
public String fileName;
@CommandLine.Option(names = {"--json"})
public boolean json = false;
@CommandLine.Option(names = {"--time"},
paramLabel = "UTC_TIME")
Date time = null;
@Override
public void run() {
BCStatus status = throwIfUnsupportedSubcommand(
((BouncyCastleSOP) SopCLI.getSop()).status(), "status");
InputStream in;
if (fileName != null) {
try {
in = getInput(fileName);
} catch (FileNotFoundException e) {
throw new SOPGPException.MissingInput("Could not find file " + fileName, e);
} catch (IOException e) {
throw new SOPGPException.BadData(e);
}
} else {
in = System.in;
}
Ready ready = status.json(json)
.time(time)
.cert(in);
try {
ready.writeTo(System.out);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View file

@ -1,6 +1,7 @@
package org.pgpainless;
import org.pgpainless.bouncycastle.sop.BouncyCastleSOP;
import org.pgpainless.bouncycastle.sop.operation.BCStatus;
import picocli.CommandLine;
import sop.cli.picocli.SOPExceptionExitCodeMapper;
import sop.cli.picocli.SOPExecutionExceptionHandler;
@ -22,6 +23,7 @@ public class BcSopCLI {
.parseArgs(args);
CommandLine cmd = new CommandLine(SopCLI.class);
cmd.addSubcommand("status", new BCStatusCmd());
cmd.getSubcommands().get("generate-completion").getCommandSpec().usageMessage().hidden(true);
cmd.setCommandName("bcsop");
cmd.setExecutionExceptionHandler(new SOPExecutionExceptionHandler());