Add list-profiles command

This commit is contained in:
Paul Schaub 2023-04-11 15:06:37 +02:00
parent 17b305924c
commit 83a003e80f
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
14 changed files with 216 additions and 0 deletions

View File

@ -18,6 +18,7 @@ import sop.external.operation.GenerateKeyExternal;
import sop.external.operation.InlineDetachExternal;
import sop.external.operation.InlineSignExternal;
import sop.external.operation.InlineVerifyExternal;
import sop.external.operation.ListProfilesExternal;
import sop.external.operation.VersionExternal;
import sop.operation.Armor;
import sop.operation.Dearmor;
@ -30,6 +31,7 @@ import sop.operation.GenerateKey;
import sop.operation.InlineDetach;
import sop.operation.InlineSign;
import sop.operation.InlineVerify;
import sop.operation.ListProfiles;
import sop.operation.Version;
import javax.annotation.Nonnull;
@ -154,6 +156,11 @@ public class ExternalSOP implements SOP {
return new ArmorExternal(binaryName, properties);
}
@Override
public ListProfiles listProfiles() {
return new ListProfilesExternal(binaryName, properties);
}
@Override
public Dearmor dearmor() {
return new DearmorExternal(binaryName, properties);

View File

@ -51,6 +51,12 @@ public class GenerateKeyExternal implements GenerateKey {
return this;
}
@Override
public GenerateKey profile(String profile) {
commandList.add("--profile=" + profile);
return this;
}
@Override
public Ready generate()
throws SOPGPException.MissingArg, SOPGPException.UnsupportedAsymmetricAlgo {

View File

@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.external.operation;
import sop.external.ExternalSOP;
import sop.operation.ListProfiles;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
public class ListProfilesExternal extends ListProfiles {
private final List<String> commandList = new ArrayList<>();
private final List<String> envList;
public ListProfilesExternal(String binary, Properties properties) {
this.commandList.add(binary);
this.commandList.add("list-profiles");
this.envList = ExternalSOP.propertiesToEnv(properties);
}
@Override
public List<String> ofCommand(String command) {
commandList.add(command);
try {
String output = new String(ExternalSOP.executeProducingOperation(Runtime.getRuntime(), commandList, envList).getBytes());
return Arrays.asList(output.split("\n"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public List<String> global() {
try {
String output = new String(ExternalSOP.executeProducingOperation(Runtime.getRuntime(), commandList, envList).getBytes());
return Arrays.asList(output.split("\n"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.testsuite.external.operation;
import org.junit.jupiter.api.condition.EnabledIf;
import sop.testsuite.operation.ListProfilesTest;
@EnabledIf("sop.testsuite.operation.AbstractSOPTest#hasBackends")
public class ExternalListProfilesTest extends ListProfilesTest {
}

View File

@ -16,6 +16,7 @@ import sop.cli.picocli.commands.ExtractCertCmd;
import sop.cli.picocli.commands.GenerateKeyCmd;
import sop.cli.picocli.commands.InlineSignCmd;
import sop.cli.picocli.commands.InlineVerifyCmd;
import sop.cli.picocli.commands.ListProfilesCmd;
import sop.cli.picocli.commands.SignCmd;
import sop.cli.picocli.commands.VerifyCmd;
import sop.cli.picocli.commands.VersionCmd;
@ -41,6 +42,7 @@ import java.util.ResourceBundle;
VerifyCmd.class,
InlineSignCmd.class,
InlineVerifyCmd.class,
ListProfilesCmd.class,
VersionCmd.class,
AutoComplete.GenerateCompletion.class
}

View File

@ -30,6 +30,10 @@ public class GenerateKeyCmd extends AbstractSopCmd {
paramLabel = "PASSWORD")
String withKeyPassword;
@CommandLine.Option(names = "--profile",
paramLabel = "PROFILE")
String profile = "default";
@Override
public void run() {
GenerateKey generateKey = throwIfUnsupportedSubcommand(
@ -43,6 +47,8 @@ public class GenerateKeyCmd extends AbstractSopCmd {
generateKey.noArmor();
}
generateKey.profile(profile);
if (withKeyPassword != null) {
try {
String password = stringFromInputStream(getInput(withKeyPassword));

View File

@ -0,0 +1,39 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.cli.picocli.commands;
import picocli.CommandLine;
import sop.cli.picocli.SopCLI;
import sop.operation.ListProfiles;
@CommandLine.Command(name = "list-profiles",
resourceBundle = "msg_list-profiles",
exitCodeOnInvalidInput = 37)
public class ListProfilesCmd extends AbstractSopCmd {
@CommandLine.Parameters(paramLabel = "COMMAND", arity="0..1", descriptionKey = "subcommand")
String subcommand;
@Override
public void run() {
ListProfiles listProfiles = throwIfUnsupportedSubcommand(
SopCLI.getSop().listProfiles(), "list-profiles");
if (subcommand == null) {
for (String profile : listProfiles.global()) {
// CHECKSTYLE:OFF
System.out.println(profile);
// CHECKSTYLE:ON
}
return;
}
for (String profile : listProfiles.ofCommand(subcommand)) {
// CHECKSTYLE:OFF
System.out.println(profile);
// CHECKSTYLE:ON
}
}
}

View File

@ -0,0 +1,10 @@
# SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
#
# SPDX-License-Identifier: Apache-2.0
usage.header=Emit a list of profiles supported by the identified subcommand
subcommand=Subcommand for which to list profiles
# Generic TODO: Remove when bumping picocli to 4.7.0
usage.synopsisHeading=Usage:\u0020
usage.commandListHeading = %nCommands:%n
usage.optionListHeading = %nOptions:%n
usage.footerHeading=Powered by picocli%n

View File

@ -0,0 +1,10 @@
# SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
#
# SPDX-License-Identifier: Apache-2.0
usage.header=Gebe eine Liste von Profilen aus, welche vom angegebenen Unterbefehl unterstützt werden
subcommand=Unterbefehl für welchen Profile gelistet werden sollen
# Generic TODO: Remove when bumping picocli to 4.7.0
usage.synopsisHeading=Aufruf:\u0020
usage.commandListHeading=%nBefehle:%n
usage.optionListHeading = %nOptionen:%n
usage.footerHeading=Powered by Picocli%n

View File

@ -26,6 +26,7 @@ import sop.operation.InlineSign;
import sop.operation.InlineVerify;
import sop.operation.DetachedSign;
import sop.operation.DetachedVerify;
import sop.operation.ListProfiles;
import sop.operation.Version;
public class SOPTest {
@ -95,6 +96,11 @@ public class SOPTest {
return null;
}
@Override
public ListProfiles listProfiles() {
return null;
}
@Override
public InlineDetach inlineDetach() {
return null;

View File

@ -15,6 +15,7 @@ import sop.operation.InlineSign;
import sop.operation.InlineVerify;
import sop.operation.DetachedSign;
import sop.operation.DetachedVerify;
import sop.operation.ListProfiles;
import sop.operation.Version;
/**
@ -146,4 +147,5 @@ public interface SOP {
*/
Dearmor dearmor();
ListProfiles listProfiles();
}

View File

@ -6,6 +6,7 @@ package sop.operation;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import sop.Ready;
import sop.exception.SOPGPException;
@ -56,6 +57,14 @@ public interface GenerateKey {
return withKeyPassword(UTF8Util.decodeUTF8(password));
}
/**
* Pass in a profile identifier.
*
* @param profile profile identifier
* @return this
*/
GenerateKey profile(String profile);
/**
* Generate the OpenPGP key and return it encoded as an {@link InputStream}.
*

View File

@ -0,0 +1,19 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.operation;
import java.util.List;
public abstract class ListProfiles {
public ListProfiles() {
}
public abstract List<String> ofCommand(String command);
public abstract List<String> global();
}

View File

@ -0,0 +1,40 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.testsuite.operation;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import sop.SOP;
import java.io.IOException;
import java.util.List;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertFalse;
public class ListProfilesTest extends AbstractSOPTest {
static Stream<Arguments> provideInstances() {
return provideBackends();
}
@ParameterizedTest
@MethodSource("provideInstances")
public void listGlobalProfiles(SOP sop) throws IOException {
List<String> profiles = sop.listProfiles()
.global();
assertFalse(profiles.isEmpty());
}
@ParameterizedTest
@MethodSource("provideInstances")
public void listGenerateKeyProfiles(SOP sop) throws IOException {
List<String> profiles = sop.listProfiles()
.ofCommand("generate-key");
assertFalse(profiles.isEmpty());
}
}