Kotlin conversion: VerifyCmd

This commit is contained in:
Paul Schaub 2023-11-04 16:15:35 +01:00
parent 1de179c015
commit 8246359a85
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 81 additions and 102 deletions

View file

@ -1,102 +0,0 @@
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.cli.picocli.commands;
import picocli.CommandLine;
import sop.Verification;
import sop.cli.picocli.Print;
import sop.cli.picocli.SopCLI;
import sop.exception.SOPGPException;
import sop.operation.DetachedVerify;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@CommandLine.Command(name = "verify",
resourceBundle = "msg_detached-verify",
exitCodeOnInvalidInput = SOPGPException.UnsupportedOption.EXIT_CODE)
public class VerifyCmd extends AbstractSopCmd {
@CommandLine.Parameters(index = "0",
paramLabel = "SIGNATURE")
String signature;
@CommandLine.Parameters(index = "1..*",
arity = "1..*",
paramLabel = "CERT")
List<String> certificates = new ArrayList<>();
@CommandLine.Option(names = {"--not-before"},
paramLabel = "DATE")
String notBefore = "-";
@CommandLine.Option(names = {"--not-after"},
paramLabel = "DATE")
String notAfter = "now";
@Override
public void run() {
DetachedVerify detachedVerify = throwIfUnsupportedSubcommand(
SopCLI.getSop().detachedVerify(), "verify");
if (notAfter != null) {
try {
detachedVerify.notAfter(parseNotAfter(notAfter));
} catch (SOPGPException.UnsupportedOption unsupportedOption) {
String errorMsg = getMsg("sop.error.feature_support.option_not_supported", "--not-after");
throw new SOPGPException.UnsupportedOption(errorMsg, unsupportedOption);
}
}
if (notBefore != null) {
try {
detachedVerify.notBefore(parseNotBefore(notBefore));
} catch (SOPGPException.UnsupportedOption unsupportedOption) {
String errorMsg = getMsg("sop.error.feature_support.option_not_supported", "--not-before");
throw new SOPGPException.UnsupportedOption(errorMsg, unsupportedOption);
}
}
for (String certInput : certificates) {
try (InputStream certIn = getInput(certInput)) {
detachedVerify.cert(certIn);
} catch (IOException ioException) {
throw new RuntimeException(ioException);
} catch (SOPGPException.BadData badData) {
String errorMsg = getMsg("sop.error.input.not_a_certificate", certInput);
throw new SOPGPException.BadData(errorMsg, badData);
}
}
if (signature != null) {
try (InputStream sigIn = getInput(signature)) {
detachedVerify.signatures(sigIn);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (SOPGPException.BadData badData) {
String errorMsg = getMsg("sop.error.input.not_a_signature", signature);
throw new SOPGPException.BadData(errorMsg, badData);
}
}
List<Verification> verifications;
try {
verifications = detachedVerify.data(System.in);
} catch (SOPGPException.NoSignature e) {
String errorMsg = getMsg("sop.error.runtime.no_verifiable_signature_found");
throw new SOPGPException.NoSignature(errorMsg, e);
} catch (IOException ioException) {
throw new RuntimeException(ioException);
} catch (SOPGPException.BadData badData) {
String errorMsg = getMsg("sop.error.input.stdin_not_a_message");
throw new SOPGPException.BadData(errorMsg, badData);
}
for (Verification verification : verifications) {
Print.outln(verification.toString());
}
}
}

View file

@ -0,0 +1,81 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.cli.picocli.commands
import java.io.IOException
import picocli.CommandLine.*
import sop.cli.picocli.SopCLI
import sop.exception.SOPGPException.*
@Command(
name = "verify",
resourceBundle = "msg_detached-verify",
exitCodeOnInvalidInput = UnsupportedOption.EXIT_CODE)
class VerifyCmd : AbstractSopCmd() {
@Parameters(index = "0", paramLabel = "SIGNATURE") lateinit var signature: String
@Parameters(index = "1..*", arity = "1..*", paramLabel = "CERT")
lateinit var certificates: List<String>
@Option(names = ["--not-before"], paramLabel = "DATE") var notBefore: String = "-"
@Option(names = ["--not-after"], paramLabel = "DATE") var notAfter: String = "now"
override fun run() {
val detachedVerify =
throwIfUnsupportedSubcommand(SopCLI.getSop().detachedVerify(), "verify")
try {
detachedVerify.notAfter(parseNotAfter(notAfter))
} catch (unsupportedOption: UnsupportedOption) {
val errorMsg = getMsg("sop.error.feature_support.option_not_supported", "--not-after")
throw UnsupportedOption(errorMsg, unsupportedOption)
}
try {
detachedVerify.notBefore(parseNotBefore(notBefore))
} catch (unsupportedOption: UnsupportedOption) {
val errorMsg = getMsg("sop.error.feature_support.option_not_supported", "--not-before")
throw UnsupportedOption(errorMsg, unsupportedOption)
}
for (certInput in certificates) {
try {
getInput(certInput).use { certIn -> detachedVerify.cert(certIn) }
} catch (ioException: IOException) {
throw RuntimeException(ioException)
} catch (badData: BadData) {
val errorMsg = getMsg("sop.error.input.not_a_certificate", certInput)
throw BadData(errorMsg, badData)
}
}
try {
getInput(signature).use { sigIn -> detachedVerify.signatures(sigIn) }
} catch (e: IOException) {
throw RuntimeException(e)
} catch (badData: BadData) {
val errorMsg = getMsg("sop.error.input.not_a_signature", signature)
throw BadData(errorMsg, badData)
}
val verifications =
try {
detachedVerify.data(System.`in`)
} catch (e: NoSignature) {
val errorMsg = getMsg("sop.error.runtime.no_verifiable_signature_found")
throw NoSignature(errorMsg, e)
} catch (ioException: IOException) {
throw RuntimeException(ioException)
} catch (badData: BadData) {
val errorMsg = getMsg("sop.error.input.stdin_not_a_message")
throw BadData(errorMsg, badData)
}
for (verification in verifications) {
println(verification.toString())
}
}
}