mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2024-12-22 04:57:56 +01:00
Kotlin conversion: ListProfilesExternal
This commit is contained in:
parent
8dc51b67a3
commit
f2204dfd4d
2 changed files with 36 additions and 50 deletions
|
@ -1,50 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
|
||||||
|
|
||||||
package sop.external.operation;
|
|
||||||
|
|
||||||
import sop.Profile;
|
|
||||||
import sop.external.ExternalSOP;
|
|
||||||
import sop.operation.ListProfiles;
|
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
public class ListProfilesExternal implements 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
|
|
||||||
@Nonnull
|
|
||||||
public List<Profile> subcommand(@Nonnull String command) {
|
|
||||||
commandList.add(command);
|
|
||||||
try {
|
|
||||||
String output = new String(ExternalSOP.executeProducingOperation(Runtime.getRuntime(), commandList, envList).getBytes());
|
|
||||||
return toProfiles(output);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<Profile> toProfiles(String output) {
|
|
||||||
List<Profile> profiles = new ArrayList<>();
|
|
||||||
for (String line : output.split("\n")) {
|
|
||||||
if (line.trim().isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
profiles.add(Profile.parse(line));
|
|
||||||
}
|
|
||||||
return profiles;
|
|
||||||
}
|
|
||||||
}
|
|
36
external-sop/src/main/kotlin/sop/external/operation/ListProfilesExternal.kt
vendored
Normal file
36
external-sop/src/main/kotlin/sop/external/operation/ListProfilesExternal.kt
vendored
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package sop.external.operation
|
||||||
|
|
||||||
|
import java.io.IOException
|
||||||
|
import java.util.Properties
|
||||||
|
import sop.Profile
|
||||||
|
import sop.external.ExternalSOP
|
||||||
|
import sop.operation.ListProfiles
|
||||||
|
|
||||||
|
/** Implementation of the [ListProfiles] operation using an external SOP binary. */
|
||||||
|
class ListProfilesExternal(binary: String, environment: Properties) : ListProfiles {
|
||||||
|
|
||||||
|
private val commandList = mutableListOf(binary, "list-profiles")
|
||||||
|
private val envList = ExternalSOP.propertiesToEnv(environment)
|
||||||
|
|
||||||
|
override fun subcommand(command: String): List<Profile> {
|
||||||
|
return try {
|
||||||
|
String(
|
||||||
|
ExternalSOP.executeProducingOperation(
|
||||||
|
Runtime.getRuntime(), commandList.plus(command), envList)
|
||||||
|
.bytes)
|
||||||
|
.let { toProfiles(it) }
|
||||||
|
} catch (e: IOException) {
|
||||||
|
throw RuntimeException(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
private fun toProfiles(output: String): List<Profile> =
|
||||||
|
output.split("\n").filter { it.isNotBlank() }.map { Profile.parse(it) }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue