001// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
002//
003// SPDX-License-Identifier: Apache-2.0
004
005package sop.cli.picocli.commands;
006
007import java.io.IOException;
008
009import picocli.CommandLine;
010import sop.Ready;
011import sop.cli.picocli.SopCLI;
012import sop.exception.SOPGPException;
013import sop.operation.ExtractCert;
014
015@CommandLine.Command(name = "extract-cert",
016        description = "Extract a public key certificate from a secret key from standard input",
017        exitCodeOnInvalidInput = 37)
018public class ExtractCertCmd implements Runnable {
019
020    @CommandLine.Option(names = "--no-armor",
021            description = "ASCII armor the output",
022            negatable = true)
023    boolean armor = true;
024
025    @Override
026    public void run() {
027        ExtractCert extractCert = SopCLI.getSop().extractCert();
028        if (!armor) {
029            extractCert.noArmor();
030        }
031
032        try {
033            Ready ready = extractCert.key(System.in);
034            ready.writeTo(System.out);
035        } catch (IOException e) {
036            throw new RuntimeException(e);
037        } catch (SOPGPException.BadData badData) {
038            throw new SOPGPException.BadData("Standard Input does not contain valid OpenPGP private key material.", badData);
039        }
040    }
041}