-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
832a455
commit 3eaae14
Showing
4 changed files
with
98 additions
and
179 deletions.
There are no files selected for viewing
163 changes: 0 additions & 163 deletions
163
external-sop/src/main/java/sop/external/operation/VersionExternal.java
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
external-sop/src/main/java/sop/external/operation/package-info.java
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
98 changes: 98 additions & 0 deletions
98
external-sop/src/main/kotlin/sop/external/operation/VersionExternal.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// 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.external.ExternalSOP | ||
import sop.operation.Version | ||
|
||
/** Implementation of the [Version] operation using an external SOP binary. */ | ||
class VersionExternal(binary: String, environment: Properties) : Version { | ||
|
||
private val commandList = listOf(binary, "version") | ||
private val envList = ExternalSOP.propertiesToEnv(environment) | ||
|
||
override fun getName(): String { | ||
val info = executeForLine(commandList) | ||
return if (info.contains(" ")) { | ||
info.substring(0, info.lastIndexOf(" ")) | ||
} else { | ||
info | ||
} | ||
} | ||
|
||
override fun getVersion(): String { | ||
val info = executeForLine(commandList) | ||
return if (info.contains(" ")) { | ||
info.substring(info.lastIndexOf(" ") + 1) | ||
} else { | ||
info | ||
} | ||
} | ||
|
||
override fun getBackendVersion(): String { | ||
return executeForLines(commandList.plus("--backend")) | ||
} | ||
|
||
override fun getExtendedVersion(): String { | ||
return executeForLines(commandList.plus("--extended")) | ||
} | ||
|
||
override fun getSopSpecRevisionNumber(): Int { | ||
val revision = getSopSpecVersion() | ||
val firstLine = | ||
if (revision.contains("\n")) { | ||
revision.substring(0, revision.indexOf("\n")) | ||
} else { | ||
revision | ||
} | ||
|
||
if (!firstLine.contains("-")) { | ||
return -1 | ||
} | ||
return Integer.parseInt(firstLine.substring(firstLine.lastIndexOf("-") + 1)) | ||
} | ||
|
||
override fun isSopSpecImplementationIncomplete(): Boolean { | ||
return getSopSpecVersion().startsWith("~") | ||
} | ||
|
||
override fun getSopSpecImplementationRemarks(): String? { | ||
val revision = getSopSpecVersion() | ||
if (revision.contains("\n")) { | ||
revision.substring(revision.indexOf("\n")).trim().takeIf { it.isNotBlank() } | ||
} | ||
return null | ||
} | ||
|
||
override fun getSopSpecVersion(): String { | ||
return executeForLines(commandList.plus("--sop-spec")) | ||
} | ||
|
||
private fun executeForLine(commandList: List<String>): String { | ||
return try { | ||
val process = | ||
Runtime.getRuntime().exec(commandList.toTypedArray(), envList.toTypedArray()) | ||
val result = process.inputStream.bufferedReader().readLine() | ||
ExternalSOP.finish(process) | ||
result.trim() | ||
} catch (e: IOException) { | ||
throw RuntimeException(e) | ||
} | ||
} | ||
|
||
private fun executeForLines(commandList: List<String>): String { | ||
return try { | ||
val process = | ||
Runtime.getRuntime().exec(commandList.toTypedArray(), envList.toTypedArray()) | ||
val result = process.inputStream.bufferedReader().readLines().joinToString("\n") | ||
ExternalSOP.finish(process) | ||
result.trim() | ||
} catch (e: IOException) { | ||
throw RuntimeException(e) | ||
} | ||
} | ||
} |