5 |
5 |
6 abstract class VcsConnector(protected val path: String) { |
6 abstract class VcsConnector(protected val path: String) { |
7 /** |
7 /** |
8 * Invokes the VCS binary with the given [args] and returns the output on stdout. |
8 * Invokes the VCS binary with the given [args] and returns the output on stdout. |
9 */ |
9 */ |
10 protected fun invokeCommand(workingDir: Path, vararg args : String): VcsConnectorResult<String> { |
10 protected fun invokeCommand( |
|
11 vararg args: String, |
|
12 workingDir: Path = Path.of("."), |
|
13 timeout: Long = 30L |
|
14 ): VcsConnectorResult<String> { |
11 return try { |
15 return try { |
12 val command = mutableListOf(path) |
16 val command = mutableListOf(path) |
13 command.addAll(args) |
17 command.addAll(args) |
14 val process = ProcessBuilder(command).directory(workingDir.toFile()).start() |
18 val process = ProcessBuilder(command).directory(workingDir.toFile()).start() |
15 val stdout = String(process.inputStream.readAllBytes(), Charsets.UTF_8) |
19 val stdout = String(process.inputStream.readAllBytes(), Charsets.UTF_8) |
16 if (process.waitFor(30, TimeUnit.SECONDS)) { |
20 if (process.waitFor(timeout, TimeUnit.SECONDS)) { |
17 if (process.exitValue() == 0) { |
21 if (process.exitValue() == 0) { |
18 VcsConnectorResult.Success(stdout) |
22 VcsConnectorResult.Success(stdout) |
19 } else { |
23 } else { |
20 VcsConnectorResult.Error("VCS process did not return successfully.") |
24 VcsConnectorResult.Error("VCS process did not return successfully.") |
21 } |
25 } |