2023-01-05 03:06:22 +01:00
|
|
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2023-01-07 15:23:57 +01:00
|
|
|
package sop.external.operation;
|
2023-01-05 03:06:22 +01:00
|
|
|
|
2023-01-09 14:56:53 +01:00
|
|
|
import sop.external.ExternalSOP;
|
2023-01-05 03:06:22 +01:00
|
|
|
import sop.operation.Version;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
2023-01-09 14:56:53 +01:00
|
|
|
import java.util.Properties;
|
2023-01-05 03:06:22 +01:00
|
|
|
|
2023-01-07 15:23:57 +01:00
|
|
|
public class VersionExternal implements Version {
|
2023-01-05 03:06:22 +01:00
|
|
|
|
|
|
|
private final Runtime runtime = Runtime.getRuntime();
|
|
|
|
private final String binary;
|
2023-01-09 14:56:53 +01:00
|
|
|
private final Properties environment;
|
2023-01-05 03:06:22 +01:00
|
|
|
|
2023-01-09 14:56:53 +01:00
|
|
|
public VersionExternal(String binaryName, Properties environment) {
|
2023-01-05 03:06:22 +01:00
|
|
|
this.binary = binaryName;
|
2023-01-09 14:56:53 +01:00
|
|
|
this.environment = environment;
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
String[] command = new String[] {binary, "version"};
|
2023-01-09 14:56:53 +01:00
|
|
|
String[] env = ExternalSOP.propertiesToEnv(environment).toArray(new String[0]);
|
2023-01-05 03:06:22 +01:00
|
|
|
try {
|
2023-01-09 14:56:53 +01:00
|
|
|
Process process = runtime.exec(command, env);
|
2023-01-05 03:06:22 +01:00
|
|
|
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
|
|
String line = stdInput.readLine().trim();
|
2023-01-12 16:04:44 +01:00
|
|
|
ExternalSOP.finish(process);
|
2023-01-05 03:06:22 +01:00
|
|
|
if (line.contains(" ")) {
|
|
|
|
return line.substring(0, line.lastIndexOf(" "));
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getVersion() {
|
|
|
|
String[] command = new String[] {binary, "version"};
|
2023-01-09 14:56:53 +01:00
|
|
|
String[] env = ExternalSOP.propertiesToEnv(environment).toArray(new String[0]);
|
2023-01-05 03:06:22 +01:00
|
|
|
try {
|
2023-01-09 14:56:53 +01:00
|
|
|
Process process = runtime.exec(command, env);
|
2023-01-05 03:06:22 +01:00
|
|
|
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
|
|
String line = stdInput.readLine().trim();
|
2023-01-12 16:04:44 +01:00
|
|
|
ExternalSOP.finish(process);
|
2023-01-05 03:06:22 +01:00
|
|
|
if (line.contains(" ")) {
|
|
|
|
return line.substring(line.lastIndexOf(" ") + 1);
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getBackendVersion() {
|
|
|
|
String[] command = new String[] {binary, "version", "--backend"};
|
2023-01-09 14:56:53 +01:00
|
|
|
String[] env = ExternalSOP.propertiesToEnv(environment).toArray(new String[0]);
|
2023-01-05 03:06:22 +01:00
|
|
|
try {
|
2023-01-09 14:56:53 +01:00
|
|
|
Process process = runtime.exec(command, env);
|
2023-01-05 03:06:22 +01:00
|
|
|
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
String line;
|
|
|
|
while ((line = stdInput.readLine()) != null) {
|
|
|
|
sb.append(line).append('\n');
|
|
|
|
}
|
2023-01-09 14:56:53 +01:00
|
|
|
ExternalSOP.finish(process);
|
2023-01-05 03:06:22 +01:00
|
|
|
return sb.toString();
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getExtendedVersion() {
|
|
|
|
String[] command = new String[] {binary, "version", "--extended"};
|
2023-01-09 14:56:53 +01:00
|
|
|
String[] env = ExternalSOP.propertiesToEnv(environment).toArray(new String[0]);
|
2023-01-05 03:06:22 +01:00
|
|
|
try {
|
2023-01-09 14:56:53 +01:00
|
|
|
Process process = runtime.exec(command, env);
|
2023-01-05 03:06:22 +01:00
|
|
|
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
String line;
|
|
|
|
while ((line = stdInput.readLine()) != null) {
|
|
|
|
sb.append(line).append('\n');
|
|
|
|
}
|
2023-01-09 14:56:53 +01:00
|
|
|
ExternalSOP.finish(process);
|
2023-01-05 03:06:22 +01:00
|
|
|
return sb.toString();
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|