Skip to content

Commit ca2bf05

Browse files
authored
Merge pull request #13 from devchat-ai/fix-topic-list
Refactor command runner
2 parents 2307b79 + 830a171 commit ca2bf05

2 files changed

Lines changed: 7 additions & 16 deletions

File tree

src/main/kotlin/ai/devchat/cli/DevChatWrapper.kt

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class DevChatWrapper {
139139

140140
val commandList: JSONArray
141141
get() {
142-
val result = runRunCommand("--list", null)
142+
val result = runCommand(null, "run", "--list")
143143
return JSON.parseArray(result)
144144
}
145145
val commandNamesList: Array<String?>
@@ -163,26 +163,17 @@ class DevChatWrapper {
163163
}
164164

165165
fun listTopics(): JSONArray {
166-
val result = runTopicCommand("--list", null)
166+
val result = runCommand(null, "topic", "--list")
167167
return JSON.parseArray(result)
168168
}
169169

170-
fun runRunCommand(subCommand: String?, flags: Map<String, List<String?>>?): String {
170+
fun runCommand(flags: Map<String, List<String?>>?, vararg subCommands: String): String {
171171
return try {
172-
val commands: List<String?> = prepareCommand(flags, "run", subCommand!!)
172+
val commands: List<String?> = prepareCommand(flags, *subCommands)
173173
execCommand(commands)
174174
} catch (e: Exception) {
175175
Log.error("Failed to run [run] command: " + e.message)
176-
throw RuntimeException("Failed to run [run] command", e)
177-
}
178-
}
179-
180-
fun runTopicCommand(subCommand: String?, flags: Map<String, List<String?>>?): String {
181-
return try {
182-
val commands: List<String?> = prepareCommand(flags, "topic", subCommand!!)
183-
execCommand(commands)
184-
} catch (e: Exception) {
185-
throw RuntimeException("Failed to run [topic] command", e)
176+
throw RuntimeException("Failed to run [${subCommands}] command", e)
186177
}
187178
}
188179

@@ -205,7 +196,7 @@ class DevChatWrapper {
205196
private fun prepareCommand(subCommand: String, flags: Map<String, List<String?>>, message: String?): List<String?> {
206197
val commands = prepareCommand(flags, subCommand)
207198
// Add the message to the command list
208-
if (message != null && !message.isEmpty()) {
199+
if (!message.isNullOrEmpty()) {
209200
commands.add("--")
210201
commands.add(message)
211202
}

src/main/kotlin/ai/devchat/devchat/handler/SendMessageRequestHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class SendMessageRequestHandler(private val devChatActionHandler: DevChatActionH
101101
if (message.length > command!!.length + 2) {
102102
message = message.substring(command.length + 2) // +2 to take into account the '/' and the space ' '
103103
}
104-
runResult = devchatWrapper.runRunCommand(command, null)
104+
runResult = devchatWrapper.runCommand(null, command)
105105
break
106106
}
107107
}

0 commit comments

Comments
 (0)