-
Notifications
You must be signed in to change notification settings - Fork 0
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
158203a
commit bd30824
Showing
3 changed files
with
100 additions
and
52 deletions.
There are no files selected for viewing
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
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
49 changes: 49 additions & 0 deletions
49
webui/src/main/kotlin/com/simiacryptus/diff/AddShellExecutionLinks.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,49 @@ | ||
package com.simiacryptus.diff | ||
|
||
import com.simiacryptus.skyenet.webui.application.ApplicationInterface | ||
import com.simiacryptus.skyenet.webui.session.SocketManagerBase | ||
import com.simiacryptus.skyenet.webui.util.MarkdownUtil | ||
import java.util.* | ||
import java.io.BufferedReader | ||
import java.io.InputStreamReader | ||
|
||
fun SocketManagerBase.addShellExecutionLinks( | ||
response: String, | ||
ui: ApplicationInterface | ||
): String { | ||
val shellCodePattern = """(?s)(?<![^\n])```shell\n(.*?)\n```""".toRegex() | ||
return shellCodePattern.replace(response) { matchResult -> | ||
val shellCode = matchResult.groupValues[1] | ||
val executionId = UUID.randomUUID().toString() | ||
val executionTask = ui.newTask(false) | ||
val executeButton = hrefLink("Execute", classname = "href-link cmd-button") { | ||
try { | ||
val process = Runtime.getRuntime().exec(arrayOf("sh", "-c", shellCode)) | ||
val reader = BufferedReader(InputStreamReader(process.inputStream)) | ||
val errorReader = BufferedReader(InputStreamReader(process.errorStream)) | ||
val output = StringBuilder() | ||
var line: String? | ||
while (reader.readLine().also { line = it } != null) { | ||
output.append(line).append("\n") | ||
} | ||
while (errorReader.readLine().also { line = it } != null) { | ||
output.append("Error: ").append(line).append("\n") | ||
} | ||
val exitCode = process.waitFor() | ||
output.append("Exit code: $exitCode") | ||
executionTask.complete(MarkdownUtil.renderMarkdown("```\n${output.toString()}\n```", ui = ui)) | ||
} catch (e: Throwable) { | ||
executionTask.error(null, e) | ||
} | ||
} | ||
""" | ||
```shell | ||
$shellCode | ||
``` | ||
<div id="execution-$executionId"> | ||
$executeButton | ||
${executionTask.placeholder} | ||
</div> | ||
""".trimIndent() | ||
} | ||
} |