sop-java/external-sop/src/main/java/sop/external/operation/ListProfilesExternal.java

51 lines
1.5 KiB
Java
Raw Normal View History

2023-04-11 15:06:37 +02:00
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.external.operation;
2023-04-14 14:06:34 +02:00
import sop.Profile;
2023-04-11 15:06:37 +02:00
import sop.external.ExternalSOP;
import sop.operation.ListProfiles;
import javax.annotation.Nonnull;
2023-04-11 15:06:37 +02:00
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
2023-04-11 15:27:23 +02:00
public class ListProfilesExternal implements ListProfiles {
2023-04-11 15:06:37 +02:00
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
@Nonnull
public List<Profile> subcommand(@Nonnull String command) {
2023-04-11 15:06:37 +02:00
commandList.add(command);
try {
String output = new String(ExternalSOP.executeProducingOperation(Runtime.getRuntime(), commandList, envList).getBytes());
2023-04-14 14:06:34 +02:00
return toProfiles(output);
2023-04-11 15:06:37 +02:00
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static List<Profile> toProfiles(String output) {
2023-04-14 14:06:34 +02:00
List<Profile> profiles = new ArrayList<>();
for (String line : output.split("\n")) {
2023-04-27 14:25:16 +02:00
if (line.trim().isEmpty()) {
continue;
}
profiles.add(Profile.parse(line));
2023-04-11 15:06:37 +02:00
}
2023-04-14 14:06:34 +02:00
return profiles;
2023-04-11 15:06:37 +02:00
}
}