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.Print;
012import sop.cli.picocli.SopCLI;
013import sop.enums.ArmorLabel;
014import sop.exception.SOPGPException;
015import sop.operation.Armor;
016
017@CommandLine.Command(name = "armor",
018        description = "Add ASCII Armor to standard input",
019        exitCodeOnInvalidInput = SOPGPException.UnsupportedOption.EXIT_CODE)
020public class ArmorCmd implements Runnable {
021
022    @CommandLine.Option(names = {"--label"}, description = "Label to be used in the header and tail of the armoring.", paramLabel = "{auto|sig|key|cert|message}")
023    ArmorLabel label;
024
025    @Override
026    public void run() {
027        Armor armor = SopCLI.getSop().armor();
028        if (label != null) {
029            try {
030                armor.label(label);
031            } catch (SOPGPException.UnsupportedOption unsupportedOption) {
032                Print.errln("Armor labels not supported.");
033                System.exit(unsupportedOption.getExitCode());
034            }
035        }
036
037        try {
038            Ready ready = armor.data(System.in);
039            ready.writeTo(System.out);
040        } catch (SOPGPException.BadData badData) {
041            Print.errln("Bad data.");
042            Print.trace(badData);
043            System.exit(badData.getExitCode());
044        } catch (IOException e) {
045            Print.errln("IO Error.");
046            Print.trace(e);
047            System.exit(1);
048        }
049    }
050}