Checkstyle and exception handling improvements

This commit is contained in:
Paul Schaub 2024-09-18 16:01:58 +02:00
parent 3104085fe7
commit 5e1915c3ce
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 13 additions and 12 deletions

View file

@ -4,11 +4,11 @@
package sop.cli.picocli.commands package sop.cli.picocli.commands
import java.io.IOException
import picocli.CommandLine import picocli.CommandLine
import picocli.CommandLine.Command import picocli.CommandLine.Command
import sop.cli.picocli.SopCLI import sop.cli.picocli.SopCLI
import sop.exception.SOPGPException import sop.exception.SOPGPException
import java.io.IOException
@Command( @Command(
name = "merge-certs", name = "merge-certs",
@ -16,11 +16,9 @@ import java.io.IOException
exitCodeOnInvalidInput = SOPGPException.UnsupportedOption.EXIT_CODE) exitCodeOnInvalidInput = SOPGPException.UnsupportedOption.EXIT_CODE)
class MergeCertsCmd : AbstractSopCmd() { class MergeCertsCmd : AbstractSopCmd() {
@CommandLine.Option(names = ["--no-armor"], negatable = true) @CommandLine.Option(names = ["--no-armor"], negatable = true) var armor = false
var armor = false
@CommandLine.Parameters(paramLabel = "CERTS") @CommandLine.Parameters(paramLabel = "CERTS") var updates: List<String> = listOf()
var updates: List<String> = listOf()
override fun run() { override fun run() {
val mergeCerts = throwIfUnsupportedSubcommand(SopCLI.getSop().mergeCerts(), "merge-certs") val mergeCerts = throwIfUnsupportedSubcommand(SopCLI.getSop().mergeCerts(), "merge-certs")
@ -45,4 +43,4 @@ class MergeCertsCmd : AbstractSopCmd() {
throw RuntimeException(e) throw RuntimeException(e)
} }
} }
} }

View file

@ -4,11 +4,11 @@
package sop.cli.picocli.commands package sop.cli.picocli.commands
import java.io.IOException
import picocli.CommandLine.Command import picocli.CommandLine.Command
import picocli.CommandLine.Option import picocli.CommandLine.Option
import sop.cli.picocli.SopCLI import sop.cli.picocli.SopCLI
import sop.exception.SOPGPException.* import sop.exception.SOPGPException.*
import java.io.IOException
@Command( @Command(
name = "update-key", name = "update-key",
@ -25,8 +25,7 @@ class UpdateKeyCmd : AbstractSopCmd() {
@Option(names = ["--with-key-password"], paramLabel = "PASSWORD") @Option(names = ["--with-key-password"], paramLabel = "PASSWORD")
var withKeyPassword: List<String> = listOf() var withKeyPassword: List<String> = listOf()
@Option(names = ["--merge-certs"], paramLabel = "CERTS") @Option(names = ["--merge-certs"], paramLabel = "CERTS") var mergeCerts: List<String> = listOf()
var mergeCerts: List<String> = listOf()
override fun run() { override fun run() {
val updateKey = throwIfUnsupportedSubcommand(SopCLI.getSop().updateKey(), "update-key") val updateKey = throwIfUnsupportedSubcommand(SopCLI.getSop().updateKey(), "update-key")
@ -48,7 +47,8 @@ class UpdateKeyCmd : AbstractSopCmd() {
val password = stringFromInputStream(getInput(passwordFileName)) val password = stringFromInputStream(getInput(passwordFileName))
updateKey.withKeyPassword(password) updateKey.withKeyPassword(password)
} catch (unsupportedOption: UnsupportedOption) { } catch (unsupportedOption: UnsupportedOption) {
val errorMsg = getMsg("sop.error.feature_support.option_not_supported", "--with-key-password") val errorMsg =
getMsg("sop.error.feature_support.option_not_supported", "--with-key-password")
throw UnsupportedOption(errorMsg, unsupportedOption) throw UnsupportedOption(errorMsg, unsupportedOption)
} catch (e: IOException) { } catch (e: IOException) {
throw RuntimeException(e) throw RuntimeException(e)
@ -57,7 +57,7 @@ class UpdateKeyCmd : AbstractSopCmd() {
for (certInput in mergeCerts) { for (certInput in mergeCerts) {
try { try {
getInput(certInput).use { certIn -> updateKey.mergeCerts(certIn) } getInput(certInput).use { updateKey.mergeCerts(it) }
} catch (e: IOException) { } catch (e: IOException) {
throw RuntimeException(e) throw RuntimeException(e)
} catch (badData: BadData) { } catch (badData: BadData) {
@ -71,6 +71,9 @@ class UpdateKeyCmd : AbstractSopCmd() {
ready.writeTo(System.out) ready.writeTo(System.out)
} catch (e: IOException) { } catch (e: IOException) {
throw RuntimeException(e) throw RuntimeException(e)
} catch (badData: BadData) {
val errorMsg = getMsg("sop.error.input.not_a_private_key", "STDIN")
throw BadData(errorMsg, badData)
} }
} }
} }