mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-26 21:07:58 +01:00
SOP: Properly return error codes to the caller, error code 37 for invalid input args
This commit is contained in:
parent
fb1c0d6ff3
commit
24c0cd8a96
11 changed files with 72 additions and 17 deletions
|
@ -26,7 +26,7 @@ import org.pgpainless.sop.commands.Verify;
|
|||
import org.pgpainless.sop.commands.Version;
|
||||
import picocli.CommandLine;
|
||||
|
||||
@CommandLine.Command(
|
||||
@CommandLine.Command(exitCodeOnInvalidInput = 69,
|
||||
subcommands = {
|
||||
Armor.class,
|
||||
Dearmor.class,
|
||||
|
@ -41,8 +41,14 @@ import picocli.CommandLine;
|
|||
)
|
||||
public class PGPainlessCLI implements Runnable {
|
||||
|
||||
public PGPainlessCLI() {
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
CommandLine.run(new PGPainlessCLI(), args);
|
||||
int code = new CommandLine(new PGPainlessCLI())
|
||||
.execute(args);
|
||||
System.exit(code);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -28,7 +28,8 @@ import java.util.Arrays;
|
|||
import static org.pgpainless.sop.Print.err_ln;
|
||||
|
||||
@CommandLine.Command(name = "armor",
|
||||
description = "Add ASCII Armor to standard input")
|
||||
description = "Add ASCII Armor to standard input",
|
||||
exitCodeOnInvalidInput = 37)
|
||||
public class Armor implements Runnable {
|
||||
|
||||
private static final byte[] BEGIN_ARMOR = "-----BEGIN PGP".getBytes(StandardCharsets.UTF_8);
|
||||
|
|
|
@ -24,7 +24,8 @@ import java.io.IOException;
|
|||
import static org.pgpainless.sop.Print.err_ln;
|
||||
|
||||
@CommandLine.Command(name = "dearmor",
|
||||
description = "Remove ASCII Armor from standard input")
|
||||
description = "Remove ASCII Armor from standard input",
|
||||
exitCodeOnInvalidInput = 37)
|
||||
public class Dearmor implements Runnable {
|
||||
|
||||
@Override
|
||||
|
|
|
@ -43,7 +43,8 @@ import org.pgpainless.key.OpenPgpV4Fingerprint;
|
|||
import picocli.CommandLine;
|
||||
|
||||
@CommandLine.Command(name = "decrypt",
|
||||
description = "Decrypt a message from standard input")
|
||||
description = "Decrypt a message from standard input",
|
||||
exitCodeOnInvalidInput = 37)
|
||||
public class Decrypt implements Runnable {
|
||||
|
||||
private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
|
||||
|
|
|
@ -44,7 +44,8 @@ import org.pgpainless.util.Passphrase;
|
|||
import picocli.CommandLine;
|
||||
|
||||
@CommandLine.Command(name = "encrypt",
|
||||
description = "Encrypt a message from standard input")
|
||||
description = "Encrypt a message from standard input",
|
||||
exitCodeOnInvalidInput = 37)
|
||||
public class Encrypt implements Runnable {
|
||||
|
||||
public enum Type {
|
||||
|
|
|
@ -29,7 +29,8 @@ import org.pgpainless.sop.Print;
|
|||
import picocli.CommandLine;
|
||||
|
||||
@CommandLine.Command(name = "extract-cert",
|
||||
description = "Extract a public key certificate from a secret key from standard input")
|
||||
description = "Extract a public key certificate from a secret key from standard input",
|
||||
exitCodeOnInvalidInput = 37)
|
||||
public class ExtractCert implements Runnable {
|
||||
|
||||
@CommandLine.Option(names = "--no-armor",
|
||||
|
|
|
@ -38,7 +38,9 @@ import picocli.CommandLine;
|
|||
import static org.pgpainless.sop.Print.err_ln;
|
||||
import static org.pgpainless.sop.Print.print_ln;
|
||||
|
||||
@CommandLine.Command(name = "generate-key", description = "Generate a secret key")
|
||||
@CommandLine.Command(name = "generate-key",
|
||||
description = "Generate a secret key",
|
||||
exitCodeOnInvalidInput = 37)
|
||||
public class GenerateKey implements Runnable {
|
||||
|
||||
@CommandLine.Option(names = "--no-armor",
|
||||
|
@ -51,6 +53,12 @@ public class GenerateKey implements Runnable {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
if (userId.isEmpty()) {
|
||||
print_ln("At least one user-id expected.");
|
||||
System.exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
KeyRingBuilderInterface.WithAdditionalUserIdOrPassphrase builder = PGPainless.generateKeyRing()
|
||||
.withSubKey(KeySpec.getBuilder(KeyType.EDDSA(EdDSACurve._Ed25519))
|
||||
|
@ -64,12 +72,6 @@ public class GenerateKey implements Runnable {
|
|||
.withDefaultAlgorithms())
|
||||
.withPrimaryUserId(userId.get(0));
|
||||
|
||||
if (userId.isEmpty()) {
|
||||
print_ln("At least one user-id expected.");
|
||||
System.exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 1; i < userId.size(); i++) {
|
||||
builder.withAdditionalUserId(userId.get(i));
|
||||
}
|
||||
|
|
|
@ -35,7 +35,8 @@ import static org.pgpainless.sop.Print.err_ln;
|
|||
import static org.pgpainless.sop.Print.print_ln;
|
||||
|
||||
@CommandLine.Command(name = "sign",
|
||||
description = "Create a detached signature on the data from standard input")
|
||||
description = "Create a detached signature on the data from standard input",
|
||||
exitCodeOnInvalidInput = 37)
|
||||
public class Sign implements Runnable {
|
||||
|
||||
public enum Type {
|
||||
|
|
|
@ -43,7 +43,8 @@ import static org.pgpainless.sop.Print.err_ln;
|
|||
import static org.pgpainless.sop.Print.print_ln;
|
||||
|
||||
@CommandLine.Command(name = "verify",
|
||||
description = "Verify a detached signature over the data from standard input")
|
||||
description = "Verify a detached signature over the data from standard input",
|
||||
exitCodeOnInvalidInput = 37)
|
||||
public class Verify implements Runnable {
|
||||
|
||||
private static final TimeZone tz = TimeZone.getTimeZone("UTC");
|
||||
|
|
|
@ -22,7 +22,8 @@ import java.util.Properties;
|
|||
|
||||
import picocli.CommandLine;
|
||||
|
||||
@CommandLine.Command(name = "version", description = "Display version information about the tool")
|
||||
@CommandLine.Command(name = "version", description = "Display version information about the tool",
|
||||
exitCodeOnInvalidInput = 37)
|
||||
public class Version implements Runnable {
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright 2021 Paul Schaub.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.pgpainless.sop;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import picocli.CommandLine;
|
||||
|
||||
public class ExitCodeTest {
|
||||
|
||||
@Test
|
||||
public void testUnknownCommand_69() {
|
||||
assertEquals(69, new CommandLine(new PGPainlessCLI()).execute("generate-kex"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCommandWithUnknownOption_37() {
|
||||
assertEquals(37, new CommandLine(new PGPainlessCLI()).execute("generate-key", "-k", "\"k is unknown\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void successfulVersion_0 () {
|
||||
assertEquals(0, new CommandLine(new PGPainlessCLI()).execute("version"));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue